Added api to get and set preferences

Этот коммит содержится в:
hmhealey
2015-10-01 15:22:04 -04:00
родитель da66599fa3
Коммит a087403e9f
5 изменённых файлов: 278 добавлений и 0 удалений

Просмотреть файл

@@ -45,6 +45,7 @@ func InitApi() {
InitAdmin(r)
InitOAuth(r)
InitWebhook(r)
InitPreference(r)
templatesDir := utils.FindDir("api/templates")
l4g.Debug("Parsing server templates at %v", templatesDir)

63
api/preference.go Обычный файл
Просмотреть файл

@@ -0,0 +1,63 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
l4g "code.google.com/p/log4go"
"encoding/json"
"github.com/gorilla/mux"
"github.com/mattermost/platform/model"
"net/http"
)
func InitPreference(r *mux.Router) {
l4g.Debug("Initializing preference api routes")
sr := r.PathPrefix("/preferences").Subrouter()
sr.Handle("/set", ApiAppHandler(setPreferences)).Methods("POST")
sr.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiAppHandler(getPreferencesByName)).Methods("GET")
}
func setPreferences(c *Context, w http.ResponseWriter, r *http.Request) {
var preferences []model.Preference
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&preferences); err != nil {
c.Err = model.NewAppError("setPreferences", "Unable to decode preferences from request", err.Error())
c.Err.StatusCode = http.StatusBadRequest
return
}
// just attempt to save/update them one by one and abort if one fails
// in the future, this could probably be done in a transaction, but that's unnecessary now
for _, preference := range preferences {
if c.Session.UserId != preference.UserId {
c.Err = model.NewAppError("setPreferences", "Unable to set preferences for other user", "session.user_id="+c.Session.UserId+", preference.user_id="+preference.UserId)
c.Err.StatusCode = http.StatusUnauthorized
return
}
if result := <-Srv.Store.Preference().Save(&preference); result.Err != nil {
if result = <-Srv.Store.Preference().Update(&preference); result.Err != nil {
c.Err = result.Err
return
}
}
}
w.Write([]byte("true"))
}
func getPreferencesByName(c *Context, w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
category := params["category"]
name := params["name"]
if result := <-Srv.Store.Preference().GetByName(c.Session.UserId, category, name); result.Err != nil {
c.Err = result.Err
return
} else {
w.Write([]byte(model.PreferenceListToJson(result.Data.([]*model.Preference))))
}
}

176
api/preference_test.go Обычный файл
Просмотреть файл

@@ -0,0 +1,176 @@
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
package api
import (
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"testing"
)
func TestSetPreferences(t *testing.T) {
Setup()
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
Client.LoginByEmail(team.Name, user1.Email, "pwd")
// save 10 preferences
var preferences []*model.Preference
for i := 0; i < 10; i++ {
preference := model.Preference{
UserId: user1.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
}
preferences = append(preferences, &preference)
}
if _, err := Client.SetPreferences(preferences); err != nil {
t.Fatal(err)
}
// update 10 preferences
for _, preference := range preferences {
preference.Value = "1234garbage"
}
if _, err := Client.SetPreferences(preferences); err != nil {
t.Fatal(err)
}
// not able to update as a different user
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
if _, err := Client.SetPreferences(preferences); err == nil {
t.Fatal("shouldn't have been able to update another user's preferences")
}
}
func TestGetPreferencesByName(t *testing.T) {
Setup()
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
store.Must(Srv.Store.User().VerifyEmail(user1.Id))
user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
preferences1 := []*model.Preference{
&model.Preference{
UserId: user1.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
},
&model.Preference{
UserId: user1.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
},
&model.Preference{
UserId: user1.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_TEST,
AltId: model.NewId(),
},
&model.Preference{
UserId: user1.Id,
Category: model.PREFERENCE_CATEGORY_TEST,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
},
}
preferences2 := []*model.Preference{
&model.Preference{
UserId: user2.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
},
}
Client.LoginByEmail(team.Name, user1.Email, "pwd")
Client.Must(Client.SetPreferences(preferences1))
Client.LoginByEmail(team.Name, user2.Email, "pwd")
Client.Must(Client.SetPreferences(preferences2))
Client.LoginByEmail(team.Name, user1.Email, "pwd")
if result, err := Client.GetPreferencesByName(model.PREFERENCE_CATEGORY_DIRECT_CHANNELS, model.PREFERENCE_NAME_SHOWHIDE); err != nil {
t.Fatal(err)
} else if data := result.Data.([]*model.Preference); len(data) != 2 {
t.Fatal("received the wrong number of preferences")
} else if !((*data[0] == *preferences1[0] && *data[1] == *preferences1[1]) || (*data[0] == *preferences1[1] && *data[1] == *preferences1[0])) {
t.Fatal("received incorrect preferences")
}
Client.LoginByEmail(team.Name, user2.Email, "pwd")
if result, err := Client.GetPreferencesByName(model.PREFERENCE_CATEGORY_DIRECT_CHANNELS, model.PREFERENCE_NAME_SHOWHIDE); err != nil {
t.Fatal(err)
} else if data := result.Data.([]*model.Preference); len(data) != 1 {
t.Fatal("received the wrong number of preferences")
} else if *data[0] != *preferences2[0] {
t.Fatal("received incorrect preference")
}
}
func TestSetAndGetProperties(t *testing.T) {
Setup()
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"}
user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
store.Must(Srv.Store.User().VerifyEmail(user.Id))
Client.LoginByEmail(team.Name, user.Email, "pwd")
p := model.Preference{
UserId: user.Id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOWHIDE,
AltId: model.NewId(),
Value: model.NewId(),
}
Client.Must(Client.SetPreferences([]*model.Preference{&p}))
if result, err := Client.GetPreferencesByName(p.Category, p.Name); err != nil {
t.Fatal(err)
} else if data := result.Data.([]*model.Preference); len(data) != 1 {
t.Fatal("received too many preferences")
} else if *data[0] != p {
t.Fatal("preference saved incorrectly")
}
p.Value = model.NewId()
Client.Must(Client.SetPreferences([]*model.Preference{&p}))
if result, err := Client.GetPreferencesByName(p.Category, p.Name); err != nil {
t.Fatal(err)
} else if data := result.Data.([]*model.Preference); len(data) != 1 {
t.Fatal("received too many preferences")
} else if *data[0] != p {
t.Fatal("preference updated incorrectly")
}
}

Просмотреть файл

@@ -844,6 +844,24 @@ func (c *Client) ListIncomingWebhooks() (*Result, *AppError) {
}
}
func (c *Client) SetPreferences(preferences []*Preference) (*Result, *AppError) {
if r, err := c.DoApiPost("/preferences/set", PreferenceListToJson(preferences)); err != nil {
return nil, err
} else {
return &Result{r.Header.Get(HEADER_REQUEST_ID),
r.Header.Get(HEADER_ETAG_SERVER), nil}, nil
}
}
func (c *Client) GetPreferencesByName(category string, name string) (*Result, *AppError) {
if r, err := c.DoApiGet("/preferences/"+category+"/"+name, "", ""); err != nil {
return nil, err
} else {
return &Result{r.Header.Get(HEADER_REQUEST_ID),
r.Header.Get(HEADER_ETAG_SERVER), PreferenceListFromJson(r.Body)}, nil
}
}
func (c *Client) MockSession(sessionToken string) {
c.AuthToken = sessionToken
c.AuthType = HEADER_BEARER

Просмотреть файл

@@ -32,6 +32,15 @@ func (o *Preference) ToJson() string {
}
}
func PreferenceListToJson(o []*Preference) string {
b, err := json.Marshal(o)
if err != nil {
return ""
} else {
return string(b)
}
}
func PreferenceFromJson(data io.Reader) *Preference {
decoder := json.NewDecoder(data)
var o Preference
@@ -43,6 +52,17 @@ func PreferenceFromJson(data io.Reader) *Preference {
}
}
func PreferenceListFromJson(data io.Reader) []*Preference {
decoder := json.NewDecoder(data)
var o []*Preference
err := decoder.Decode(&o)
if err == nil {
return o
} else {
return nil
}
}
func (o *Preference) IsValid() *AppError {
if len(o.UserId) != 26 {
return NewAppError("Preference.IsValid", "Invalid user id", "user_id="+o.UserId)