@@ -52,7 +52,7 @@ import GHC.Generics (Generic)
5252import Data.Aeson
5353import Servant.Docs
5454import Data.Proxy
55−import Data.Vector (Vector, toList)
55+import Data.Vector (Vector, toList, fromList)
5656import Data.Text (Text)
5757import Data.Channel.NamedChannel
5858import Data.Channel.AnonymousChannel
@@ -118,6 +118,14 @@ getPostQ oTe te = select (
118118getUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG User)
119119getUserQ = selectStar $ from (table #users) & where_ (#userId .== param @1)
120120
121+verifiedUserIdsQ :: [UserId] -> Maybe (Query Schema '[] (RowPG (Only UserId)))
122+verifiedUserIdsQ uIds = case idsToColumn uIds of
123+ Just idQ -> Just $ select (#users ! #userId `as` #fromOnly) $ from (subquery (idQ `As` #ids) &
124+ innerJoin (table #users)
125+ (#ids ! #id .== #users ! #userId))
126+ Nothing -> Nothing
127+
128+
121129getUsersQ :: [UserId] -> Maybe (Query Schema '[] (RowPG User))
122130getUsersQ uIds = case idsToColumn uIds of
123131 Just idQ -> Just $ selectDotStar #users $ from (subquery (idQ `As` #ids) &
@@ -264,6 +272,12 @@ getChannelForUser uId cId = do
264272 (Just c) -> return c
265273 Nothing -> lift $ S.throwError S.err404
266274
275+-- TODO: Reject bad requests. Should be binary.
276+verifiedUsers :: [UserId] -> StaticPQ [UserId]
277+verifiedUsers uIds =
278+ case verifiedUserIdsQ uIds of
279+ Just uIdsQ -> fmap fromOnly <$> (runQuery uIdsQ >>= getRows)
280+ Nothing -> return []
267281
268282-- MARK: FrontEnd functions
269283
@@ -305,13 +319,15 @@ publishPost uId pc = transactionally_ $ do
305319
306320createNewChannel :: UserId -> NamedChannelCreationRequest -> StaticPQ (Maybe NamedChannelId)
307321createNewChannel uId req = transactionally_ $ do
308− cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId req) >>= firstRow
322+ uIds <- verifiedUsers . toList $ namedChannelCreationRequestPeopleIds req
323+ cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId (req {namedChannelCreationRequestPeopleIds = fromList uIds})) >>= firstRow
309324 return $ fmap fromOnly cId
310325
311326updateChannel :: UserId -> NamedChannel UserId -> StaticPQ ()
312327updateChannel uId req = transactionally_ $ do
328+ uIds <- fmap fromList . verifiedUsers . toList $ namedChannelPeopleIds req
313329 _ <- getChannelForUser uId $ namedChannelId req
314− _ <- manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId req
330+ _ <- manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId (req {namedChannelPeopleIds = uIds})
315331 return ()
316332
317333getChannelPosts :: UserId -> Limit -> NamedChannelId -> StaticPQ PostResponse