mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Cleaned up user_settings_theme.jsx and import_theme_modal.jsx * Made ImportThemeModal use a callback to return the theme to the user settings modal instead of saving it directly * Moved user theme from model to preferences * Added serverside API to delete preferences TODO update package with client stuff * Changed constants.jsx so that Preferences and ActionTypes can be imported on their own * Updated ThemeProps migration code to properly rename solarized code themes * Fixed warnings thrown by AppDispatcher * Added clientside UI to support team-specific themes * Removed debugging code from test * Fixed setting a user's theme when they haven't set their theme before
112 lines
3.6 KiB
Go
112 lines
3.6 KiB
Go
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package api
|
|
|
|
import (
|
|
l4g "github.com/alecthomas/log4go"
|
|
"github.com/gorilla/mux"
|
|
"github.com/mattermost/platform/model"
|
|
"github.com/mattermost/platform/utils"
|
|
"net/http"
|
|
)
|
|
|
|
func InitPreference() {
|
|
l4g.Debug(utils.T("api.preference.init.debug"))
|
|
|
|
BaseRoutes.Preferences.Handle("/", ApiUserRequired(getAllPreferences)).Methods("GET")
|
|
BaseRoutes.Preferences.Handle("/save", ApiUserRequired(savePreferences)).Methods("POST")
|
|
BaseRoutes.Preferences.Handle("/delete", ApiUserRequired(deletePreferences)).Methods("POST")
|
|
BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}", ApiUserRequired(getPreferenceCategory)).Methods("GET")
|
|
BaseRoutes.Preferences.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiUserRequired(getPreference)).Methods("GET")
|
|
}
|
|
|
|
func getAllPreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
if result := <-Srv.Store.Preference().GetAll(c.Session.UserId); result.Err != nil {
|
|
c.Err = result.Err
|
|
} else {
|
|
data := result.Data.(model.Preferences)
|
|
|
|
w.Write([]byte(data.ToJson()))
|
|
}
|
|
}
|
|
|
|
func savePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
preferences, err := model.PreferencesFromJson(r.Body)
|
|
if err != nil {
|
|
c.Err = model.NewLocAppError("savePreferences", "api.preference.save_preferences.decode.app_error", nil, err.Error())
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
return
|
|
}
|
|
|
|
for _, preference := range preferences {
|
|
if c.Session.UserId != preference.UserId {
|
|
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}))
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
}
|
|
|
|
w.Write([]byte("true"))
|
|
}
|
|
|
|
func getPreferenceCategory(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
category := params["category"]
|
|
|
|
if result := <-Srv.Store.Preference().GetCategory(c.Session.UserId, category); result.Err != nil {
|
|
c.Err = result.Err
|
|
} else {
|
|
data := result.Data.(model.Preferences)
|
|
|
|
w.Write([]byte(data.ToJson()))
|
|
}
|
|
}
|
|
|
|
func getPreference(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
params := mux.Vars(r)
|
|
category := params["category"]
|
|
name := params["name"]
|
|
|
|
if result := <-Srv.Store.Preference().Get(c.Session.UserId, category, name); result.Err != nil {
|
|
c.Err = result.Err
|
|
} else {
|
|
data := result.Data.(model.Preference)
|
|
w.Write([]byte(data.ToJson()))
|
|
}
|
|
}
|
|
|
|
func deletePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
preferences, err := model.PreferencesFromJson(r.Body)
|
|
if err != nil {
|
|
c.Err = model.NewLocAppError("savePreferences", "api.preference.delete_preferences.decode.app_error", nil, err.Error())
|
|
c.Err.StatusCode = http.StatusBadRequest
|
|
return
|
|
}
|
|
|
|
for _, preference := range preferences {
|
|
if c.Session.UserId != preference.UserId {
|
|
c.Err = model.NewLocAppError("deletePreferences", "api.preference.delete_preferences.user_id.app_error",
|
|
nil, "session.user_id="+c.Session.UserId+",preference.user_id="+preference.UserId)
|
|
c.Err.StatusCode = http.StatusForbidden
|
|
return
|
|
}
|
|
}
|
|
|
|
for _, preference := range preferences {
|
|
if result := <-Srv.Store.Preference().Delete(c.Session.UserId, preference.Category, preference.Name); result.Err != nil {
|
|
c.Err = result.Err
|
|
return
|
|
}
|
|
}
|
|
|
|
ReturnStatusOK(w)
|
|
}
|