2015-12-10 14:33:21 -08:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-10-01 15:22:04 -04:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2016-01-11 09:12:51 -06:00
|
|
|
l4g "github.com/alecthomas/log4go"
|
2015-10-01 15:22:04 -04:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/mattermost/platform/model"
|
2016-01-22 01:37:11 -03:00
|
|
|
"github.com/mattermost/platform/utils"
|
2015-10-01 15:22:04 -04:00
|
|
|
"net/http"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InitPreference(r *mux.Router) {
|
2016-01-22 01:37:11 -03:00
|
|
|
l4g.Debug(utils.T("api.preference.init.debug"))
|
2015-10-01 15:22:04 -04:00
|
|
|
|
|
|
|
|
sr := r.PathPrefix("/preferences").Subrouter()
|
2015-10-15 15:09:40 -04:00
|
|
|
sr.Handle("/", ApiUserRequired(getAllPreferences)).Methods("GET")
|
2015-10-13 15:18:01 -04:00
|
|
|
sr.Handle("/save", ApiUserRequired(savePreferences)).Methods("POST")
|
|
|
|
|
sr.Handle("/{category:[A-Za-z0-9_]+}", ApiUserRequired(getPreferenceCategory)).Methods("GET")
|
|
|
|
|
sr.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiUserRequired(getPreference)).Methods("GET")
|
2015-10-01 15:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
2015-10-15 15:09:40 -04:00
|
|
|
func getAllPreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
2016-01-20 13:36:16 -06:00
|
|
|
if result := <-Srv.Store.Preference().GetAll(c.Session.UserId); result.Err != nil {
|
2015-10-15 15:09:40 -04:00
|
|
|
c.Err = result.Err
|
|
|
|
|
} else {
|
|
|
|
|
data := result.Data.(model.Preferences)
|
|
|
|
|
|
|
|
|
|
w.Write([]byte(data.ToJson()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-09 13:47:11 -04:00
|
|
|
func savePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
preferences, err := model.PreferencesFromJson(r.Body)
|
|
|
|
|
if err != nil {
|
2016-01-22 01:37:11 -03:00
|
|
|
c.Err = model.NewLocAppError("savePreferences", "api.preference.save_preferences.decode.app_error", nil, err.Error())
|
2015-10-01 15:22:04 -04:00
|
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, preference := range preferences {
|
|
|
|
|
if c.Session.UserId != preference.UserId {
|
2016-01-22 01:37:11 -03:00
|
|
|
c.Err = model.NewLocAppError("savePreferences", "api.preference.save_preferences.set.app_error", nil,
|
|
|
|
|
c.T("api.preference.save_preferences.set_details.app_error",
|
|
|
|
|
map[string]interface{}{"SessionUserId": c.Session.UserId, "PreferenceUserId": preference.UserId}))
|
2015-10-01 15:22:04 -04:00
|
|
|
c.Err.StatusCode = http.StatusUnauthorized
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-10-07 10:19:02 -04:00
|
|
|
}
|
2015-10-01 15:22:04 -04:00
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil {
|
2015-10-07 10:19:02 -04:00
|
|
|
c.Err = result.Err
|
|
|
|
|
return
|
2015-10-01 15:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write([]byte("true"))
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 11:52:17 -04:00
|
|
|
func getPreferenceCategory(c *Context, w http.ResponseWriter, r *http.Request) {
|
2015-10-01 15:22:04 -04:00
|
|
|
params := mux.Vars(r)
|
|
|
|
|
category := params["category"]
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
if result := <-Srv.Store.Preference().GetCategory(c.Session.UserId, category); result.Err != nil {
|
2015-10-01 15:22:04 -04:00
|
|
|
c.Err = result.Err
|
|
|
|
|
} else {
|
2015-10-09 13:47:11 -04:00
|
|
|
data := result.Data.(model.Preferences)
|
2015-10-05 12:45:15 -04:00
|
|
|
|
2015-10-09 13:47:11 -04:00
|
|
|
w.Write([]byte(data.ToJson()))
|
2015-10-01 15:22:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
2015-10-07 10:39:36 -04:00
|
|
|
|
2015-10-13 11:52:17 -04:00
|
|
|
func getPreference(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
|
category := params["category"]
|
|
|
|
|
name := params["name"]
|
|
|
|
|
|
2016-01-20 13:36:16 -06:00
|
|
|
if result := <-Srv.Store.Preference().Get(c.Session.UserId, category, name); result.Err != nil {
|
2015-10-13 11:52:17 -04:00
|
|
|
c.Err = result.Err
|
|
|
|
|
} else {
|
|
|
|
|
data := result.Data.(model.Preference)
|
|
|
|
|
w.Write([]byte(data.ToJson()))
|
|
|
|
|
}
|
|
|
|
|
}
|