@@ -52,7 +52,7 @@ import GHC.Generics (Generic)
52import Data.Aeson
52import Data.Aeson
53import Servant.Docs
53import Servant.Docs
54import Data.Proxy
54import Data.Proxy
55−import Data.Vector (Vector, toList)
55+import Data.Vector (Vector, toList, fromList)
56import Data.Text (Text)
56import Data.Text (Text)
57import Data.Channel.NamedChannel
57import Data.Channel.NamedChannel
58import Data.Channel.AnonymousChannel
58import Data.Channel.AnonymousChannel
@@ -118,6 +118,14 @@ getPostQ oTe te = select (
118getUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG User)
118getUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG User)
119getUserQ = selectStar $ from (table #users) & where_ (#userId .== param @1)
119getUserQ = selectStar $ from (table #users) & where_ (#userId .== param @1)
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))
121getUsersQ :: [UserId] -> Maybe (Query Schema '[] (RowPG User))
129getUsersQ :: [UserId] -> Maybe (Query Schema '[] (RowPG User))
122getUsersQ uIds = case idsToColumn uIds of
130getUsersQ uIds = case idsToColumn uIds of
123 Just idQ -> Just $ selectDotStar #users $ from (subquery (idQ `As` #ids) &
131 Just idQ -> Just $ selectDotStar #users $ from (subquery (idQ `As` #ids) &
@@ -264,6 +272,12 @@ getChannelForUser uId cId = do
264 (Just c) -> return c
272 (Just c) -> return c
265 Nothing -> lift $ S.throwError S.err404
273 Nothing -> lift $ S.throwError S.err404
275+-- TODO: Reject bad requests. Should be binary.
276+verifiedUsers :: [UserId] -> StaticPQ [UserId]
278+ case verifiedUserIdsQ uIds of
279+ Just uIdsQ -> fmap fromOnly <$> (runQuery uIdsQ >>= getRows)
280+ Nothing -> return []
268-- MARK: FrontEnd functions
282-- MARK: FrontEnd functions
@@ -305,13 +319,15 @@ publishPost uId pc = transactionally_ $ do
306createNewChannel :: UserId -> NamedChannelCreationRequest -> StaticPQ (Maybe NamedChannelId)
320createNewChannel :: UserId -> NamedChannelCreationRequest -> StaticPQ (Maybe NamedChannelId)
307createNewChannel uId req = transactionally_ $ do
321createNewChannel uId req = transactionally_ $ do
322+ uIds <- verifiedUsers . toList $ namedChannelCreationRequestPeopleIds req
308− cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId req) >>= firstRow
323+ cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId (req {namedChannelCreationRequestPeopleIds = fromList uIds})) >>= firstRow
309 return $ fmap fromOnly cId
324 return $ fmap fromOnly cId
311updateChannel :: UserId -> NamedChannel UserId -> StaticPQ ()
326updateChannel :: UserId -> NamedChannel UserId -> StaticPQ ()
312updateChannel uId req = transactionally_ $ do
327updateChannel uId req = transactionally_ $ do
328+ uIds <- fmap fromList . verifiedUsers . toList $ namedChannelPeopleIds req
313 _ <- getChannelForUser uId $ namedChannelId req
329 _ <- getChannelForUser uId $ namedChannelId req
314− _ <- manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId req
330+ _ <- manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId (req {namedChannelPeopleIds = uIds})
315 return ()
331 return ()
317getChannelPosts :: UserId -> Limit -> NamedChannelId -> StaticPQ PostResponse
333getChannelPosts :: UserId -> Limit -> NamedChannelId -> StaticPQ PostResponse