From 6c0fefad152e1843bccf80fb675301b789f70dd5 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Mon, 10 Aug 2015 14:47:45 -0400 Subject: added getChannelCounts service and refactored the client to more intelligently pull channel data --- store/sql_channel_store.go | 33 +++++++++++++++++++++++++++++++++ store/store.go | 1 + 2 files changed, 34 insertions(+) (limited to 'store') diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index cac5c681b..6caa6fc70 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -281,6 +281,39 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string) StoreChan return storeChannel } +type channelIdWithCount struct { + Id string + TotalMsgCount int64 +} + +func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + var data []channelIdWithCount + _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND TeamId = :TeamId AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) + + if err != nil { + result.Err = model.NewAppError("SqlChannelStore.GetChannelCounts", "We couldn't get the channel counts", "teamId="+teamId+", userId="+userId+", err="+err.Error()) + } else { + counts := &model.ChannelCounts{Counts: make(map[string]int64)} + for i := range data { + v := data[i] + counts.Counts[v.Id] = v.TotalMsgCount + } + + result.Data = counts + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) GetByName(teamId string, name string) StoreChannel { storeChannel := make(StoreChannel) diff --git a/store/store.go b/store/store.go index 0934fe84b..613fe4198 100644 --- a/store/store.go +++ b/store/store.go @@ -50,6 +50,7 @@ type ChannelStore interface { GetByName(team_id string, domain string) StoreChannel GetChannels(teamId string, userId string) StoreChannel GetMoreChannels(teamId string, userId string) StoreChannel + GetChannelCounts(teamId string, userId string) StoreChannel SaveMember(member *model.ChannelMember) StoreChannel GetMembers(channelId string) StoreChannel -- cgit v1.2.3-1-g7c22 From 4ec76e059cddc127c35bf758f7fda7c7636c8d70 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Tue, 11 Aug 2015 08:20:17 -0400 Subject: incorporate channel updateAt into channel counts and pull channel data on channel update --- store/sql_channel_store.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'store') diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 6caa6fc70..7b2d9df24 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -281,9 +281,10 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string) StoreChan return storeChannel } -type channelIdWithCount struct { +type channelIdWithCountandUpdateAt struct { Id string TotalMsgCount int64 + UpdateAt int64 } func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) StoreChannel { @@ -292,16 +293,17 @@ func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) StoreCha go func() { result := StoreResult{} - var data []channelIdWithCount - _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND TeamId = :TeamId AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) + var data []channelIdWithCountandUpdateAt + _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND TeamId = :TeamId AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) if err != nil { result.Err = model.NewAppError("SqlChannelStore.GetChannelCounts", "We couldn't get the channel counts", "teamId="+teamId+", userId="+userId+", err="+err.Error()) } else { - counts := &model.ChannelCounts{Counts: make(map[string]int64)} + counts := &model.ChannelCounts{Counts: make(map[string]int64), UpdateTimes: make(map[string]int64)} for i := range data { v := data[i] counts.Counts[v.Id] = v.TotalMsgCount + counts.UpdateTimes[v.Id] = v.UpdateAt } result.Data = counts -- cgit v1.2.3-1-g7c22 From 9f57ed5bf12d2d3a4cb143985c80646b877a1706 Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Tue, 11 Aug 2015 09:02:58 -0400 Subject: added store unit test for GetChannelCounts --- store/sql_channel_store_test.go | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'store') diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index b14883843..dabe39904 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -462,6 +462,53 @@ func TestChannelStoreGetMoreChannels(t *testing.T) { } } +func TestChannelStoreGetChannelCounts(t *testing.T) { + Setup() + + o2 := model.Channel{} + o2.TeamId = model.NewId() + o2.DisplayName = "Channel2" + o2.Name = "a" + model.NewId() + "b" + o2.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o2)) + + o1 := model.Channel{} + o1.TeamId = model.NewId() + o1.DisplayName = "Channel1" + o1.Name = "a" + model.NewId() + "b" + o1.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o1)) + + m1 := model.ChannelMember{} + m1.ChannelId = o1.Id + m1.UserId = model.NewId() + m1.NotifyLevel = model.CHANNEL_NOTIFY_ALL + Must(store.Channel().SaveMember(&m1)) + + m2 := model.ChannelMember{} + m2.ChannelId = o1.Id + m2.UserId = model.NewId() + m2.NotifyLevel = model.CHANNEL_NOTIFY_ALL + Must(store.Channel().SaveMember(&m2)) + + m3 := model.ChannelMember{} + m3.ChannelId = o2.Id + m3.UserId = model.NewId() + m3.NotifyLevel = model.CHANNEL_NOTIFY_ALL + Must(store.Channel().SaveMember(&m3)) + + cresult := <-store.Channel().GetChannelCounts(o1.TeamId, m1.UserId) + counts := cresult.Data.(*model.ChannelCounts) + + if len(counts.Counts) != 1 { + t.Fatal("wrong number of counts") + } + + if len(counts.UpdateTimes) != 1 { + t.Fatal("wrong number of update times") + } +} + func TestChannelStoreUpdateLastViewedAt(t *testing.T) { Setup() -- cgit v1.2.3-1-g7c22 From cb17851e2df46f4a76ef4eefda980df818b4b01d Mon Sep 17 00:00:00 2001 From: JoramWilander Date: Tue, 11 Aug 2015 09:39:54 -0400 Subject: fix channelIdWithCountAndUpdateAt naming to be proper camel case --- store/sql_channel_store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'store') diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 7b2d9df24..b8bf8b5ac 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -281,7 +281,7 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string) StoreChan return storeChannel } -type channelIdWithCountandUpdateAt struct { +type channelIdWithCountAndUpdateAt struct { Id string TotalMsgCount int64 UpdateAt int64 @@ -293,7 +293,7 @@ func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) StoreCha go func() { result := StoreResult{} - var data []channelIdWithCountandUpdateAt + var data []channelIdWithCountAndUpdateAt _, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND TeamId = :TeamId AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId}) if err != nil { -- cgit v1.2.3-1-g7c22