github.com/levb/mattermost-server@v5.3.1+incompatible/app/preference.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func (a *App) GetPreferencesForUser(userId string) (model.Preferences, *model.AppError) {
    13  	if result := <-a.Srv.Store.Preference().GetAll(userId); result.Err != nil {
    14  		result.Err.StatusCode = http.StatusBadRequest
    15  		return nil, result.Err
    16  	} else {
    17  		return result.Data.(model.Preferences), nil
    18  	}
    19  }
    20  
    21  func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) {
    22  	if result := <-a.Srv.Store.Preference().GetCategory(userId, category); result.Err != nil {
    23  		result.Err.StatusCode = http.StatusBadRequest
    24  		return nil, result.Err
    25  	} else if len(result.Data.(model.Preferences)) == 0 {
    26  		err := model.NewAppError("getPreferenceCategory", "api.preference.preferences_category.get.app_error", nil, "", http.StatusNotFound)
    27  		return nil, err
    28  	} else {
    29  		return result.Data.(model.Preferences), nil
    30  	}
    31  }
    32  
    33  func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) {
    34  	if result := <-a.Srv.Store.Preference().Get(userId, category, preferenceName); result.Err != nil {
    35  		result.Err.StatusCode = http.StatusBadRequest
    36  		return nil, result.Err
    37  	} else {
    38  		data := result.Data.(model.Preference)
    39  		return &data, nil
    40  	}
    41  }
    42  
    43  func (a *App) UpdatePreferences(userId string, preferences model.Preferences) *model.AppError {
    44  	for _, preference := range preferences {
    45  		if userId != preference.UserId {
    46  			return model.NewAppError("savePreferences", "api.preference.update_preferences.set.app_error", nil,
    47  				"userId="+userId+", preference.UserId="+preference.UserId, http.StatusForbidden)
    48  		}
    49  	}
    50  
    51  	if result := <-a.Srv.Store.Preference().Save(&preferences); result.Err != nil {
    52  		result.Err.StatusCode = http.StatusBadRequest
    53  		return result.Err
    54  	}
    55  
    56  	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCES_CHANGED, "", "", userId, nil)
    57  	message.Add("preferences", preferences.ToJson())
    58  	a.Publish(message)
    59  
    60  	return nil
    61  }
    62  
    63  func (a *App) DeletePreferences(userId string, preferences model.Preferences) *model.AppError {
    64  	for _, preference := range preferences {
    65  		if userId != preference.UserId {
    66  			err := model.NewAppError("deletePreferences", "api.preference.delete_preferences.delete.app_error", nil,
    67  				"userId="+userId+", preference.UserId="+preference.UserId, http.StatusForbidden)
    68  			return err
    69  		}
    70  	}
    71  
    72  	for _, preference := range preferences {
    73  		if result := <-a.Srv.Store.Preference().Delete(userId, preference.Category, preference.Name); result.Err != nil {
    74  			result.Err.StatusCode = http.StatusBadRequest
    75  			return result.Err
    76  		}
    77  	}
    78  
    79  	message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_PREFERENCES_DELETED, "", "", userId, nil)
    80  	message.Add("preferences", preferences.ToJson())
    81  	a.Publish(message)
    82  
    83  	return nil
    84  }