コード例 #1
0
ファイル: threads.go プロジェクト: wujiang/chatable
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
ファイル: envelopes.go プロジェクト: wujiang/chatable
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
ファイル: users.go プロジェクト: wujiang/chatable
func serveCreateUser(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	if err := r.ParseForm(); err != nil {
		return chatable.NewServerError(err.Error())
	}
	valid := usersForm.Valid(r.PostForm)
	if !valid {
		return usersForm.ConsolidateErrors()
	}
	u := chatable.NewUser(usersForm.Values["first_name"].(string),
		usersForm.Values["last_name"].(string),
		usersForm.Values["username"].(string),
		usersForm.Values["password"].(string),
		usersForm.Values["email"].(string),
		usersForm.Values["phone"].(string),
		r.RemoteAddr)
	err := store.UserStore.Create(u)
	// TODO: refine the error message
	if err != nil {
		msg := err.Error()
		if strings.Contains(msg, "violates") {
			return chatable.NewUserError("Some fileds are not unique")
		}
		return chatable.NewServerError(msg)
	}
	// create a auth token
	token, err := createNewAuthToken(w, r, u)
	if err != nil {
		return chatable.NewServerError(err.Error())
	}
	data := []chatable.UserWithToken{
		chatable.UserWithToken{
			FirstName:   u.FirstName,
			LastName:    u.LastName,
			Username:    u.Username,
			Email:       u.Email,
			PhoneNumber: u.PhoneNumber,
			Token:       *token,
		},
	}
	return writeJSON(w, chatable.NewJSONResult(data, 1))
}
コード例 #4
0
ファイル: auth_token.go プロジェクト: wujiang/chatable
func serveCreateAuthToken(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	if err := r.ParseForm(); err != nil {
		return chatable.NewServerError(err.Error())
	}
	valid := authTokenForm.Valid(r.PostForm)
	if !valid {
		return authTokenForm.ConsolidateErrors()
	}
	u, err := store.UserStore.GetByUsername(authTokenForm.Values["username"].(string))
	if err != nil {
		return auth.ErrUnauthenticated
	}
	if !chatable.CompareHash(u.Password, authTokenForm.Values["password"].(string)) {
		return auth.ErrUnauthenticated
	}

	token, cerr := createNewAuthToken(w, r, u)
	if cerr != nil {
		return cerr
	}
	return writeJSON(w, chatable.NewJSONResult([]*chatable.PublicToken{token}, 1))
}
コード例 #5
0
ファイル: auth_token.go プロジェクト: wujiang/chatable
func serveDeactivateAuthToken(w http.ResponseWriter, r *http.Request) chatable.CompoundError {
	if err := auth.TokenUnAuthenticate(w, r); err != nil {
		return err
	}
	return writeJSON(w, chatable.NewJSONResult([]*chatable.PublicToken{}, 1))
}