@@ -31,7 +31,7 @@ import Wire.API.Pagination
31
31
import Wire.API.User.Profile
32
32
import Wire.API.UserGroup hiding (UpdateUserGroupChannels )
33
33
import Wire.API.UserGroup.Pagination
34
- import Wire.UserGroupStore (PaginationState (.. ), UserGroupPageRequest (.. ), UserGroupStore (.. ), toSortBy )
34
+ import Wire.UserGroupStore (IncludeConversations ( .. ), PaginationState (.. ), UserGroupPageRequest (.. ), UserGroupStore (.. ), toSortBy )
35
35
36
36
type UserGroupStorePostgresEffectConstraints r =
37
37
( Member (Embed IO ) r ,
@@ -46,7 +46,11 @@ interpretUserGroupStoreToPostgres ::
46
46
interpretUserGroupStoreToPostgres =
47
47
interpret $ \ case
48
48
CreateUserGroup team newUserGroup managedBy -> createUserGroup team newUserGroup managedBy
49
- GetUserGroup team userGroupId -> getUserGroup team userGroupId
49
+ GetUserGroup team userGroupId includeConversations' ->
50
+ case includeConversations' of
51
+ DoNotIncludeConversations -> getUserGroup team userGroupId
52
+ IncludeConversationsCountOnly -> getUserGroup team userGroupId
53
+ IncludeConversations -> getUserGroupWithConversations team userGroupId
50
54
GetUserGroups req -> getUserGroups req
51
55
UpdateUserGroup tid gid gup -> updateGroup tid gid gup
52
56
DeleteUserGroup tid gid -> deleteGroup tid gid
@@ -115,6 +119,92 @@ getUserGroup team id_ = do
115
119
select (user_id :: uuid) from user_group_member where user_group_id = ($1 :: uuid)
116
120
|]
117
121
122
+ getUserGroupWithChannelIds ::
123
+ forall r .
124
+ (UserGroupStorePostgresEffectConstraints r ) =>
125
+ TeamId ->
126
+ UserGroupId ->
127
+ Sem r (Maybe (UserGroup , Vector ConvId ))
128
+ getUserGroupWithChannelIds team id_ = do
129
+ pool <- input
130
+ eitherRes <- liftIO $ use pool session
131
+ either throw pure eitherRes
132
+ where
133
+ session :: Session (Maybe (UserGroup , Vector ConvId ))
134
+ session = runMaybeT do
135
+ (name, managedBy, createdAt, memberIds, channelIds) <- MaybeT $ statement (id_, team) stmt
136
+ let members = Identity $ fmap Id memberIds
137
+ membersCount = Nothing
138
+ channels = mempty
139
+ channelsCount = Nothing
140
+ ug = UserGroup_ {id_ = id_, name, managedBy, createdAt, members, membersCount, channels, channelsCount}
141
+ pure (ug, fmap Id channelIds)
142
+
143
+ decodeRow :: (Text , Int32 , UTCTime , Vector UUID , Vector UUID ) -> Either Text (UserGroupName , ManagedBy , UTCTimeMillis , Vector UUID , Vector UUID )
144
+ decodeRow (nameTxt, managedByInt, utcTime, membs, chans) = do
145
+ name <- userGroupNameFromText nameTxt
146
+ managedBy <- managedByFromInt32 managedByInt
147
+ pure (name, managedBy, toUTCTimeMillis utcTime, membs, chans)
148
+
149
+ stmt :: Statement (UserGroupId , TeamId ) (Maybe (UserGroupName , ManagedBy , UTCTimeMillis , Vector UUID , Vector UUID ))
150
+ stmt =
151
+ lmap (\ (gid, tid) -> (gid. toUUID, tid. toUUID))
152
+ . refineResult (mapM decodeRow)
153
+ $ [maybeStatement |
154
+ select
155
+ (name :: text),
156
+ (managed_by :: int),
157
+ (created_at :: timestamptz),
158
+ coalesce((select array_agg(ugm.user_id) from user_group_member ugm where ugm.user_group_id = ug.id), array[]::uuid[]) :: uuid[],
159
+ coalesce((select array_agg(ugc.conv_id) from user_group_channel ugc where ugc.user_group_id = ug.id), array[]::uuid[]) :: uuid[]
160
+ from user_group ug
161
+ where ug.id = ($1 :: uuid) and ug.team_id = ($2 :: uuid)
162
+ |]
163
+
164
+ getUserGroupWithConversations ::
165
+ forall r .
166
+ -- (UserGroupStorePostgresEffectConstraints r) =>
167
+ TeamId ->
168
+ UserGroupId ->
169
+ Sem r (Maybe UserGroup )
170
+ getUserGroupWithConversations _team _id_ = do
171
+ error " TODO"
172
+
173
+ -- pool <- input
174
+ -- eitherUserGroup <- liftIO $ use pool session
175
+ -- either throw pure eitherUserGroup
176
+ -- where
177
+ -- session :: Session (Maybe UserGroup)
178
+ -- session = runMaybeT do
179
+ -- (name, managedBy, createdAt, members', channels') <- MaybeT $ statement (id_, team) getGroupMetadataStatement
180
+ -- let membersCount = Nothing
181
+ -- channelsCount = Just $ length channels
182
+ -- members = Id <$> members'
183
+ -- channels = Identity $ Id <$> channels'
184
+ -- pure $ UserGroup_ {..}
185
+ --
186
+ -- decodeMetadataRow ::
187
+ -- (Text, Int32, UTCTime, Vector UUID, Vector UUID) ->
188
+ -- Either Text (UserGroupName, ManagedBy, UTCTimeMillis, Vector UUID, Vector UUID)
189
+ -- decodeMetadataRow (name, managedByInt, utcTime) =
190
+ -- (,,toUTCTimeMillis utcTime)
191
+ -- <$> userGroupNameFromText name
192
+ -- <*> managedByFromInt32 managedByInt
193
+ --
194
+ -- getGroupMetadataStatement :: Statement (UserGroupId, TeamId) (Maybe (UserGroupName, ManagedBy, UTCTimeMillis), Vector UUID, Vector UUID)
195
+ -- getGroupMetadataStatement =
196
+ -- lmap (\(gid, tid) -> (gid.toUUID, tid.toUUID))
197
+ -- . refineResult (mapM decodeMetadataRow)
198
+ -- $ [maybeStatement|
199
+ -- select
200
+ -- (name :: text),
201
+ -- (managed_by :: int),
202
+ -- (created_at :: timestamptz),
203
+ -- coalesce((select array_agg(ugm.user_id) from user_group_member ugm where ugm.user_group_id = ug.id), array[]::uuid[]) :: uuid[],
204
+ -- coalesce((select array_agg(ugc.conv_id) from user_group_channel ugc where ugc.user_group_id = ug.id), array[]::uuid[]) :: uuid[]
205
+ -- from user_group ug where ug.id = ($1 :: uuid) AND ug.team_id = ($2 :: uuid)
206
+ -- | ]
207
+
118
208
divide3 :: (Divisible f ) => (p -> (a , b , c )) -> f a -> f b -> f c -> f p
119
209
divide3 f a b c = divide (\ p -> let (x, y, z) = f p in (x, (y, z))) a (divide id b c)
120
210
0 commit comments