@@ -9,8 +9,7 @@
9 FlexibleInstances,
9 FlexibleInstances,
10 KindSignatures,
10 KindSignatures,
11 DeriveAnyClass,
11 DeriveAnyClass,
12− RecordWildCards,
12+ RecordWildCards #-}
13− ScopedTypeVariables #-}
15{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
14{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
@@ -58,15 +57,7 @@ module Data.Query (
58 tryActivateToken,
57 tryActivateToken,
59 deactivateToken,
58 deactivateToken,
60 searchUsers,
59 searchUsers,
61− searchTags,
60+ searchTags
69− groupChatIsValidForUser
72import Squeal.PostgreSQL
63import Squeal.PostgreSQL
@@ -96,7 +87,7 @@ import Data.Text (Text)
96import qualified Data.Text as T
87import qualified Data.Text as T
97import Data.Channel.NamedChannel
88import Data.Channel.NamedChannel
98import Data.Channel.AnonymousChannel
89import Data.Channel.AnonymousChannel
99−import Data.Maybe (fromMaybe, catMaybes, isJust)
90+import Data.Maybe (fromMaybe)
100import Squeal.PostgreSQL.Render
91import Squeal.PostgreSQL.Render
101import Data.GroupChat
92import Data.GroupChat
102import Data.Message
93import Data.Message
@@ -108,8 +99,6 @@ import Data.Foldable (fold)
108import Data.Char
99import Data.Char
109import qualified Data.Vector as V
100import qualified Data.Vector as V
110import Server.Auth.Token
101import Server.Auth.Token
112−import Data.Text.Validator
114-- MARK: Documentation
103-- MARK: Documentation
@@ -297,40 +286,12 @@ getChannelForUserQ = selectStar $
297 from (table #channels) &
286 from (table #channels) &
298 where_ (#namedChannelFullOwner .== param @1 .&& #namedChannelFullId .== param @2)
287 where_ (#namedChannelFullOwner .== param @1 .&& #namedChannelFullId .== param @2)
300−updateUserQ :: Manipulation Schema (TuplePG (UserUpdateRow)) (RowPG (Only UserId))
301−updateUserQ = update #users (
302− Same `as` #userRowId :*
303− Set (param @2) `as` #userRowFirstName :*
304− Set (param @3) `as` #userRowMiddleName :*
305− Set (param @4) `as` #userRowLastName :*
306− Set (param @5) `as` #userRowFacultyUrl :*
307− Same `as` #userRowEmail :*
308− Same `as` #userRowIsValidated)
309− (#userRowId .== param @1)
310− (Returning $ #userRowId `as` #fromOnly)
312getAllChannelsForUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG NamedChannelFull)
289getAllChannelsForUserQ :: Query Schema (TuplePG (Only UserId)) (RowPG NamedChannelFull)
313getAllChannelsForUserQ = selectStar $
290getAllChannelsForUserQ = selectStar $
314 from (table #channels) &
291 from (table #channels) &
315 where_ (#namedChannelFullOwner .== param @1) &
292 where_ (#namedChannelFullOwner .== param @1) &
316 orderBy [#namedChannelFullId & Desc]
293 orderBy [#namedChannelFullId & Desc]
318−searchChannelsForUserQ :: Query Schema (TuplePG (UserId, Text)) (RowPG NamedChannelFull)
319−searchChannelsForUserQ = selectStar $
320− from (table #channels) &
321− where_ (#namedChannelFullOwner .== param @1) &
323− tsRankCd vector1 query & Desc,
324− tsRankCd vector query & Desc
327− vector = tsVector' (textArrayToText [
328− #namedChannelFullName,
329− unsafeFunction "unnest" #namedChannelFullTags
331− vector1 = tsVector' #namedChannelFullName
332− query = tsQuery' (param @2)
334updateFacultyQ :: Manipulation Schema (TuplePG Faculty) '[]
295updateFacultyQ :: Manipulation Schema (TuplePG Faculty) '[]
335updateFacultyQ = insertRow #faculties
296updateFacultyQ = insertRow #faculties
@@ -361,11 +322,6 @@ createNamedChannelQ = insertRow #channels
361 OnConflictDoRaise
322 OnConflictDoRaise
362 (Returning $ #namedChannelFullId `as` #fromOnly)
323 (Returning $ #namedChannelFullId `as` #fromOnly)
364−countChannelsQ :: Query Schema (TuplePG (Only UserId)) (RowPG (Only Int64))
365−countChannelsQ = select (countStar `as` #fromOnly) (from (table #channels) &
366− where_ (#namedChannelFullOwner .== param @1) &
367− groupBy #namedChannelFullOwner)
369deleteNamedChannelQ :: Manipulation Schema (TuplePG (Only NamedChannelId)) '[]
325deleteNamedChannelQ :: Manipulation Schema (TuplePG (Only NamedChannelId)) '[]
370deleteNamedChannelQ = deleteFrom_ #channels (#namedChannelFullId .== param @1)
326deleteNamedChannelQ = deleteFrom_ #channels (#namedChannelFullId .== param @1)
@@ -409,26 +365,15 @@ createMessageQ :: Manipulation
409createMessageQ = insertRow #messages
365createMessageQ = insertRow #messages
411 Default `as` #messageStorageId :*
367 Default `as` #messageStorageId :*
412− Set (notNull $ param @1) `as` #messageStorageAuthorId :*
368+ Set (param @1) `as` #messageStorageAuthorId :*
413 Set (param @2) `as` #messageStorageDestinationGroupId :*
369 Set (param @2) `as` #messageStorageDestinationGroupId :*
414 Set (param @3) `as` #messageStorageDestinationUserId :*
370 Set (param @3) `as` #messageStorageDestinationUserId :*
415− Set (notNull $ param @4) `as` #messageStorageBody :*
371+ Set (param @4) `as` #messageStorageBody :*
416 Set currentTimestamp `as` #messageStorageTime
372 Set currentTimestamp `as` #messageStorageTime
418 OnConflictDoRaise
374 OnConflictDoRaise
419 (Returning $ #messageStorageId `as` #fromOnly)
375 (Returning $ #messageStorageId `as` #fromOnly)
421−createPhantomMessageQ :: Manipulation Schema (TuplePG (Only GroupChatId)) '[]
422−createPhantomMessageQ = insertRow_ #messages
424− Default `as` #messageStorageId :*
425− Set null_ `as` #messageStorageAuthorId :*
426− Set (notNull $ param @1) `as` #messageStorageDestinationGroupId :*
427− Set null_ `as` #messageStorageDestinationUserId :*
428− Set null_ `as` #messageStorageBody :*
429− Set currentTimestamp `as` #messageStorageTime
432createGroupChatQ :: Manipulation Schema (TuplePG GroupChatCreation) (RowPG (Only GroupChatId))
377createGroupChatQ :: Manipulation Schema (TuplePG GroupChatCreation) (RowPG (Only GroupChatId))
433createGroupChatQ = insertRow #groupChats
378createGroupChatQ = insertRow #groupChats
@@ -504,7 +449,7 @@ getChatsQ lim dir = select (
504 (ifThenElse
449 (ifThenElse
505 (#messageStorageAuthorId .== param @1)
450 (#messageStorageAuthorId .== param @1)
506 #messageStorageDestinationUserId
451 #messageStorageDestinationUserId
507− #messageStorageAuthorId) `as` #messageUserId) $
452+ (notNull #messageStorageAuthorId)) `as` #messageUserId) $
508 from (table #messages) &
453 from (table #messages) &
509 where_ (isNotNull #messageStorageDestinationUserId .&&
454 where_ (isNotNull #messageStorageDestinationUserId .&&
510 (#messageStorageAuthorId .== param @1 .||
455 (#messageStorageAuthorId .== param @1 .||
@@ -571,21 +516,13 @@ getGroupChatForUserQ = selectStar $
571getFacultySearchResultQ :: Query Schema (TuplePG (Only Text)) (RowPG Faculty)
516getFacultySearchResultQ :: Query Schema (TuplePG (Only Text)) (RowPG Faculty)
572getFacultySearchResultQ = selectStar (from (table #faculties) &
517getFacultySearchResultQ = selectStar (from (table #faculties) &
573 where_ (vector @@ query) &
518 where_ (vector @@ query) &
575− tsRankCd vector1 query & Desc,
576− tsRankCd vector2 query & Desc,
577− tsRankCd vector query & Desc
519+ orderBy [tsRankCd vector query & Desc])
580 vector = tsVector "russian" (textArrayToText [
521 vector = tsVector "russian" (textArrayToText [
581 #facultyName,
522 #facultyName,
582 #facultyPath,
523 #facultyPath,
583 #facultyCampusName,
524 #facultyCampusName,
584 (arrayToText #facultyTags)])
525 (arrayToText #facultyTags)])
585− vector1 = tsVector "russian" #facultyName
586− vector2 = tsVector "russian" (textArrayToText [
589 query = tsQuery "russian" (param @1)
526 query = tsQuery "russian" (param @1)
591searchUsersQ :: Query Schema (TuplePG (Only Text)) (RowPG UserRow)
528searchUsersQ :: Query Schema (TuplePG (Only Text)) (RowPG UserRow)
@@ -647,7 +584,7 @@ tsRankCd
647 -> Expression schema from grouping params ('NotNull 'PGtext)
584 -> Expression schema from grouping params ('NotNull 'PGtext)
648 -> Expression schema from grouping params ('NotNull 'PGfloat4)
585 -> Expression schema from grouping params ('NotNull 'PGfloat4)
649tsRankCd vector query = UnsafeExpression $
586tsRankCd vector query = UnsafeExpression $
650− "ts_rank_cd" <> parenthesized (commaSeparated ["ARRAY[1,1,1,1]", renderExpression vector, renderExpression query, "2|4|8|16|32"])
587+ "ts_rank_cd" <> parenthesized (commaSeparated . map renderExpression $ [vector, query])
653 :: ByteString -- ^ Language
590 :: ByteString -- ^ Language
@@ -772,32 +709,10 @@ getVerifiedTokenQ :: Query Schema (TuplePG (UserId, ByteString)) (RowPG Token)
772getVerifiedTokenQ = selectStar (from (table #tokens) &
709getVerifiedTokenQ = selectStar (from (table #tokens) &
773 where_ (#tokenUserId .== param @1 .&&
710 where_ (#tokenUserId .== param @1 .&&
774 #tokenValue .== param @2 .&&
711 #tokenValue .== param @2 .&&
775− isNull #tokenVerificationCode .&&
712+ isNull #tokenVerificationCode))
776− currentTimestamp .< #tokenExpiryDate))
778−killAllTokensQ :: Manipulation Schema (TuplePG (Only UserId)) '[]
779−killAllTokensQ = deleteFrom_ #tokens
780− (#tokenUserId .== param @1)
782−pruneDeadTokensQ :: Manipulation Schema '[] '[]
783−pruneDeadTokensQ = deleteFrom_ #tokens
784− (#tokenExpiryDate .< currentTimestamp)
786−pruneGhostUsersQ :: Manipulation Schema '[] '[]
787−pruneGhostUsersQ = deleteFrom_ #users
788− ((not_ . in_ #userRowId $ select (#tokens ! #tokenUserId `as` #id) (from $ table #tokens)) .&&
789− (not_ . notNull $ #userRowIsValidated))
791-- MARK: FrontEnd Data Structures
714-- MARK: FrontEnd Data Structures
793−instance HasValidatableText UserCreation where
794− validateText UserCreation{..} = all validateText [
795− userCreationFirstName,
796− userCreationMiddleName,
797− userCreationLastName,
798− userCreationUserFaculty,
799− userCreationUserEmail]
801data UserCreation = UserCreation {
716data UserCreation = UserCreation {
802 userCreationFirstName :: Text,
717 userCreationFirstName :: Text,
803 userCreationMiddleName :: Text,
718 userCreationMiddleName :: Text,
@@ -816,18 +731,9 @@ instance ToJSON UserCreation where
818instance FromJSON UserCreation where
733instance FromJSON UserCreation where
819− parseJSON = withObject "user creation" $ \e ->
734+ parseJSON = withObject "named channel" $ \e ->
820 UserCreation <$> e .: "firstName" <*> e .: "middleName" <*> e .: "lastName" <*> e .: "faculty" <*> e .: "email"
735 UserCreation <$> e .: "firstName" <*> e .: "middleName" <*> e .: "lastName" <*> e .: "faculty" <*> e .: "email"
822−data UserUpdateRow = UserUpdateRow {
823− userUpdateRowId :: UserId,
824− userUpdateRowFirstName :: Text,
825− userUpdateRowMiddleName :: Text,
826− userUpdateRowLastName :: Text,
827− userUpdateRowUserFaculty :: FacultyUrl
828−} deriving (GHC.Generic, SOP.Generic, SOP.HasDatatypeInfo)
831data NamedChannelCreation = NamedChannelCreation {
737data NamedChannelCreation = NamedChannelCreation {
832 namedChannelCreationOwner :: UserId,
738 namedChannelCreationOwner :: UserId,
833 namedChannelCreationName :: Text,
739 namedChannelCreationName :: Text,
@@ -863,13 +769,11 @@ getChannelForUser uId cId = do
863 Nothing -> lift $ S.throwError S.err404
769 Nothing -> lift $ S.throwError S.err404
865-- TODO: Reject bad requests. Should be binary.
771-- TODO: Reject bad requests. Should be binary.
866−verifyUsers :: [UserId] -> StaticPQ ()
772+verifiedUsers :: [UserId] -> StaticPQ [UserId]
867−verifyUsers uIds =
773+verifiedUsers uIds =
868 case verifiedUserIdsQ uIds of
774 case verifiedUserIdsQ uIds of
870− (us :: [UserId]) <- fmap fromOnly <$> (runQuery uIdsQ >>= getRows)
775+ Just uIdsQ -> fmap fromOnly <$> (runQuery uIdsQ >>= getRows)
871− when (length us /= length uIds) $ lift $ S.throwError impossibleContent
872− Nothing -> return ()
776+ Nothing -> return []
874deleteGroupChat :: GroupChatId -> StaticPQ ()
778deleteGroupChat :: GroupChatId -> StaticPQ ()
875deleteGroupChat gId = do
779deleteGroupChat gId = do
@@ -931,40 +835,30 @@ getAllUsers :: StaticPQ [User Faculty]
931getAllUsers = fmap rowToUser <$> (runQuery getAllUsersQ >>= getRows)
835getAllUsers = fmap rowToUser <$> (runQuery getAllUsersQ >>= getRows)
933publishPost :: UserId -> PostCreation -> StaticPQ PostId
837publishPost :: UserId -> PostCreation -> StaticPQ PostId
934−publishPost uId pc = commitedTransactionallyUpdate $ do
838+publishPost uId pc = commitedTransactionallyUpdate $ do
935 pId' <- manipulateParams createPostRowQ (uId, postCreationTags pc) >>= firstRow
839 pId' <- manipulateParams createPostRowQ (uId, postCreationTags pc) >>= firstRow
936 case pId' of
840 case pId' of
937 (Just (Only pId)) -> do
841 (Just (Only pId)) -> do
938− traversePrepared_ createPostElementRowsQ $ elementsToRows pId (map stripPostElement $ postCreationBody pc)
842+ traversePrepared_ createPostElementRowsQ $ elementsToRows pId (postCreationBody pc)
939 return pId
843 return pId
940 _ -> lift $ S.throwError S.err404
844 _ -> lift $ S.throwError S.err404
942createNewChannel :: UserId -> NamedChannelCreationRequest -> StaticPQ (Maybe NamedChannelId)
846createNewChannel :: UserId -> NamedChannelCreationRequest -> StaticPQ (Maybe NamedChannelId)
943createNewChannel uId req = commitedTransactionallyUpdate $ do
847createNewChannel uId req = commitedTransactionallyUpdate $ do
944− let uIds = toList $ namedChannelCreationRequestPeopleIds req
848+ uIds <- verifiedUsers . toList $ namedChannelCreationRequestPeopleIds req
946 cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId (req {namedChannelCreationRequestPeopleIds = fromList uIds})) >>= firstRow
849 cId <- manipulateParams createNamedChannelQ (addUserToChannelCreate uId (req {namedChannelCreationRequestPeopleIds = fromList uIds})) >>= firstRow
947 return $ fmap fromOnly cId
850 return $ fmap fromOnly cId
949−channelCountForUser :: UserId -> StaticPQ Int64
950−channelCountForUser uId = (fromMaybe 0 . fmap fromOnly) <$> (runQueryParams countChannelsQ (Only uId) >>= firstRow)
952updateChannel :: UserId -> NamedChannel UserId -> StaticPQ ()
852updateChannel :: UserId -> NamedChannel UserId -> StaticPQ ()
953−updateChannel uId req = do
853+updateChannel uId req = commitedTransactionallyUpdate $ do
954− let uIds = namedChannelPeopleIds req
854+ uIds <- fmap fromList . verifiedUsers . toList $ namedChannelPeopleIds req
955− verifyUsers (toList uIds)
956 _ <- getChannelForUser uId $ namedChannelId req
855 _ <- getChannelForUser uId $ namedChannelId req
957− _ <- commitedTransactionallyUpdate $ manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId (req {namedChannelPeopleIds = uIds})
856+ _ <- manipulateParams updateNamedChannelQ $ addUserToChannelUpdate uId (req {namedChannelPeopleIds = uIds})
958 return ()
857 return ()
960getFaculty :: Text -> StaticPQ Faculty
859getFaculty :: Text -> StaticPQ Faculty
961getFaculty t = runQueryParams getFacultyQ (Only t) >>= firstRow >>= fromMaybe404
860getFaculty t = runQueryParams getFacultyQ (Only t) >>= firstRow >>= fromMaybe404
963−isValidFaculty :: FacultyUrl -> StaticPQ Bool
964−isValidFaculty url = do
965− (faculty :: Maybe Faculty) <- runQueryParams getFacultyQ (Only url) >>= firstRow
966− return $ isJust faculty
968updateFaculty :: Faculty -> StaticPQ ()
862updateFaculty :: Faculty -> StaticPQ ()
969updateFaculty fac = void $ manipulateParams updateFacultyQ fac
863updateFaculty fac = void $ manipulateParams updateFacultyQ fac
@@ -1001,11 +895,12 @@ getAllChannels uId = fmap (map removeUserFromChannel) $
1001 runQueryParams getAllChannelsForUserQ (Only uId) >>= getRows
895 runQueryParams getAllChannelsForUserQ (Only uId) >>= getRows
1003deleteNamedChannel :: UserId -> NamedChannelId -> StaticPQ ()
897deleteNamedChannel :: UserId -> NamedChannelId -> StaticPQ ()
1004−deleteNamedChannel uId cId = do
898+deleteNamedChannel uId cId = commitedTransactionallyUpdate $ do
1005 _ <- getChannelForUser uId cId
899 _ <- getChannelForUser uId cId
1006− _ <- commitedTransactionallyUpdate $ manipulateParams deleteNamedChannelQ (Only cId)
900+ _ <- manipulateParams deleteNamedChannelQ (Only cId)
1007 return ()
901 return ()
903+-- Returns n most popular
1009getTags :: Limit -> StaticPQ [String]
904getTags :: Limit -> StaticPQ [String]
1010getTags lim = map fromOnly <$> (runQuery (getNpopularTagsQ lim) >>= getRows)
905getTags lim = map fromOnly <$> (runQuery (getNpopularTagsQ lim) >>= getRows)
@@ -1040,40 +935,36 @@ getMessagesInGroupChat
1040getMessagesInGroupChat selfId gId mId lim dir = do
935getMessagesInGroupChat selfId gId mId lim dir = do
1041 _ <- getGroupChatForUser selfId gId
936 _ <- getGroupChatForUser selfId gId
1042 resp <- runQueryParams (getGroupMessagesQ lim dir) (gId, mId) >>= getRows
937 resp <- runQueryParams (getGroupMessagesQ lim dir) (gId, mId) >>= getRows
1043− return . catMaybes . map restoreMessage $ resp
938+ (return . sequence . map restoreMessage $ resp) >>= fromMaybe500
1045sendMessage :: UserId -> MessageCreation -> StaticPQ MessageId
940sendMessage :: UserId -> MessageCreation -> StaticPQ MessageId
1046−sendMessage selfId (MessageCreation (Just gId) uId@Nothing (Jsonb body)) = do
941+sendMessage selfId (MessageCreation (Just gId) uId@Nothing body) = do
1047 _ <- getGroupChatForUser selfId gId
942 _ <- getGroupChatForUser selfId gId
1048− (Only mId) <- manipulateParams createMessageQ (selfId, Just gId, uId, Jsonb $ stripPostElement body)
943+ (Only mId) <- manipulateParams createMessageQ (selfId, Just gId, uId, body)
1049 >>= firstRow >>= fromMaybe500
944 >>= firstRow >>= fromMaybe500
1050 return mId
945 return mId
1051−sendMessage selfId (MessageCreation gId@Nothing (Just uId) (Jsonb body)) = do
946+sendMessage selfId (MessageCreation gId@Nothing (Just uId) body) = do
1053− (Only mId) <- manipulateParams createMessageQ (selfId, gId, Just uId, Jsonb $ stripPostElement body)
947+ (Only mId) <- manipulateParams createMessageQ (selfId, gId, Just uId, body)
1054 >>= firstRow >>= fromMaybe500
948 >>= firstRow >>= fromMaybe500
1055 return mId
949 return mId
1056sendMessage _ _ = lift $ S.throwError S.err400
950sendMessage _ _ = lift $ S.throwError S.err400
1058createGroupChat :: UserId -> GroupChatCreation -> StaticPQ GroupChatId
952createGroupChat :: UserId -> GroupChatCreation -> StaticPQ GroupChatId
1059createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = do
953createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = do
1060− verifyUsers . M.keys $ chatUsers
1061− commitedTransactionallyUpdate $ do
1062− let chatUsers' = M.insert uId maxChatPermissions chatUsers
954+ let chatUsers' = M.insert uId maxChatPermissions chatUsers
1063− (Only chatId) <- manipulateParams createGroupChatQ (GroupChatCreation (Jsonb chatUsers') name)
955+ (Only chatId) <- manipulateParams createGroupChatQ (GroupChatCreation (Jsonb chatUsers') name)
1064− >>= firstRow >>= fromMaybe500
956+ >>= firstRow >>= fromMaybe500
1065− _ <- manipulateParams createPhantomMessageQ (Only chatId)
957+ -- TODO: Do somehting with this
958+ _ <- sendMessage uId $ MessageCreation (Just chatId) Nothing (Jsonb $ Markdown "henlo")
1066− return chatId
959+ return chatId
1068updateGroupChat :: UserId -> GroupChat -> StaticPQ ()
961updateGroupChat :: UserId -> GroupChat -> StaticPQ ()
1069−updateGroupChat uId (GroupChat gId (Jsonb perms) name) = do
962+updateGroupChat uId (GroupChat gId (Jsonb perms) name) = commitedTransactionallyUpdate $ do
1070− verifyUsers . M.keys $ perms
1071− commitedTransactionallyUpdate $ do
1072− (GroupChat _ (Jsonb perms') _) <- getGroupChatForUser uId gId
963+ (GroupChat _ (Jsonb perms') _) <- getGroupChatForUser uId gId
1073− let userPerm = fromMaybe minChatPermissions $ M.lookup uId perms'
964+ let userPerm = fromMaybe minChatPermissions $ M.lookup uId perms'
1074− unless (isAdmin userPerm) $ lift $ S.throwError $ S.err401
965+ unless (isAdmin userPerm) $ lift $ S.throwError $ S.err401
1075− let newPerms = M.insert uId userPerm perms
966+ let newPerms = M.insert uId userPerm perms
1076− void $ manipulateParams updateGroupChatQ (GroupChat gId (Jsonb newPerms) name)
967+ void $ manipulateParams updateGroupChatQ (GroupChat gId (Jsonb newPerms) name)
1078leaveGroupChat :: UserId -> GroupChatId -> StaticPQ ()
969leaveGroupChat :: UserId -> GroupChatId -> StaticPQ ()
1079leaveGroupChat uId gId = commitedTransactionallyUpdate $ do
970leaveGroupChat uId gId = commitedTransactionallyUpdate $ do
@@ -1090,24 +981,15 @@ getGroupChatForUser uId gId = do
1090 (Just c) -> return c
981 (Just c) -> return c
1091 Nothing -> lift $ S.throwError S.err404
982 Nothing -> lift $ S.throwError S.err404
1093−groupChatIsValidForUser :: UserId -> GroupChatId -> StaticPQ Bool
1094−groupChatIsValidForUser uId gId = do
1095− (chat :: Maybe GroupChat) <- runQueryParams getGroupChatForUserQ (show uId, gId) >>= firstRow
1096− return $ isJust chat
1098getFacultyFromQuery :: T.Text -> StaticPQ [Faculty]
984getFacultyFromQuery :: T.Text -> StaticPQ [Faculty]
1099getFacultyFromQuery query =
985getFacultyFromQuery query =
1100 runQueryParams getFacultySearchResultQ (Only . queryFromTextAbbr $ query) >>= getRows
986 runQueryParams getFacultySearchResultQ (Only . queryFromTextAbbr $ query) >>= getRows
1102searchUsers :: T.Text -> StaticPQ [User Faculty]
988searchUsers :: T.Text -> StaticPQ [User Faculty]
1103−searchUsers query = do
989+searchUsers query =
1104− unless (validateText query) $ lift $ throwError lengthExceeded
1105− if (T.null . T.filter isAlpha $ query)
1107− else map rowToUser <$> (runQueryParams searchUsersQ (Only . queryFromText $ query) >>= getRows)
990+ map rowToUser <$> (runQueryParams searchUsersQ (Only . queryFromText $ query) >>= getRows)
1109searchTags :: T.Text -> StaticPQ [Text]
992searchTags :: T.Text -> StaticPQ [Text]
1110−searchTags "" = return []
1111searchTags query = map fromOnly <$>
993searchTags query = map fromOnly <$>
1112 (runQueryParams searchTagsQ
994 (runQueryParams searchTagsQ
1113 (Only . (<> ":*") . T.strip $ query) >>= getRows)
995 (Only . (<> ":*") . T.strip $ query) >>= getRows)
@@ -1115,18 +997,6 @@ searchTags query = map fromOnly <$>
1115createToken :: Token -> StaticPQ ()
997createToken :: Token -> StaticPQ ()
1116createToken token = void $ manipulateParams createTokenQ token
998createToken token = void $ manipulateParams createTokenQ token
1118−killAllTokens :: UserId -> StaticPQ ()
1119−killAllTokens = void . manipulateParams killAllTokensQ . Only
1121−pruneAuth :: StaticPQ ()
1123− _ <- commitedTransactionallyUpdate . manipulate $ pruneDeadTokensQ
1124− _ <- commitedTransactionallyUpdate . manipulate $ pruneGhostUsersQ
1127−searchChannels :: UserId -> Text -> StaticPQ [NamedChannel UserId]
1128−searchChannels uId query = fmap (map removeUserFromChannel) $
1129− runQueryParams searchChannelsForUserQ (uId, queryFromText query) >>= getRows
1131-- NOTE: Not transactional
1001-- NOTE: Not transactional
1132validateToken :: Token -> StaticPQ ()
1002validateToken :: Token -> StaticPQ ()
@@ -1171,20 +1041,8 @@ deactivateToken uId dData = fmap fromOnly <$> (manipulateParams deactivateTokenQ
1171getVerifiedToken :: UserId -> ByteString -> StaticPQ (Maybe Token)
1041getVerifiedToken :: UserId -> ByteString -> StaticPQ (Maybe Token)
1172getVerifiedToken uId tokenData = runQueryParams getVerifiedTokenQ (uId, hash tokenData) >>= firstRow
1042getVerifiedToken uId tokenData = runQueryParams getVerifiedTokenQ (uId, hash tokenData) >>= firstRow
1174−updateUser :: (UserUpdateRow) -> StaticPQ (Maybe UserId)
1175−updateUser user = fmap fromOnly <$> (manipulateParams updateUserQ (normalizeUser user) >>= firstRow)
1177-- MARK: Utils
1044-- MARK: Utils
1179−normalizeUser :: UserUpdateRow -> UserUpdateRow
1180−normalizeUser UserUpdateRow{..} = UserUpdateRow {
1181− userUpdateRowId = userUpdateRowId,
1182− userUpdateRowFirstName = T.strip $ userUpdateRowFirstName,
1183− userUpdateRowMiddleName = T.strip $ userUpdateRowMiddleName,
1184− userUpdateRowLastName = T.strip $ userUpdateRowLastName,
1185− userUpdateRowUserFaculty = T.toLower . T.strip $ userUpdateRowUserFaculty
1188data UserRow = UserRow {
1046data UserRow = UserRow {
1189 userRowId :: UserId,
1047 userRowId :: UserId,
1190 userRowFirstName :: Text,
1048 userRowFirstName :: Text,
@@ -1231,7 +1089,7 @@ queryFromTextAbbr = T.unwords . L.intersperse "&" . map processTerm . filter (no
1231 ) <> ")"
1089 ) <> ")"
1233queryFromText :: Text -> Text
1091queryFromText :: Text -> Text
1234−queryFromText = T.unwords . L.intersperse "&" . map processTerm . filter (not . T.null) . T.split (not . isPrint)
1092+queryFromText = T.unwords . L.intersperse "&" . map processTerm . filter (not . T.null) . T.split (not . isAlphaNum)
1236 processTerm :: Text -> Text
1094 processTerm :: Text -> Text
1237 processTerm t = t <> ":*"
1095 processTerm t = t <> ":*"