diff options
Diffstat (limited to 'store')
-rw-r--r-- | store/sql_preference_store.go | 22 | ||||
-rw-r--r-- | store/sql_preference_store_test.go | 35 | ||||
-rw-r--r-- | store/sql_team_store.go | 33 | ||||
-rw-r--r-- | store/sql_team_store_test.go | 47 | ||||
-rw-r--r-- | store/store.go | 2 |
5 files changed, 139 insertions, 0 deletions
diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index e20226ce6..5c46d1328 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -326,3 +326,25 @@ func (s SqlPreferenceStore) Delete(userId, category, name string) StoreChannel { return storeChannel } + +func (s SqlPreferenceStore) DeleteCategory(userId string, category string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + if _, err := s.GetMaster().Exec( + `DELETE FROM + Preferences + WHERE + UserId = :UserId + AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil { + result.Err = model.NewLocAppError("SqlPreferenceStore.DeleteCategory", "store.sql_preference.delete.app_error", nil, err.Error()) + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} diff --git a/store/sql_preference_store_test.go b/store/sql_preference_store_test.go index 8c6a2b6af..fc1cf5f5b 100644 --- a/store/sql_preference_store_test.go +++ b/store/sql_preference_store_test.go @@ -392,3 +392,38 @@ func TestPreferenceDelete(t *testing.T) { t.Fatal("should've returned no preferences") } } + +func TestPreferenceDeleteCategory(t *testing.T) { + Setup() + + category := model.NewId() + userId := model.NewId() + + preference1 := model.Preference{ + UserId: userId, + Category: category, + Name: model.NewId(), + Value: "value1a", + } + + preference2 := model.Preference{ + UserId: userId, + Category: category, + Name: model.NewId(), + Value: "value1a", + } + + Must(store.Preference().Save(&model.Preferences{preference1, preference2})) + + if prefs := Must(store.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 2 { + t.Fatal("should've returned 2 preferences") + } + + if result := <-store.Preference().DeleteCategory(userId, category); result.Err != nil { + t.Fatal(result.Err) + } + + if prefs := Must(store.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 { + t.Fatal("should've returned no preferences") + } +} diff --git a/store/sql_team_store.go b/store/sql_team_store.go index 3ec336be5..13b6baa1a 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -583,6 +583,39 @@ func (s SqlTeamStore) GetTeamsForUser(userId string) StoreChannel { return storeChannel } +func (s SqlTeamStore) GetTeamsUnreadForUser(teamId, userId string) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + var members []*model.TeamUnread + _, err := s.GetReplica().Select(&members, + `SELECT + Channels.TeamId, + SUM(Channels.TotalMsgCount - ChannelMembers.MsgCount) as MsgCount, + SUM(ChannelMembers.MentionCount) as MentionCount + FROM + Channels, + ChannelMembers + WHERE + Channels.Id = ChannelMembers.ChannelId AND + ChannelMembers.UserId = :UserId AND Channels.TeamId != :TeamId + GROUP BY + Channels.TeamId`, map[string]interface{}{"UserId": userId, "TeamId": teamId}) + if err != nil { + result.Err = model.NewLocAppError("SqlTeamStore.GetTeamsUnreadForUser", "store.sql_team.get_unread.app_error", nil, "userId="+userId+" "+err.Error()) + } else { + result.Data = members + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlTeamStore) RemoveMember(teamId string, userId string) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_team_store_test.go b/store/sql_team_store_test.go index 0e472a961..5e6abd102 100644 --- a/store/sql_team_store_test.go +++ b/store/sql_team_store_test.go @@ -529,3 +529,50 @@ func TestTeamStoreMemberCount(t *testing.T) { } } } + +func TestMyTeamMembersUnread(t *testing.T) { + Setup() + + teamId1 := model.NewId() + teamId2 := model.NewId() + + uid := model.NewId() + m1 := &model.TeamMember{TeamId: teamId1, UserId: uid} + m2 := &model.TeamMember{TeamId: teamId2, UserId: uid} + Must(store.Team().SaveMember(m1)) + Must(store.Team().SaveMember(m2)) + + c1 := &model.Channel{TeamId: m1.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN} + Must(store.Channel().Save(c1)) + c2 := &model.Channel{TeamId: m2.TeamId, Name: model.NewId(), DisplayName: "Town Square", Type: model.CHANNEL_OPEN} + Must(store.Channel().Save(c2)) + + cm1 := &model.ChannelMember{ChannelId: c1.Id, UserId: m1.UserId, NotifyProps: model.GetDefaultChannelNotifyProps()} + Must(store.Channel().SaveMember(cm1)) + cm2 := &model.ChannelMember{ChannelId: c2.Id, UserId: m2.UserId, NotifyProps: model.GetDefaultChannelNotifyProps()} + Must(store.Channel().SaveMember(cm2)) + + if r1 := <-store.Team().GetTeamsUnreadForUser("", uid); r1.Err != nil { + t.Fatal(r1.Err) + } else { + ms := r1.Data.([]*model.TeamUnread) + + if len(ms) != 2 { + t.Fatal("Should be the unreads for all the teams") + } + } + + if r2 := <-store.Team().GetTeamsUnreadForUser(teamId1, uid); r2.Err != nil { + t.Fatal(r2.Err) + } else { + ms := r2.Data.([]*model.TeamUnread) + + if len(ms) != 1 { + t.Fatal("Should be the unreads for just one team") + } + } + + if r1 := <-store.Team().RemoveAllMembersByUser(uid); r1.Err != nil { + t.Fatal(r1.Err) + } +} diff --git a/store/store.go b/store/store.go index b0cc09983..05eb7f4e1 100644 --- a/store/store.go +++ b/store/store.go @@ -74,6 +74,7 @@ type TeamStore interface { GetTotalMemberCount(teamId string) StoreChannel GetActiveMemberCount(teamId string) StoreChannel GetTeamsForUser(userId string) StoreChannel + GetTeamsUnreadForUser(teamId, userId string) StoreChannel RemoveMember(teamId string, userId string) StoreChannel RemoveAllMembersByTeam(teamId string) StoreChannel RemoveAllMembersByUser(userId string) StoreChannel @@ -270,6 +271,7 @@ type PreferenceStore interface { GetCategory(userId string, category string) StoreChannel GetAll(userId string) StoreChannel Delete(userId, category, name string) StoreChannel + DeleteCategory(userId string, category string) StoreChannel PermanentDeleteByUser(userId string) StoreChannel IsFeatureEnabled(feature, userId string) StoreChannel } |