Ejemplo n.º 1
0
// SaveHandler saves a item
func SaveHandler(w http.ResponseWriter, r *http.Request) {
	u, _ := auth.GetAuthenticatedUser(r)

	hash := r.FormValue("hash")
	name := r.FormValue("name")
	kind := Open
	folder := r.FormValue("folder")
	created := time.Now()

	if hash == "" {
		hash = crypto.UniqueHash(name)
	}

	room, err := repo.Load(hash)
	if err == nil {
		created = room.Created
	}

	room = &Room{Hash: hash, Name: name, Kind: kind, Folder: folder, Created: created}
	err = repo.Save(room)
	render.Check(err, w)

	// Add members to room
	members := r.Form["members"]
	members = append(members, u.Key)
	for _, user := range members {
		hash = crypto.Hash(room.Hash + user)
		rm := &RoomMember{Hash: hash, User: user, Room: room.Hash}
		err = roomMemberRepo.Save(rm)
		render.Check(err, w)
	}

	http.Redirect(w, r, "/r/"+room.Hash, http.StatusFound)
}
Ejemplo n.º 2
0
// SpokeHandler handles webocket requests from the peer.
func SpokeHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}

	// Upgrade request to WebSocket
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		}
		return
	}

	// Check for authenticated user
	if !auth.IsAuthenticated(r) {
		return
	}

	// Grab user making request
	u, err := auth.GetAuthenticatedUser(r)
	if err != nil {
		log.Println(err)
	}

	// Grab cookie
	cookie, _ := r.Cookie("authenticated-user")

	// Create connection
	c := &Connection{send: make(chan []byte, 256), ws: ws, User: u, Cookie: cookie}
	Hub.Register <- c
	go c.writePump()
	c.readPump()
}
Ejemplo n.º 3
0
func messageSaveHandler(w http.ResponseWriter, r *http.Request) {
	au, err := auth.GetAuthenticatedUser(r)
	if err != nil {
		http.Redirect(w, r, "/login", http.StatusFound)
		return
	}

	hash := r.FormValue("hash")
	room := r.FormValue("room")
	text := r.FormValue("text")

	if hash == "" {
		hash = crypto.UniqueHash(text)
	}

	m := &messages.Message{Hash: hash, Room: room, User: au.Key, Text: text}
	err = messageRepo.Save(m)
	render.Check(err, w)

	// Check for any resources in message
	go dropbox.HandleDropboxFilesPut("DMX/Test.gdoc", text, r)

	// Push members
	go push.PushMembers(room, m.Text)

	// Redirect to message (this is kind of a hack so we return the right JSON
	// to the clients connected over websockets).
	http.Redirect(w, r, "/m/"+hash, http.StatusFound)
}
Ejemplo n.º 4
0
// FormHandler presents a form for creating a new room
func FormHandler(w http.ResponseWriter, r *http.Request) {
	u, _ := auth.GetAuthenticatedUser(r)

	users, err := userRepo.List(100)
	render.Check(err, w)

	render.Render(w, r, "room_form", map[string]interface{}{
		"request": r,
		"users":   users,
		"user":    u,
	})
}
Ejemplo n.º 5
0
// SavePathHandler saves a path for a board
func SavePathHandler(w http.ResponseWriter, r *http.Request) {
	u, _ := auth.GetAuthenticatedUser(r)

	hash := crypto.UniqueHash("")
	board := r.FormValue("board")
	data := r.FormValue("data")

	path := &Path{Hash: hash, Board: board, Data: data, User: u.Key}
	err := pathRepo.Save(path)
	render.Check(err, w)

	http.Redirect(w, r, "/b/"+board, http.StatusFound)
}
Ejemplo n.º 6
0
// LeaveHandler allows people to leave rooms
func LeaveHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	hash := vars["hash"]

	user, _ := auth.GetAuthenticatedUser(r)

	membership, err := roomMemberRepo.Load(hash, user.Key)
	render.Check(err, w)

	err = roomMemberRepo.Delete(membership.Hash)
	render.Check(err, w)

	http.Redirect(w, r, "/", 302)
}
Ejemplo n.º 7
0
// JoinHandler allows people to join rooms
func JoinHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	hash := vars["hash"]

	user, _ := auth.GetAuthenticatedUser(r)

	room, err := repo.Load(hash)
	render.Check(err, w)

	err = JoinRoom(room.Hash, user.Key)
	render.Check(err, w)

	http.Redirect(w, r, "/r/"+room.Hash, 302)
}
Ejemplo n.º 8
0
// SaveHandler saves a item
func SaveHandler(w http.ResponseWriter, r *http.Request) {
	user, err := auth.GetAuthenticatedUser(r)
	if err != nil {
		http.Redirect(w, r, "/login", http.StatusFound)
		return
	}

	token := r.FormValue("token")
	platform := r.FormValue("platform")

	d := &Token{Token: token, Platform: platform, User: user.Key}
	err = repo.Insert(d)
	render.Check(err, w)

	http.Redirect(w, r, "/", http.StatusFound)
}
Ejemplo n.º 9
0
// EditHandler handles editing of rooms
func EditHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	hash := vars["hash"]

	u, _ := auth.GetAuthenticatedUser(r)

	users, err := userRepo.List(100)
	render.Check(err, w)

	room, err := repo.Load(hash)
	render.Check(err, w)

	members, err := roomMemberRepo.ListMembers(room.Hash)
	render.Check(err, w)

	render.Render(w, r, "room_form", map[string]interface{}{
		"request": r,
		"room":    room,
		"users":   users,
		"members": members,
		"user":    u,
	})
}
Ejemplo n.º 10
0
func oneOnOneHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	hash := vars["hash"]

	user1, _ := auth.GetAuthenticatedUser(r)
	user2, err := authRepo.Get(hash)
	render.Check(err, w)

	// Create a one-on-one room between the logged in user and the clicked on
	// user if the room doesn't already exist.
	room, err := roomRepo.LoadOneOnOne(user1.Key, user2.Key)
	if err != nil {
		roomHash := rooms.GenerateOneOnOneHash(user1.Key, user2.Key)
		room = &rooms.Room{Hash: roomHash, Name: roomHash, Kind: rooms.OneOnOne, Folder: "", Created: time.Now()}
		err = roomRepo.Save(room)
		render.Check(err, w)

		rooms.JoinRoom(roomHash, user1.Key)
		rooms.JoinRoom(roomHash, user2.Key)
	}

	render.Redirect(w, r, "/r/"+room.Hash)
}