Files
mattermost/app/preference.go
Giorgos Christos Dimitriou 2e79ae9636 Change the implementation of GetAll() in preference_store.go to ret… (#10926)
* Change the implementation of GetAll() in `preference_store.go` to return an
object from `model` and a *model.AppError.
Change the Interface in `store.go` to accomodate for the change
Change the test that called the GetAll() function

* Rename the result variable to preferences so it makes more sense.
Use assertions to keep the test consistent

* Generate the correct mocks after the code changes

* Remove redundant conditions

* Address govet errors

* Resolve conflicts with master's new changes

* Fix Save() function according to the new changes in master (Got
overwritten with the previous commit)

* Change the assertions to have the same format as commit 2d97f01
2019-06-03 07:25:04 -04:00

85 lines
2.7 KiB
Go

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"net/http"
"github.com/mattermost/mattermost-server/model"
)
func (a *App) GetPreferencesForUser(userId string) (model.Preferences, *model.AppError) {
preferences, err := a.Srv.Store.Preference().GetAll(userId)
if err != nil {
err.StatusCode = http.StatusBadRequest
return nil, err
}
return preferences, nil
}
func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) {
preferences, err := a.Srv.Store.Preference().GetCategory(userId, category)
if err != nil {
err.StatusCode = http.StatusBadRequest
return nil, err
}
if len(preferences) == 0 {
err := model.NewAppError("getPreferenceCategory", "api.preference.preferences_category.get.app_error", nil, "", http.StatusNotFound)
return nil, err
}
return preferences, nil
}
func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) {
res, err := a.Srv.Store.Preference().Get(userId, category, preferenceName)
if err != nil {
err.StatusCode = http.StatusBadRequest
return nil, err
}
return res, nil
}
func (a *App) UpdatePreferences(userId string, preferences model.Preferences) *model.AppError {
for _, preference := range preferences {
if userId != preference.UserId {
return model.NewAppError("savePreferences", "api.preference.update_preferences.set.app_error", nil,
"userId="+userId+", preference.UserId="+preference.UserId, http.StatusForbidden)
}
}
if err := a.Srv.Store.Preference().Save(&preferences); err != nil {
err.StatusCode = http.StatusBadRequest
return err
}
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCES_CHANGED, "", "", userId, nil)
message.Add("preferences", preferences.ToJson())
a.Publish(message)
return nil
}
func (a *App) DeletePreferences(userId string, preferences model.Preferences) *model.AppError {
for _, preference := range preferences {
if userId != preference.UserId {
err := model.NewAppError("deletePreferences", "api.preference.delete_preferences.delete.app_error", nil,
"userId="+userId+", preference.UserId="+preference.UserId, http.StatusForbidden)
return err
}
}
for _, preference := range preferences {
if result := <-a.Srv.Store.Preference().Delete(userId, preference.Category, preference.Name); result.Err != nil {
result.Err.StatusCode = http.StatusBadRequest
return result.Err
}
}
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCES_DELETED, "", "", userId, nil)
message.Add("preferences", preferences.ToJson())
a.Publish(message)
return nil
}