-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
group.go
247 lines (201 loc) · 7.86 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"github.com/mattermost/mattermost-server/v5/model"
)
func (a *App) GetGroup(id string) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().Get(id)
}
func (a *App) GetGroupByName(name string) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetByName(name)
}
func (a *App) GetGroupByRemoteID(remoteID string, groupSource model.GroupSource) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetByRemoteID(remoteID, groupSource)
}
func (a *App) GetGroupsBySource(groupSource model.GroupSource) ([]*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetAllBySource(groupSource)
}
func (a *App) GetGroupsByUserId(userId string) ([]*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetByUser(userId)
}
func (a *App) CreateGroup(group *model.Group) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().Create(group)
}
func (a *App) UpdateGroup(group *model.Group) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().Update(group)
}
func (a *App) DeleteGroup(groupID string) (*model.Group, *model.AppError) {
return a.Srv.Store.Group().Delete(groupID)
}
func (a *App) GetGroupMemberUsers(groupID string) ([]*model.User, *model.AppError) {
return a.Srv.Store.Group().GetMemberUsers(groupID)
}
func (a *App) GetGroupMemberUsersPage(groupID string, page int, perPage int) ([]*model.User, int, *model.AppError) {
members, err := a.Srv.Store.Group().GetMemberUsersPage(groupID, page, perPage)
if err != nil {
return nil, 0, err
}
count, err := a.Srv.Store.Group().GetMemberCount(groupID)
if err != nil {
return nil, 0, err
}
return members, int(count), nil
}
func (a *App) UpsertGroupMember(groupID string, userID string) (*model.GroupMember, *model.AppError) {
return a.Srv.Store.Group().UpsertMember(groupID, userID)
}
func (a *App) DeleteGroupMember(groupID string, userID string) (*model.GroupMember, *model.AppError) {
return a.Srv.Store.Group().DeleteMember(groupID, userID)
}
func (a *App) CreateGroupSyncable(groupSyncable *model.GroupSyncable) (*model.GroupSyncable, *model.AppError) {
return a.Srv.Store.Group().CreateGroupSyncable(groupSyncable)
}
func (a *App) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
return a.Srv.Store.Group().GetGroupSyncable(groupID, syncableID, syncableType)
}
func (a *App) GetGroupSyncables(groupID string, syncableType model.GroupSyncableType) ([]*model.GroupSyncable, *model.AppError) {
return a.Srv.Store.Group().GetAllGroupSyncablesByGroupId(groupID, syncableType)
}
func (a *App) UpdateGroupSyncable(groupSyncable *model.GroupSyncable) (*model.GroupSyncable, *model.AppError) {
return a.Srv.Store.Group().UpdateGroupSyncable(groupSyncable)
}
func (a *App) DeleteGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
return a.Srv.Store.Group().DeleteGroupSyncable(groupID, syncableID, syncableType)
}
func (a *App) TeamMembersToAdd(since int64) ([]*model.UserTeamIDPair, *model.AppError) {
return a.Srv.Store.Group().TeamMembersToAdd(since)
}
func (a *App) ChannelMembersToAdd(since int64) ([]*model.UserChannelIDPair, *model.AppError) {
return a.Srv.Store.Group().ChannelMembersToAdd(since)
}
func (a *App) TeamMembersToRemove() ([]*model.TeamMember, *model.AppError) {
return a.Srv.Store.Group().TeamMembersToRemove()
}
func (a *App) ChannelMembersToRemove() ([]*model.ChannelMember, *model.AppError) {
return a.Srv.Store.Group().ChannelMembersToRemove()
}
func (a *App) GetGroupsByChannel(channelId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) {
groups, err := a.Srv.Store.Group().GetGroupsByChannel(channelId, opts)
if err != nil {
return nil, 0, err
}
count, err := a.Srv.Store.Group().CountGroupsByChannel(channelId, opts)
if err != nil {
return nil, 0, err
}
return groups, int(count), nil
}
func (a *App) GetGroupsByTeam(teamId string, opts model.GroupSearchOpts) ([]*model.Group, int, *model.AppError) {
groups, err := a.Srv.Store.Group().GetGroupsByTeam(teamId, opts)
if err != nil {
return nil, 0, err
}
count, err := a.Srv.Store.Group().CountGroupsByTeam(teamId, opts)
if err != nil {
return nil, 0, err
}
return groups, int(count), nil
}
func (a *App) GetGroups(page, perPage int, opts model.GroupSearchOpts) ([]*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetGroups(page, perPage, opts)
}
// TeamMembersMinusGroupMembers returns the set of users on the given team minus the set of users in the given
// groups.
//
// The result can be used, for example, to determine the set of users who would be removed from a team if the team
// were group-constrained with the given groups.
func (a *App) TeamMembersMinusGroupMembers(teamID string, groupIDs []string, page, perPage int) ([]*model.UserWithGroups, int64, *model.AppError) {
users, err := a.Srv.Store.Group().TeamMembersMinusGroupMembers(teamID, groupIDs, page, perPage)
if err != nil {
return nil, 0, err
}
// parse all group ids of all users
allUsersGroupIDMap := map[string]bool{}
for _, user := range users {
for _, groupID := range user.GetGroupIDs() {
allUsersGroupIDMap[groupID] = true
}
}
// create a slice of distinct group ids
var allUsersGroupIDSlice []string
for key := range allUsersGroupIDMap {
allUsersGroupIDSlice = append(allUsersGroupIDSlice, key)
}
// retrieve groups from DB
groups, err := a.GetGroupsByIDs(allUsersGroupIDSlice)
if err != nil {
return nil, 0, err
}
// map groups by id
groupMap := map[string]*model.Group{}
for _, group := range groups {
groupMap[group.Id] = group
}
// populate each instance's groups field
for _, user := range users {
user.Groups = []*model.Group{}
for _, groupID := range user.GetGroupIDs() {
group, ok := groupMap[groupID]
if ok {
user.Groups = append(user.Groups, group)
}
}
}
totalCount, err := a.Srv.Store.Group().CountTeamMembersMinusGroupMembers(teamID, groupIDs)
if err != nil {
return nil, 0, err
}
return users, totalCount, nil
}
func (a *App) GetGroupsByIDs(groupIDs []string) ([]*model.Group, *model.AppError) {
return a.Srv.Store.Group().GetByIDs(groupIDs)
}
// ChannelMembersMinusGroupMembers returns the set of users in the given channel minus the set of users in the given
// groups.
//
// The result can be used, for example, to determine the set of users who would be removed from a channel if the
// channel were group-constrained with the given groups.
func (a *App) ChannelMembersMinusGroupMembers(channelID string, groupIDs []string, page, perPage int) ([]*model.UserWithGroups, int64, *model.AppError) {
users, err := a.Srv.Store.Group().ChannelMembersMinusGroupMembers(channelID, groupIDs, page, perPage)
if err != nil {
return nil, 0, err
}
// parse all group ids of all users
allUsersGroupIDMap := map[string]bool{}
for _, user := range users {
for _, groupID := range user.GetGroupIDs() {
allUsersGroupIDMap[groupID] = true
}
}
// create a slice of distinct group ids
var allUsersGroupIDSlice []string
for key := range allUsersGroupIDMap {
allUsersGroupIDSlice = append(allUsersGroupIDSlice, key)
}
// retrieve groups from DB
groups, err := a.GetGroupsByIDs(allUsersGroupIDSlice)
if err != nil {
return nil, 0, err
}
// map groups by id
groupMap := map[string]*model.Group{}
for _, group := range groups {
groupMap[group.Id] = group
}
// populate each instance's groups field
for _, user := range users {
user.Groups = []*model.Group{}
for _, groupID := range user.GetGroupIDs() {
group, ok := groupMap[groupID]
if ok {
user.Groups = append(user.Groups, group)
}
}
}
totalCount, err := a.Srv.Store.Group().CountChannelMembersMinusGroupMembers(channelID, groupIDs)
if err != nil {
return nil, 0, err
}
return users, totalCount, nil
}