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 ++++++++++++++----- model/channel_member.go | 34 +++++--- model/channel_member_test.go | 6 +- model/client.go | 4 +- store/sql_channel_store.go | 105 +++++++++++++++++-------- store/sql_channel_store_test.go | 32 ++++---- store/sql_post_store_test.go | 2 +- store/store.go | 2 +- web/react/components/channel_notifications.jsx | 16 ++-- web/react/components/notify_counts.jsx | 2 +- web/react/components/sidebar.jsx | 2 +- web/react/utils/client.jsx | 7 +- 13 files changed, 225 insertions(+), 117 deletions(-) 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") } } diff --git a/model/channel_member.go b/model/channel_member.go index 58c73d54a..f4a9a9836 100644 --- a/model/channel_member.go +++ b/model/channel_member.go @@ -20,15 +20,15 @@ const ( ) type ChannelMember struct { - ChannelId string `json:"channel_id"` - UserId string `json:"user_id"` - Roles string `json:"roles"` - LastViewedAt int64 `json:"last_viewed_at"` - MsgCount int64 `json:"msg_count"` - MentionCount int64 `json:"mention_count"` - NotifyLevel string `json:"notify_level"` - MarkUnreadLevel string `json:"mark_unread_level"` - LastUpdateAt int64 `json:"last_update_at"` + ChannelId string `json:"channel_id"` + UserId string `json:"user_id"` + Roles string `json:"roles"` + LastViewedAt int64 `json:"last_viewed_at"` + MsgCount int64 `json:"msg_count"` + MentionCount int64 `json:"mention_count"` + NotifyProps StringMap `json:"notify_props"` + NotifyLevel string `json:"notify_level"` + LastUpdateAt int64 `json:"last_update_at"` } func (o *ChannelMember) ToJson() string { @@ -71,8 +71,9 @@ func (o *ChannelMember) IsValid() *AppError { return NewAppError("ChannelMember.IsValid", "Invalid notify level", "notify_level="+o.NotifyLevel) } - if len(o.MarkUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(o.MarkUnreadLevel) { - return NewAppError("ChannelMember.IsValid", "Invalid mark unread level", "mark_unread_level="+o.MarkUnreadLevel) + markUnreadLevel := o.NotifyProps["mark_unread"] + if len(markUnreadLevel) > 20 || !IsChannelMarkUnreadLevelValid(markUnreadLevel) { + return NewAppError("ChannelMember.IsValid", "Invalid mark unread level", "mark_unread_level="+markUnreadLevel) } return nil @@ -82,6 +83,10 @@ func (o *ChannelMember) PreSave() { o.LastUpdateAt = GetMillis() } +func (o *ChannelMember) PreUpdate() { + o.LastUpdateAt = GetMillis() +} + func IsChannelNotifyLevelValid(notifyLevel string) bool { return notifyLevel == CHANNEL_NOTIFY_DEFAULT || notifyLevel == CHANNEL_NOTIFY_ALL || @@ -92,3 +97,10 @@ func IsChannelNotifyLevelValid(notifyLevel string) bool { func IsChannelMarkUnreadLevelValid(markUnreadLevel string) bool { return markUnreadLevel == CHANNEL_MARK_UNREAD_ALL || markUnreadLevel == CHANNEL_MARK_UNREAD_MENTION } + +func GetDefaultChannelNotifyProps() StringMap { + return StringMap{ + "desktop": CHANNEL_NOTIFY_DEFAULT, + "mark_unread": CHANNEL_MARK_UNREAD_ALL, + } +} diff --git a/model/channel_member_test.go b/model/channel_member_test.go index ae3da73cc..5dc34ae56 100644 --- a/model/channel_member_test.go +++ b/model/channel_member_test.go @@ -32,7 +32,7 @@ func TestChannelMemberIsValid(t *testing.T) { o.Roles = "missing" o.NotifyLevel = CHANNEL_NOTIFY_ALL - o.MarkUnreadLevel = CHANNEL_MARK_UNREAD_ALL + o.NotifyProps = GetDefaultChannelNotifyProps() o.UserId = NewId() if err := o.IsValid(); err == nil { t.Fatal("should be invalid") @@ -54,12 +54,12 @@ func TestChannelMemberIsValid(t *testing.T) { t.Fatal(err) } - o.MarkUnreadLevel = "123456789012345678901" + o.NotifyProps["mark_unread"] = "123456789012345678901" if err := o.IsValid(); err == nil { t.Fatal("should be invalid") } - o.MarkUnreadLevel = CHANNEL_MARK_UNREAD_ALL + o.NotifyProps["mark_unread"] = CHANNEL_MARK_UNREAD_ALL if err := o.IsValid(); err != nil { t.Fatal(err) } diff --git a/model/client.go b/model/client.go index 1d1c5a52c..bba839809 100644 --- a/model/client.go +++ b/model/client.go @@ -459,8 +459,8 @@ func (c *Client) UpdateNotifyLevel(data map[string]string) (*Result, *AppError) } } -func (c *Client) UpdateMarkUnreadLevel(data map[string]string) (*Result, *AppError) { - if r, err := c.DoApiPost("/channels/update_mark_unread_level", MapToJson(data)); err != nil { +func (c *Client) UpdateNotifyProps(data map[string]string) (*Result, *AppError) { + if r, err := c.DoApiPost("/channels/update_notify_props", MapToJson(data)); err != nil { return nil, err } else { return &Result{r.Header.Get(HEADER_REQUEST_ID), diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 892ee1398..f453c3589 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -4,6 +4,7 @@ package store import ( + l4g "code.google.com/p/log4go" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -30,14 +31,56 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore { tablem.ColMap("ChannelId").SetMaxSize(26) tablem.ColMap("UserId").SetMaxSize(26) tablem.ColMap("Roles").SetMaxSize(64) - tablem.ColMap("NotifyLevel").SetMaxSize(20) + tablem.ColMap("NotifyProps").SetMaxSize(2000) } return s } func (s SqlChannelStore) UpgradeSchemaIfNeeded() { - s.CreateColumnIfNotExists("ChannelMembers", "MarkUnreadLevel", "varchar(20)", "varchar(20)", model.CHANNEL_MARK_UNREAD_ALL) + if s.CreateColumnIfNotExists("ChannelMembers", "NotifyProps", "varchar(2000)", "varchar(2000)", "{}") { + // populate NotifyProps from existing NotifyLevel field + + // set default values + _, err := s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = CONCAT('{"desktop":"', CONCAT(NotifyLevel, '","mark_unread":"` + model.CHANNEL_MARK_UNREAD_ALL + `"}'))`) + if err != nil { + l4g.Error("Unable to set default values for ChannelMembers.NotifyProps") + l4g.Error(err.Error()) + } + + // assume channels with all notifications enabled are just using the default settings + _, err = s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = '{"desktop":"` + model.CHANNEL_NOTIFY_DEFAULT + `","mark_unread":"` + model.CHANNEL_MARK_UNREAD_ALL + `"}' + WHERE + NotifyLevel = '` + model.CHANNEL_NOTIFY_ALL + `'`) + if err != nil { + l4g.Error("Unable to set values for ChannelMembers.NotifyProps when members previously had notifyLevel=all") + l4g.Error(err.Error()) + } + + // set quiet mode channels to have no notifications and only mark the channel unread on mentions + _, err = s.GetMaster().Exec( + `UPDATE + ChannelMembers + SET + NotifyProps = '{"desktop":"` + model.CHANNEL_NOTIFY_NONE + `","mark_unread":"` + model.CHANNEL_MARK_UNREAD_MENTION + `"}' + WHERE + NotifyLevel = 'quiet'`) + if err != nil { + l4g.Error("Unable to set values for ChannelMembers.NotifyProps when members previously had notifyLevel=quiet") + l4g.Error(err.Error()) + } + + // TODO uncomment me + // s.RemoveColumnIfExists("ChannelMembers", "NotifyLevel") + } } func (s SqlChannelStore) CreateIndexesIfNotExists() { @@ -387,6 +430,34 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel { return storeChannel } +func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + member.PreUpdate() + + if result.Err = member.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + if _, err := s.GetMaster().Update(member); err != nil { + result.Err = model.NewAppError("SqlChannelStore.UpdateMember", "We encounted an error updating the channel member", + "channel_id="+member.ChannelId+", "+"user_id="+member.UserId+", "+err.Error()) + } else { + result.Data = member + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetMembers(channelId string) StoreChannel { storeChannel := make(StoreChannel) @@ -650,6 +721,7 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) return storeChannel } +// TODO remove me func (s SqlChannelStore) UpdateNotifyLevel(channelId, userId, notifyLevel string) StoreChannel { storeChannel := make(StoreChannel) @@ -679,35 +751,6 @@ func (s SqlChannelStore) UpdateNotifyLevel(channelId, userId, notifyLevel string return storeChannel } -func (s SqlChannelStore) UpdateMarkUnreadLevel(channelId, userId, markUnreadLevel string) StoreChannel { - storeChannel := make(StoreChannel) - - go func() { - result := StoreResult{} - - updateAt := model.GetMillis() - - _, err := s.GetMaster().Exec( - `UPDATE - ChannelMembers - SET - MarkUnreadLevel = :MarkUnreadLevel, - LastUpdateAt = :LastUpdateAt - WHERE - UserId = :UserId - AND ChannelId = :ChannelId`, - map[string]interface{}{"ChannelId": channelId, "UserId": userId, "MarkUnreadLevel": markUnreadLevel, "LastUpdateAt": updateAt}) - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.UpdateMarkUnreadLevel", "We couldn't update the mark unread level", "channel_id="+channelId+", user_id="+userId+", "+err.Error()) - } - - storeChannel <- result - close(storeChannel) - }() - - return storeChannel -} - func (s SqlChannelStore) GetForExport(teamId string) StoreChannel { storeChannel := make(StoreChannel) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index b97440306..45c05645e 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -136,14 +136,14 @@ func TestChannelStoreDelete(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o2.Id m2.UserId = m1.UserId m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m2.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) if r := <-store.Channel().Delete(o1.Id, model.GetMillis()); r.Err != nil { @@ -225,14 +225,14 @@ func TestChannelMemberStore(t *testing.T) { o1.ChannelId = c1.Id o1.UserId = u1.Id o1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - o1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + o1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&o1)) o2 := model.ChannelMember{} o2.ChannelId = c1.Id o2.UserId = u2.Id o2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - o2.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + o2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&o2)) c1t2 := (<-store.Channel().Get(c1.Id)).Data.(*model.Channel) @@ -296,7 +296,7 @@ func TestChannelStorePermissionsTo(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) count := (<-store.Channel().CheckPermissionsTo(o1.TeamId, o1.Id, m1.UserId)).Data.(int64) @@ -377,21 +377,21 @@ func TestChannelStoreGetChannels(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m2.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m3.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) cresult := <-store.Channel().GetChannels(o1.TeamId, m1.UserId) @@ -423,21 +423,21 @@ func TestChannelStoreGetMoreChannels(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m2.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m3.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) o3 := model.Channel{} @@ -494,21 +494,21 @@ func TestChannelStoreGetChannelCounts(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o1.Id m2.UserId = model.NewId() m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m2.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) m3 := model.ChannelMember{} m3.ChannelId = o2.Id m3.UserId = model.NewId() m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m3.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m3.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m3)) cresult := <-store.Channel().GetChannelCounts(o1.TeamId, m1.UserId) @@ -538,7 +538,7 @@ func TestChannelStoreUpdateLastViewedAt(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().UpdateLastViewedAt(m1.ChannelId, m1.UserId)).Err @@ -567,7 +567,7 @@ func TestChannelStoreIncrementMentionCount(t *testing.T) { m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) err := (<-store.Channel().IncrementMentionCount(m1.ChannelId, m1.UserId)).Err diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index 39761c51c..c8f7e7587 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -485,7 +485,7 @@ func TestPostStoreSearch(t *testing.T) { m1.ChannelId = c1.Id m1.UserId = userId m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL - m1.MarkUnreadLevel = model.CHANNEL_MARK_UNREAD_ALL + m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) c2 := &model.Channel{} diff --git a/store/store.go b/store/store.go index 83366b79e..2cffe99de 100644 --- a/store/store.go +++ b/store/store.go @@ -62,6 +62,7 @@ type ChannelStore interface { GetForExport(teamId string) StoreChannel SaveMember(member *model.ChannelMember) StoreChannel + UpdateMember(member *model.ChannelMember) StoreChannel GetMembers(channelId string) StoreChannel GetMember(channelId string, userId string) StoreChannel RemoveMember(channelId string, userId string) StoreChannel @@ -72,7 +73,6 @@ type ChannelStore interface { UpdateLastViewedAt(channelId string, userId string) StoreChannel IncrementMentionCount(channelId string, userId string) StoreChannel UpdateNotifyLevel(channelId string, userId string, notifyLevel string) StoreChannel - UpdateMarkUnreadLevel(channelId string, userId string, markUnreadLevel string) StoreChannel } type PostStore interface { diff --git a/web/react/components/channel_notifications.jsx b/web/react/components/channel_notifications.jsx index eaa1c8255..55c0b5438 100644 --- a/web/react/components/channel_notifications.jsx +++ b/web/react/components/channel_notifications.jsx @@ -42,7 +42,7 @@ export default class ChannelNotifications extends React.Component { const member = ChannelStore.getMember(channelId); var notifyLevel = member.notify_level; - var markUnreadLevel = member.mark_unread_level; + var markUnreadLevel = member.notify_props.mark_unread; this.setState({ notifyLevel, @@ -63,7 +63,7 @@ export default class ChannelNotifications extends React.Component { const member = ChannelStore.getMember(this.state.channelId); var notifyLevel = member.notify_level; - var markUnreadLevel = member.mark_unread_level; + var markUnreadLevel = member.notify_props.mark_unread; var newState = this.state; newState.notifyLevel = notifyLevel; @@ -249,7 +249,7 @@ export default class ChannelNotifications extends React.Component { const channelId = this.state.channelId; const markUnreadLevel = this.state.markUnreadLevel; - if (ChannelStore.getMember(channelId).mark_unread_level === markUnreadLevel) { + if (ChannelStore.getMember(channelId).notify_props.mark_unread === markUnreadLevel) { this.updateSection(''); return; } @@ -257,17 +257,13 @@ export default class ChannelNotifications extends React.Component { const data = { channel_id: channelId, user_id: UserStore.getCurrentId(), - mark_unread_level: markUnreadLevel + mark_unread: markUnreadLevel }; - if (!data.mark_unread_level || data.mark_unread_level.length === 0) { - return; - } - - Client.updateMarkUnreadLevel(data, + Client.updateNotifyProps(data, () => { var member = ChannelStore.getMember(channelId); - member.mark_unread_level = markUnreadLevel; + member.notify_props.mark_unread = markUnreadLevel; ChannelStore.setChannelMember(member); this.updateSection(''); }, diff --git a/web/react/components/notify_counts.jsx b/web/react/components/notify_counts.jsx index 3df661d70..f34b4669f 100644 --- a/web/react/components/notify_counts.jsx +++ b/web/react/components/notify_counts.jsx @@ -15,7 +15,7 @@ function getCountsStateFromStores() { count += channel.total_msg_count - channelMember.msg_count; } else if (channelMember.mention_count > 0) { count += channelMember.mention_count; - } else if (channelMember.mark_unread_level !== 'mention' && channel.total_msg_count - channelMember.msg_count > 0) { + } else if (channelMember.notify_props.mark_unread !== 'mention' && channel.total_msg_count - channelMember.msg_count > 0) { count += 1; } }); diff --git a/web/react/components/sidebar.jsx b/web/react/components/sidebar.jsx index 5a4fb1169..821d7fd4a 100644 --- a/web/react/components/sidebar.jsx +++ b/web/react/components/sidebar.jsx @@ -334,7 +334,7 @@ export default class Sidebar extends React.Component { var unread = false; if (channelMember) { msgCount = channel.total_msg_count - channelMember.msg_count; - unread = (msgCount > 0 && channelMember.mark_unread_level !== 'mention') || channelMember.mention_count > 0; + unread = (msgCount > 0 && channelMember.notify_props.mark_unread !== 'mention') || channelMember.mention_count > 0; } var titleClass = ''; diff --git a/web/react/utils/client.jsx b/web/react/utils/client.jsx index ce831be0d..041c00516 100644 --- a/web/react/utils/client.jsx +++ b/web/react/utils/client.jsx @@ -568,6 +568,7 @@ export function updateChannelDesc(data, success, error) { track('api', 'api_channels_desc'); } +// TODO remove me export function updateNotifyLevel(data, success, error) { $.ajax({ url: '/api/v1/channels/update_notify_level', @@ -583,16 +584,16 @@ export function updateNotifyLevel(data, success, error) { }); } -export function updateMarkUnreadLevel(data, success, error) { +export function updateNotifyProps(data, success, error) { $.ajax({ - url: '/api/v1/channels/update_mark_unread_level', + url: '/api/v1/channels/update_notify_props', dataType: 'json', contentType: 'application/json', type: 'POST', data: JSON.stringify(data), success, error: function onError(xhr, status, err) { - var e = handleError('updateMarkUnreadLevel', xhr, status, err); + var e = handleError('updateNotifyProps', xhr, status, err); error(e); } }); -- cgit v1.2.3-1-g7c22