mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Made further changes based on feedback
This commit is contained in:
@@ -14,9 +14,9 @@ func InitPreference(r *mux.Router) {
|
||||
l4g.Debug("Initializing preference api routes")
|
||||
|
||||
sr := r.PathPrefix("/preferences").Subrouter()
|
||||
sr.Handle("/save", ApiAppHandler(savePreferences)).Methods("POST")
|
||||
sr.Handle("/{category:[A-Za-z0-9_]+}", ApiAppHandler(getPreferenceCategory)).Methods("GET")
|
||||
sr.Handle("/{category:[A-Za-z0-9_]+}/{name:[A-Za-z0-9_]+}", ApiAppHandler(getPreference)).Methods("GET")
|
||||
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")
|
||||
}
|
||||
|
||||
func savePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -52,17 +52,21 @@ func getPreferenceCategory(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
data := result.Data.(model.Preferences)
|
||||
|
||||
if len(data) == 0 {
|
||||
if category == model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW {
|
||||
// add direct channels for a user that existed before preferences were added
|
||||
data = addDirectChannels(c.Session.UserId, c.Session.TeamId)
|
||||
}
|
||||
}
|
||||
data = transformPreferences(c, data, category)
|
||||
|
||||
w.Write([]byte(data.ToJson()))
|
||||
}
|
||||
}
|
||||
|
||||
func transformPreferences(c *Context, preferences model.Preferences, category string) model.Preferences {
|
||||
if len(preferences) == 0 && category == model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW {
|
||||
// add direct channels for a user that existed before preferences were added
|
||||
preferences = addDirectChannels(c.Session.UserId, c.Session.TeamId)
|
||||
}
|
||||
|
||||
return preferences
|
||||
}
|
||||
|
||||
func addDirectChannels(userId, teamId string) model.Preferences {
|
||||
var profiles map[string]*model.User
|
||||
if result := <-Srv.Store.User().GetProfiles(teamId); result.Err != nil {
|
||||
|
||||
@@ -71,13 +71,13 @@ func TestGetPreferenceCategory(t *testing.T) {
|
||||
user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
|
||||
store.Must(Srv.Store.User().VerifyEmail(user2.Id))
|
||||
|
||||
category := model.PREFERENCE_CATEGORY_TEST
|
||||
category := model.NewId()
|
||||
|
||||
preferences1 := model.Preferences{
|
||||
{
|
||||
UserId: user1.Id,
|
||||
Category: category,
|
||||
Name: model.PREFERENCE_NAME_TEST,
|
||||
Name: model.NewId(),
|
||||
},
|
||||
{
|
||||
UserId: user1.Id,
|
||||
@@ -86,8 +86,8 @@ func TestGetPreferenceCategory(t *testing.T) {
|
||||
},
|
||||
{
|
||||
UserId: user1.Id,
|
||||
Category: model.PREFERENCE_CATEGORY_TEST,
|
||||
Name: model.PREFERENCE_NAME_TEST,
|
||||
Category: model.NewId(),
|
||||
Name: model.NewId(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestGetPreferenceCategory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDefaultDirectChannels(t *testing.T) {
|
||||
func TestTransformPreferences(t *testing.T) {
|
||||
Setup()
|
||||
|
||||
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
|
||||
|
||||
Reference in New Issue
Block a user