diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2015-10-01 15:22:04 -0400 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2015-10-13 09:42:23 -0400 |
commit | a087403e9f25373d5bdea5e10fafb0c5d496a703 (patch) | |
tree | 0bf290c50fea2398da8beee262ff5ed6ec3c38bc | |
parent | da66599fa39ddbff96b0844fabac161e130a2bc4 (diff) | |
download | chat-a087403e9f25373d5bdea5e10fafb0c5d496a703.tar.gz chat-a087403e9f25373d5bdea5e10fafb0c5d496a703.tar.bz2 chat-a087403e9f25373d5bdea5e10fafb0c5d496a703.zip |
Added api to get and set preferences
-rw-r--r-- | api/api.go | 1 | ||||
-rw-r--r-- | api/preference.go | 63 | ||||
-rw-r--r-- | api/preference_test.go | 176 | ||||
-rw-r--r-- | model/client.go | 18 | ||||
-rw-r--r-- | model/preference.go | 20 |
5 files changed, 278 insertions, 0 deletions
diff --git a/api/api.go b/api/api.go index 5c3c0d8c6..4da1de62d 100644 --- a/api/api.go +++ b/api/api.go @@ -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) diff --git a/api/preference.go b/api/preference.go new file mode 100644 index 000000000..9fadfd2e4 --- /dev/null +++ b/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)))) + } +} diff --git a/api/preference_test.go b/api/preference_test.go new file mode 100644 index 000000000..63aaeaf3e --- /dev/null +++ b/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") + } +} diff --git a/model/client.go b/model/client.go index 11beb9a87..892d3e979 100644 --- a/model/client.go +++ b/model/client.go @@ -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 diff --git a/model/preference.go b/model/preference.go index f363434e3..8e2ff14bc 100644 --- a/model/preference.go +++ b/model/preference.go @@ -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) |