示例#1
0
func serveGetThreads(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	params := r.URL.Query()
	pg := params.Get("page")
	page, err := strconv.Atoi(pg)
	if err != nil || page < 1 {
		page = 1
	}

	offset := page - 1
	activeUser := auth.ActiveUser(r)
	// this should never happen
	if activeUser == nil {
		return auth.ErrUnauthenticated
	}

	threads, err := store.ThreadStore.GetByUserID(activeUser.ID, offset)
	if err != nil {
		return chatable.NewServerError(err.Error())
	}
	var pubThreads []*chatable.PublicThread
	for _, th := range threads {
		pubThreads = append(pubThreads, th.ToPublic())
	}
	return writeJSON(w, chatable.NewJSONResult(pubThreads, page))
}
示例#2
0
func serveGetEnvelopes(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	withUsername := mux.Vars(r)["username"]
	params := r.URL.Query()
	pg := params.Get("page")
	page, err := strconv.Atoi(pg)
	if err != nil || page < 1 {
		page = 1
	}

	offset := page - 1
	activeUser := auth.ActiveUser(r)
	// this should never happen
	if activeUser == nil {
		return auth.ErrUnauthenticated
	}
	withUser, err := store.UserStore.GetByUsername(withUsername)
	if err == sql.ErrNoRows {
		return chatable.NewUserError("unknown username")
	} else if err != nil {
		return chatable.NewServerError(err.Error())
	}
	envelopes, err := store.EnvelopeStore.GetByUserIDWithUserID(
		activeUser.ID, withUser.ID, offset)
	if err != nil {
		return chatable.NewServerError(err.Error())
	}
	var pubEnv []*chatable.PublicEnvelope
	for _, env := range envelopes {
		pubEnv = append(pubEnv, env.ToPublic(store.UserStore))
	}

	return writeJSON(w, chatable.NewJSONResult(pubEnv, page))
}
示例#3
0
func serveWSConnect(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		return chatable.NewServerError(err.Error())
	}
	activeUser := auth.ActiveUser(r)
	// this should never happen
	if activeUser == nil {
		return auth.ErrUnauthenticated
	}
	c := &connection{
		conn:   ws,
		uname:  activeUser.Username,
		uid:    activeUser.ID,
		outbuf: make(chan chatable.PublicEnvelope),
	}

	Hub.register <- c
	go c.write()
	c.read()

	return nil
}