on master help sign in
Resolved #41.
48f5f57iko
denis/src/Data/Channel/AnonymousChannel.hs+6−1
@@ -24,6 +24,7 @@ import Data.Time.Clock
24import Data.Time.Calendar
24import Data.Time.Calendar
25import qualified Data.Vector as V
25import qualified Data.Vector as V
26import Data.User
26import Data.User
27+import qualified Data.Set as Set
27
28
28
29
29-- MARK: Documentation
30-- MARK: Documentation
@@ -50,5 +51,9 @@ instance ToJSON AnonymousChannel where
50
51
51instance FromJSON AnonymousChannel where
52instance FromJSON AnonymousChannel where
52 parseJSON = withObject "anonymous channel" $ \e ->
53 parseJSON = withObject "anonymous channel" $ \e ->
53− AnonymousChannel <$> e .: "tags" <*> e .: "people"
54+ AnonymousChannel <$> fmap (V.fromList . removeDuplicates) (e .: "tags") <*> fmap removeDuplicates (e .: "people")
55+ where
56+ removeDuplicates :: Ord a => [a] -> [a]
57+ removeDuplicates = Set.toList . Set.fromList
58+
54
59
denis/src/Data/Channel/NamedChannel.hs+10−3
@@ -25,6 +25,7 @@ import Data.Time.Clock
25import Data.Time.Calendar
25import Data.Time.Calendar
26import qualified Data.Vector as V
26import qualified Data.Vector as V
27import Data.User
27import Data.User
28+import qualified Data.Set as Set
28
29
29-- MARK: Documentation
30-- MARK: Documentation
30
31
@@ -54,9 +55,12 @@ instance (ToJSON u) => ToJSON (NamedChannel u) where
54 "tags" .= cTags,
55 "tags" .= cTags,
55 "people" .= cPeople ]
56 "people" .= cPeople ]
56
57
57−instance (FromJSON u) => FromJSON (NamedChannel u) where
58+instance (FromJSON u, Ord u) => FromJSON (NamedChannel u) where
58 parseJSON = withObject "named channel" $ \e ->
59 parseJSON = withObject "named channel" $ \e ->
59− NamedChannel <$> e .: "id" <*> e .: "name" <*> e .: "tags" <*> e .: "people"
60+ NamedChannel <$> e .: "id" <*> e .: "name" <*> fmap removeDuplicates (e .: "tags") <*> fmap removeDuplicates (e .: "people")
61+ where
62+ removeDuplicates :: Ord a => [a] -> V.Vector a
63+ removeDuplicates = V.fromList . Set.toList . Set.fromList
60
64
61-- MARK: NamedChannelCreationRequest
65-- MARK: NamedChannelCreationRequest
62
66
@@ -77,4 +81,7 @@ instance ToJSON NamedChannelCreationRequest where
77
81
78instance FromJSON NamedChannelCreationRequest where
82instance FromJSON NamedChannelCreationRequest where
79 parseJSON = withObject "named channel" $ \e ->
83 parseJSON = withObject "named channel" $ \e ->
80− NamedChannelCreationRequest <$> e .: "name" <*> e .: "tags" <*> e .: "people"
84+ NamedChannelCreationRequest <$> e .: "name" <*> fmap removeDuplicates (e .: "tags") <*> fmap removeDuplicates (e .: "people")
85+ where
86+ removeDuplicates :: Ord a => [a] -> V.Vector a
87+ removeDuplicates = V.fromList . Set.toList . Set.fromList
denis/src/Data/Query.hs+19−3
@@ -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)
120
120
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+
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
266
274
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 []
267
281
268-- MARK: FrontEnd functions
282-- MARK: FrontEnd functions
269
283
@@ -305,13 +319,15 @@ publishPost uId pc = transactionally_ $ do
305
319
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
310
325
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 ()
316
332
317getChannelPosts :: UserId -> Limit -> NamedChannelId -> StaticPQ PostResponse
333getChannelPosts :: UserId -> Limit -> NamedChannelId -> StaticPQ PostResponse