From 4bc625e8d10ab7735b76814fe9bbf3fb9144d4e1 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 22 Sep 2015 12:00:34 -0400 Subject: Added 'default' option to channel notification settings that just uses the user's notification level --- api/channel.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index 896e22793..17322179d 100644 --- a/api/channel.go +++ b/api/channel.go @@ -76,7 +76,7 @@ func CreateChannel(c *Context, channel *model.Channel, addMember bool) (*model.C if addMember { cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, - Roles: model.CHANNEL_ROLE_ADMIN, NotifyLevel: model.CHANNEL_NOTIFY_ALL} + Roles: model.CHANNEL_ROLE_ADMIN, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -135,7 +135,7 @@ func CreateDirectChannel(c *Context, otherUserId string) (*model.Channel, *model return nil, err } else { cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, - Roles: "", NotifyLevel: model.CHANNEL_NOTIFY_ALL} + Roles: "", NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -372,7 +372,7 @@ func JoinChannel(c *Context, channelId string, role string) { } if channel.Type == model.CHANNEL_OPEN { - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, NotifyLevel: model.CHANNEL_NOTIFY_ALL, Roles: role} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: role} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { c.Err = cmresult.Err @@ -405,7 +405,7 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "town-square"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_ALL, Roles: channelRole} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: channelRole} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err } @@ -414,7 +414,7 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "off-topic"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_ALL, Roles: channelRole} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: channelRole} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err } @@ -694,7 +694,7 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } else { oUser := oresult.Data.(*model.User) - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyLevel: model.CHANNEL_NOTIFY_ALL} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { l4g.Error("Failed to add member user_id=%v channel_id=%v err=%v", userId, id, cmresult.Err) -- cgit v1.2.3-1-g7c22 From 7c319f052441c01f7c0ffd744a50b671c785591a Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 23 Sep 2015 11:38:08 -0400 Subject: Removed UI for quiet mode and added UI to set when a channel will be marked unread in the sidebar --- api/channel.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index 17322179d..b69fe6ea0 100644 --- a/api/channel.go +++ b/api/channel.go @@ -24,6 +24,7 @@ func InitChannel(r *mux.Router) { sr.Handle("/update", ApiUserRequired(updateChannel)).Methods("POST") sr.Handle("/update_desc", ApiUserRequired(updateChannelDesc)).Methods("POST") sr.Handle("/update_notify_level", ApiUserRequired(updateNotifyLevel)).Methods("POST") + sr.Handle("/update_mark_unread_level", ApiUserRequired(updateMarkUnreadLevel)).Methods("POST") sr.Handle("/{id:[A-Za-z0-9]+}/", ApiUserRequiredActivity(getChannel, false)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}/extra_info", ApiUserRequired(getChannelExtraInfo)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}/join", ApiUserRequired(joinChannel)).Methods("POST") @@ -75,8 +76,8 @@ func CreateChannel(c *Context, channel *model.Channel, addMember bool) (*model.C sc := result.Data.(*model.Channel) if addMember { - cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, - Roles: model.CHANNEL_ROLE_ADMIN, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} + cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, Roles: model.CHANNEL_ROLE_ADMIN, + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -134,8 +135,8 @@ func CreateDirectChannel(c *Context, otherUserId string) (*model.Channel, *model if sc, err := CreateChannel(c, channel, true); err != nil { return nil, err } else { - cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, - Roles: "", NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} + cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, Roles: "", + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -372,7 +373,8 @@ func JoinChannel(c *Context, channelId string, role string) { } if channel.Type == model.CHANNEL_OPEN { - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: role} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, Roles: role, + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { c.Err = cmresult.Err @@ -405,7 +407,9 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "town-square"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: channelRole} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err } @@ -414,7 +418,9 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "off-topic"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, Roles: channelRole} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err } @@ -694,7 +700,7 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } else { oUser := oresult.Data.(*model.User) - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { l4g.Error("Failed to add member user_id=%v channel_id=%v err=%v", userId, id, cmresult.Err) @@ -821,3 +827,41 @@ func updateNotifyLevel(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapToJson(data))) } + +func updateMarkUnreadLevel(c *Context, w http.ResponseWriter, r *http.Request) { + data := model.MapFromJson(r.Body) + userId := data["user_id"] + if len(userId) != 26 { + c.SetInvalidParam("updateMarkUnreadLevel", "user_id") + return + } + + channelId := data["channel_id"] + if len(channelId) != 26 { + c.SetInvalidParam("updateMarkUnreadLevel", "channel_id") + return + } + + markUnreadLevel := data["mark_unread_level"] + if len(markUnreadLevel) == 0 || !model.IsChannelMarkUnreadLevelValid(markUnreadLevel) { + c.SetInvalidParam("updateMarkUnreadLevel", "mark_unread_level") + return + } + + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId) + + if !c.HasPermissionsToUser(userId, "updateMarkUnreadLevel") { + return + } + + if !c.HasPermissionsToChannel(cchan, "updateMarkUnreadLevel") { + return + } + + if result := <-Srv.Store.Channel().UpdateMarkUnreadLevel(channelId, userId, markUnreadLevel); result.Err != nil { + c.Err = result.Err + return + } + + w.Write([]byte(model.MapToJson(data))) +} -- cgit v1.2.3-1-g7c22 From 9995c7ccfcb9ad0787439f476db55ac56fbecd04 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 23 Sep 2015 14:42:07 -0400 Subject: Added additional test cases and fixed existing ones for ChannelMember.MarkUnreadLevel --- api/channel_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'api') diff --git a/api/channel_test.go b/api/channel_test.go index 7845ac499..3276022fa 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -885,6 +885,88 @@ func TestUpdateNotifyLevel(t *testing.T) { } } +func TestUpdateMarkUnreadLevel(t *testing.T) { + Setup() + + team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} + team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team) + + user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} + user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User) + store.Must(Srv.Store.User().VerifyEmail(user.Id)) + + Client.LoginByEmail(team.Name, user.Email, "pwd") + + channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} + channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) + + data := make(map[string]string) + data["channel_id"] = channel1.Id + data["user_id"] = user.Id + data["mark_unread_level"] = model.CHANNEL_MARK_UNREAD_MENTION + + timeBeforeUpdate := model.GetMillis() + time.Sleep(100 * time.Millisecond) + + if _, err := Client.UpdateMarkUnreadLevel(data); err != nil { + t.Fatal(err) + } + + rget := Client.Must(Client.GetChannels("")) + rdata := rget.Data.(*model.ChannelList) + if len(rdata.Members) == 0 || rdata.Members[channel1.Id].MarkUnreadLevel != data["mark_unread_level"] { + t.Fatal("MarkUnreadLevel did not update properly") + } + + if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate { + t.Fatal("LastUpdateAt did not update") + } + + data["user_id"] = "junk" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - bad user id") + } + + data["user_id"] = "12345678901234567890123456" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - bad user id") + } + + data["user_id"] = user.Id + data["channel_id"] = "junk" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - bad channel id") + } + + data["channel_id"] = "12345678901234567890123456" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - bad channel id") + } + + data["channel_id"] = channel1.Id + data["mark_unread_level"] = "" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - empty notify level") + } + + data["mark_unread_level"] = "junk" + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - bad notify level") + } + + user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} + user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User) + + Client.LoginByEmail(team.Name, user2.Email, "pwd") + + data["channel_id"] = channel1.Id + data["user_id"] = user2.Id + data["mark_unread_level"] = model.CHANNEL_MARK_UNREAD_MENTION + if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + t.Fatal("Should have errored - user not in channel") + } +} + func TestFuzzyChannel(t *testing.T) { Setup() -- cgit v1.2.3-1-g7c22 From c16b9de8dc4924cf2fb243579284e67f55cf3a47 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 30 Sep 2015 11:08:36 -0400 Subject: Replaced ChannelMember.MarkUnreadLevel with ChannelMember.NotifyProps --- api/channel.go | 50 +++++++++++++++++++++------------ api/channel_test.go | 80 +++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 93 insertions(+), 37 deletions(-) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index b69fe6ea0..5d54cdc6f 100644 --- a/api/channel.go +++ b/api/channel.go @@ -24,7 +24,7 @@ func InitChannel(r *mux.Router) { sr.Handle("/update", ApiUserRequired(updateChannel)).Methods("POST") sr.Handle("/update_desc", ApiUserRequired(updateChannelDesc)).Methods("POST") sr.Handle("/update_notify_level", ApiUserRequired(updateNotifyLevel)).Methods("POST") - sr.Handle("/update_mark_unread_level", ApiUserRequired(updateMarkUnreadLevel)).Methods("POST") + sr.Handle("/update_notify_props", ApiUserRequired(updateNotifyProps)).Methods("POST") sr.Handle("/{id:[A-Za-z0-9]+}/", ApiUserRequiredActivity(getChannel, false)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}/extra_info", ApiUserRequired(getChannelExtraInfo)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}/join", ApiUserRequired(joinChannel)).Methods("POST") @@ -77,7 +77,7 @@ func CreateChannel(c *Context, channel *model.Channel, addMember bool) (*model.C if addMember { cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, Roles: model.CHANNEL_ROLE_ADMIN, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -136,7 +136,7 @@ func CreateDirectChannel(c *Context, otherUserId string) (*model.Channel, *model return nil, err } else { cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, Roles: "", - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -374,7 +374,7 @@ func JoinChannel(c *Context, channelId string, role string) { if channel.Type == model.CHANNEL_OPEN { cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, Roles: role, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { c.Err = cmresult.Err @@ -408,7 +408,7 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { err = result.Err } else { cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err @@ -419,7 +419,7 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { err = result.Err } else { cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err @@ -700,7 +700,8 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } else { oUser := oresult.Data.(*model.User) - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, MarkUnreadLevel: model.CHANNEL_MARK_UNREAD_ALL} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, + NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { l4g.Error("Failed to add member user_id=%v channel_id=%v err=%v", userId, id, cmresult.Err) @@ -790,6 +791,7 @@ func removeChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } +// TODO remove me func updateNotifyLevel(c *Context, w http.ResponseWriter, r *http.Request) { data := model.MapFromJson(r.Body) userId := data["user_id"] @@ -828,8 +830,9 @@ func updateNotifyLevel(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapToJson(data))) } -func updateMarkUnreadLevel(c *Context, w http.ResponseWriter, r *http.Request) { +func updateNotifyProps(c *Context, w http.ResponseWriter, r *http.Request) { data := model.MapFromJson(r.Body) + userId := data["user_id"] if len(userId) != 26 { c.SetInvalidParam("updateMarkUnreadLevel", "user_id") @@ -842,26 +845,39 @@ func updateMarkUnreadLevel(c *Context, w http.ResponseWriter, r *http.Request) { return } - markUnreadLevel := data["mark_unread_level"] - if len(markUnreadLevel) == 0 || !model.IsChannelMarkUnreadLevelValid(markUnreadLevel) { - c.SetInvalidParam("updateMarkUnreadLevel", "mark_unread_level") + cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId) + + if !c.HasPermissionsToUser(userId, "updateNotifyLevel") { return } - cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId) - - if !c.HasPermissionsToUser(userId, "updateMarkUnreadLevel") { + if !c.HasPermissionsToChannel(cchan, "updateNotifyLevel") { return } - if !c.HasPermissionsToChannel(cchan, "updateMarkUnreadLevel") { + result := <-Srv.Store.Channel().GetMember(channelId, userId) + if result.Err != nil { + c.Err = result.Err return } - if result := <-Srv.Store.Channel().UpdateMarkUnreadLevel(channelId, userId, markUnreadLevel); result.Err != nil { + member := result.Data.(model.ChannelMember) + + // update whichever notify properties have been provided, but don't change the others + if markUnread, exists := data["mark_unread"]; exists { + member.NotifyProps["mark_unread"] = markUnread + } + + if desktop, exists := data["desktop"]; exists { + member.NotifyProps["desktop"] = desktop + } + + if result := <-Srv.Store.Channel().UpdateMember(&member); result.Err != nil { c.Err = result.Err return + } else { + // return the updated notify properties including any unchanged ones + w.Write([]byte(model.MapToJson(member.NotifyProps))) } - w.Write([]byte(model.MapToJson(data))) } diff --git a/api/channel_test.go b/api/channel_test.go index 3276022fa..0e9dbbd41 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -885,7 +885,7 @@ func TestUpdateNotifyLevel(t *testing.T) { } } -func TestUpdateMarkUnreadLevel(t *testing.T) { +func TestUpdateNotifyProps(t *testing.T) { Setup() team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} @@ -903,55 +903,94 @@ func TestUpdateMarkUnreadLevel(t *testing.T) { data := make(map[string]string) data["channel_id"] = channel1.Id data["user_id"] = user.Id - data["mark_unread_level"] = model.CHANNEL_MARK_UNREAD_MENTION + data["desktop"] = model.CHANNEL_NOTIFY_MENTION timeBeforeUpdate := model.GetMillis() time.Sleep(100 * time.Millisecond) - if _, err := Client.UpdateMarkUnreadLevel(data); err != nil { + // test updating desktop + if result, err := Client.UpdateNotifyProps(data); err != nil { t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { + t.Fatal("NotifyProps[\"desktop\"] did not update properly") + } else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { + t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) } rget := Client.Must(Client.GetChannels("")) rdata := rget.Data.(*model.ChannelList) - if len(rdata.Members) == 0 || rdata.Members[channel1.Id].MarkUnreadLevel != data["mark_unread_level"] { - t.Fatal("MarkUnreadLevel did not update properly") + if len(rdata.Members) == 0 || rdata.Members[channel1.Id].NotifyProps["desktop"] != data["desktop"] { + t.Fatal("NotifyProps[\"desktop\"] did not update properly") + } else if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate { + t.Fatal("LastUpdateAt did not update") } - if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate { - t.Fatal("LastUpdateAt did not update") + // test an empty update + delete(data, "desktop") + + if result, err := Client.UpdateNotifyProps(data); err != nil { + t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_ALL { + t.Fatalf("NotifyProps[\"mark_unread\"] changed to %v", notifyProps["mark_unread"]) + } else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { + t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) + } + + // test updating mark unread + data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + + if result, err := Client.UpdateNotifyProps(data); err != nil { + t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { + t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") + } else if notifyProps["desktop"] != model.CHANNEL_NOTIFY_MENTION { + t.Fatalf("NotifyProps[\"desktop\"] changed to %v", notifyProps["desktop"]) } + // test updating both + data["desktop"] = model.CHANNEL_NOTIFY_NONE + data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + + if result, err := Client.UpdateNotifyProps(data); err != nil { + t.Fatal(err) + } else if notifyProps := result.Data.(map[string]string); notifyProps["desktop"] != model.CHANNEL_NOTIFY_NONE { + t.Fatal("NotifyProps[\"desktop\"] did not update properly") + } else if notifyProps["mark_unread"] != model.CHANNEL_MARK_UNREAD_MENTION { + t.Fatal("NotifyProps[\"mark_unread\"] did not update properly") + } + + // test error cases data["user_id"] = "junk" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad user id") } data["user_id"] = "12345678901234567890123456" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad user id") } data["user_id"] = user.Id data["channel_id"] = "junk" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad channel id") } data["channel_id"] = "12345678901234567890123456" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - bad channel id") } - data["channel_id"] = channel1.Id - data["mark_unread_level"] = "" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { - t.Fatal("Should have errored - empty notify level") + data["desktop"] = "junk" + data["mark_unread"] = model.CHANNEL_MARK_UNREAD_ALL + if _, err := Client.UpdateNotifyProps(data); err == nil { + t.Fatal("Should have errored - bad desktop notify level") } - data["mark_unread_level"] = "junk" - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { - t.Fatal("Should have errored - bad notify level") + data["desktop"] = model.CHANNEL_NOTIFY_ALL + data["mark_unread"] = "junk" + if _, err := Client.UpdateNotifyProps(data); err == nil { + t.Fatal("Should have errored - bad mark unread level") } user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} @@ -961,8 +1000,9 @@ func TestUpdateMarkUnreadLevel(t *testing.T) { data["channel_id"] = channel1.Id data["user_id"] = user2.Id - data["mark_unread_level"] = model.CHANNEL_MARK_UNREAD_MENTION - if _, err := Client.UpdateMarkUnreadLevel(data); err == nil { + data["desktop"] = model.CHANNEL_NOTIFY_MENTION + data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION + if _, err := Client.UpdateNotifyLevel(data); err == nil { t.Fatal("Should have errored - user not in channel") } } -- cgit v1.2.3-1-g7c22 From c9a0030551f289241407743fbd21080cd8a358a4 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 30 Sep 2015 12:28:28 -0400 Subject: Moved ChannelMember.NotifyLevel into ChannelMember.NotifyProps --- api/channel.go | 62 ++++++-------------------------- api/channel_benchmark_test.go | 11 +++--- api/channel_test.go | 84 +------------------------------------------ 3 files changed, 17 insertions(+), 140 deletions(-) (limited to 'api') diff --git a/api/channel.go b/api/channel.go index 5d54cdc6f..6494e3528 100644 --- a/api/channel.go +++ b/api/channel.go @@ -23,7 +23,6 @@ func InitChannel(r *mux.Router) { sr.Handle("/create_direct", ApiUserRequired(createDirectChannel)).Methods("POST") sr.Handle("/update", ApiUserRequired(updateChannel)).Methods("POST") sr.Handle("/update_desc", ApiUserRequired(updateChannelDesc)).Methods("POST") - sr.Handle("/update_notify_level", ApiUserRequired(updateNotifyLevel)).Methods("POST") sr.Handle("/update_notify_props", ApiUserRequired(updateNotifyProps)).Methods("POST") sr.Handle("/{id:[A-Za-z0-9]+}/", ApiUserRequiredActivity(getChannel, false)).Methods("GET") sr.Handle("/{id:[A-Za-z0-9]+}/extra_info", ApiUserRequired(getChannelExtraInfo)).Methods("GET") @@ -76,8 +75,8 @@ func CreateChannel(c *Context, channel *model.Channel, addMember bool) (*model.C sc := result.Data.(*model.Channel) if addMember { - cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, Roles: model.CHANNEL_ROLE_ADMIN, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: sc.Id, UserId: c.Session.UserId, + Roles: model.CHANNEL_ROLE_ADMIN, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -135,8 +134,7 @@ func CreateDirectChannel(c *Context, otherUserId string) (*model.Channel, *model if sc, err := CreateChannel(c, channel, true); err != nil { return nil, err } else { - cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, Roles: "", - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: sc.Id, UserId: otherUserId, Roles: "", NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { return nil, cmresult.Err @@ -373,8 +371,8 @@ func JoinChannel(c *Context, channelId string, role string) { } if channel.Type == model.CHANNEL_OPEN { - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, Roles: role, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: c.Session.UserId, + Roles: role, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { c.Err = cmresult.Err @@ -407,8 +405,8 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "town-square"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, + Roles: channelRole, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err @@ -418,8 +416,8 @@ func JoinDefaultChannels(user *model.User, channelRole string) *model.AppError { if result := <-Srv.Store.Channel().GetByName(user.TeamId, "off-topic"); result.Err != nil { err = result.Err } else { - cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, Roles: channelRole, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: result.Data.(*model.Channel).Id, UserId: user.Id, + Roles: channelRole, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmResult := <-Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil { err = cmResult.Err @@ -700,8 +698,7 @@ func addChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } else { oUser := oresult.Data.(*model.User) - cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, - NotifyLevel: model.CHANNEL_NOTIFY_DEFAULT, NotifyProps: model.GetDefaultChannelNotifyProps()} + cm := &model.ChannelMember{ChannelId: channel.Id, UserId: userId, NotifyProps: model.GetDefaultChannelNotifyProps()} if cmresult := <-Srv.Store.Channel().SaveMember(cm); cmresult.Err != nil { l4g.Error("Failed to add member user_id=%v channel_id=%v err=%v", userId, id, cmresult.Err) @@ -791,45 +788,6 @@ func removeChannelMember(c *Context, w http.ResponseWriter, r *http.Request) { } -// TODO remove me -func updateNotifyLevel(c *Context, w http.ResponseWriter, r *http.Request) { - data := model.MapFromJson(r.Body) - userId := data["user_id"] - if len(userId) != 26 { - c.SetInvalidParam("updateNotifyLevel", "user_id") - return - } - - channelId := data["channel_id"] - if len(channelId) != 26 { - c.SetInvalidParam("updateNotifyLevel", "channel_id") - return - } - - notifyLevel := data["notify_level"] - if len(notifyLevel) == 0 || !model.IsChannelNotifyLevelValid(notifyLevel) { - c.SetInvalidParam("updateNotifyLevel", "notify_level") - return - } - - cchan := Srv.Store.Channel().CheckPermissionsTo(c.Session.TeamId, channelId, c.Session.UserId) - - if !c.HasPermissionsToUser(userId, "updateNotifyLevel") { - return - } - - if !c.HasPermissionsToChannel(cchan, "updateNotifyLevel") { - return - } - - if result := <-Srv.Store.Channel().UpdateNotifyLevel(channelId, userId, notifyLevel); result.Err != nil { - c.Err = result.Err - return - } - - w.Write([]byte(model.MapToJson(data))) -} - func updateNotifyProps(c *Context, w http.ResponseWriter, r *http.Request) { data := model.MapFromJson(r.Body) diff --git a/api/channel_benchmark_test.go b/api/channel_benchmark_test.go index 77e679c14..7820f4a03 100644 --- a/api/channel_benchmark_test.go +++ b/api/channel_benchmark_test.go @@ -255,7 +255,7 @@ func BenchmarkRemoveChannelMember(b *testing.B) { } } -func BenchmarkUpdateNotifyLevel(b *testing.B) { +func BenchmarkUpdateNotifyProps(b *testing.B) { var ( NUM_CHANNELS_RANGE = utils.Range{NUM_CHANNELS, NUM_CHANNELS} ) @@ -271,9 +271,10 @@ func BenchmarkUpdateNotifyLevel(b *testing.B) { for i := range data { newmap := map[string]string{ - "channel_id": channels[i].Id, - "user_id": user.Id, - "notify_level": model.CHANNEL_NOTIFY_MENTION, + "channel_id": channels[i].Id, + "user_id": user.Id, + "desktop": model.CHANNEL_NOTIFY_MENTION, + "mark_unread": model.CHANNEL_MARK_UNREAD_MENTION, } data[i] = newmap } @@ -282,7 +283,7 @@ func BenchmarkUpdateNotifyLevel(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { for j := range channels { - Client.Must(Client.UpdateNotifyLevel(data[j])) + Client.Must(Client.UpdateNotifyProps(data[j])) } } } diff --git a/api/channel_test.go b/api/channel_test.go index 0e9dbbd41..e6c7ed80e 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -803,88 +803,6 @@ func TestRemoveChannelMember(t *testing.T) { } -func TestUpdateNotifyLevel(t *testing.T) { - Setup() - - team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN} - team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team) - - user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} - user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User) - store.Must(Srv.Store.User().VerifyEmail(user.Id)) - - Client.LoginByEmail(team.Name, user.Email, "pwd") - - channel1 := &model.Channel{DisplayName: "A Test API Name", Name: "a" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id} - channel1 = Client.Must(Client.CreateChannel(channel1)).Data.(*model.Channel) - - data := make(map[string]string) - data["channel_id"] = channel1.Id - data["user_id"] = user.Id - data["notify_level"] = model.CHANNEL_NOTIFY_MENTION - - timeBeforeUpdate := model.GetMillis() - time.Sleep(100 * time.Millisecond) - - if _, err := Client.UpdateNotifyLevel(data); err != nil { - t.Fatal(err) - } - - rget := Client.Must(Client.GetChannels("")) - rdata := rget.Data.(*model.ChannelList) - if len(rdata.Members) == 0 || rdata.Members[channel1.Id].NotifyLevel != data["notify_level"] { - t.Fatal("NotifyLevel did not update properly") - } - - if rdata.Members[channel1.Id].LastUpdateAt <= timeBeforeUpdate { - t.Fatal("LastUpdateAt did not update") - } - - data["user_id"] = "junk" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - bad user id") - } - - data["user_id"] = "12345678901234567890123456" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - bad user id") - } - - data["user_id"] = user.Id - data["channel_id"] = "junk" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - bad channel id") - } - - data["channel_id"] = "12345678901234567890123456" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - bad channel id") - } - - data["channel_id"] = channel1.Id - data["notify_level"] = "" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - empty notify level") - } - - data["notify_level"] = "junk" - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - bad notify level") - } - - user2 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey@test.com", Nickname: "Corey Hulen", Password: "pwd"} - user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User) - - Client.LoginByEmail(team.Name, user2.Email, "pwd") - - data["channel_id"] = channel1.Id - data["user_id"] = user2.Id - data["notify_level"] = model.CHANNEL_NOTIFY_MENTION - if _, err := Client.UpdateNotifyLevel(data); err == nil { - t.Fatal("Should have errored - user not in channel") - } -} - func TestUpdateNotifyProps(t *testing.T) { Setup() @@ -1002,7 +920,7 @@ func TestUpdateNotifyProps(t *testing.T) { data["user_id"] = user2.Id data["desktop"] = model.CHANNEL_NOTIFY_MENTION data["mark_unread"] = model.CHANNEL_MARK_UNREAD_MENTION - if _, err := Client.UpdateNotifyLevel(data); err == nil { + if _, err := Client.UpdateNotifyProps(data); err == nil { t.Fatal("Should have errored - user not in channel") } } -- cgit v1.2.3-1-g7c22