on master help sign in
Resolved #65
974b5e1iko
denis/src/Data/Message.hs+6−5
@@ -81,10 +81,10 @@ instance FromJSON Message where
81
81
82data MessageStorage = MessageStorage {
82data MessageStorage = MessageStorage {
83 messageStorageId :: MessageId,
83 messageStorageId :: MessageId,
84− messageStorageAuthorId :: UserId,
84+ messageStorageAuthorId :: Maybe UserId,
85 messageStorageDestinationGroupId :: Maybe GroupChatId,
85 messageStorageDestinationGroupId :: Maybe GroupChatId,
86 messageStorageDestinationUserId :: Maybe UserId,
86 messageStorageDestinationUserId :: Maybe UserId,
87− messageStorageBody :: Jsonb (PostElement Message),
87+ messageStorageBody :: Maybe (Jsonb (PostElement Message)),
88 messageStorageTime :: UTCTime
88 messageStorageTime :: UTCTime
89} deriving GHC.Generic
89} deriving GHC.Generic
90
90
@@ -97,17 +97,18 @@ infixl 4 <>>
97f <>> a = f <*> pure a
97f <>> a = f <*> pure a
98
98
99restoreMessage :: MessageStorage -> Maybe Message
99restoreMessage :: MessageStorage -> Maybe Message
100−restoreMessage (MessageStorage mId aId gId uId (Jsonb pb) mt) =
100+restoreMessage (MessageStorage mId (Just aId) gId uId (Just (Jsonb pb)) mt) =
101 Message mId aId <$> case (gId, uId) of
101 Message mId aId <$> case (gId, uId) of
102 (Just gId', Nothing) -> Just $ GroupChatDestination gId'
102 (Just gId', Nothing) -> Just $ GroupChatDestination gId'
103 (Nothing, Just uId') -> Just $ UserChatDestination uId'
103 (Nothing, Just uId') -> Just $ UserChatDestination uId'
104 _ -> Nothing
104 _ -> Nothing
105 <>> pb <>> mt
105 <>> pb <>> mt
106+restoreMessage _ = Nothing
106
107
107storeMessage :: Message -> MessageStorage
108storeMessage :: Message -> MessageStorage
108storeMessage (Message mId aId dId pb mt) = case dId of
109storeMessage (Message mId aId dId pb mt) = case dId of
109− GroupChatDestination gId -> MessageStorage mId aId (Just gId) Nothing (Jsonb pb) mt
110+ GroupChatDestination gId -> MessageStorage mId (Just aId) (Just gId) Nothing (Just (Jsonb pb)) mt
110− UserChatDestination uId -> MessageStorage mId aId Nothing (Just uId) (Jsonb pb) mt
111+ UserChatDestination uId -> MessageStorage mId (Just aId) Nothing (Just uId) (Just (Jsonb pb)) mt
111
112
112instance ToSample MessageCreation where
113instance ToSample MessageCreation where
113 toSamples _ = samples $ (MessageCreation Nothing (Just 69) <$>
114 toSamples _ = samples $ (MessageCreation Nothing (Just 69) <$>
denis/src/Data/Query.hs+16−6
@@ -379,15 +379,26 @@ createMessageQ :: Manipulation
379createMessageQ = insertRow #messages
379createMessageQ = insertRow #messages
380 (
380 (
381 Default `as` #messageStorageId :*
381 Default `as` #messageStorageId :*
382− Set (param @1) `as` #messageStorageAuthorId :*
382+ Set (notNull $ param @1) `as` #messageStorageAuthorId :*
383 Set (param @2) `as` #messageStorageDestinationGroupId :*
383 Set (param @2) `as` #messageStorageDestinationGroupId :*
384 Set (param @3) `as` #messageStorageDestinationUserId :*
384 Set (param @3) `as` #messageStorageDestinationUserId :*
385− Set (param @4) `as` #messageStorageBody :*
385+ Set (notNull $ param @4) `as` #messageStorageBody :*
386 Set currentTimestamp `as` #messageStorageTime
386 Set currentTimestamp `as` #messageStorageTime
387 )
387 )
388 OnConflictDoRaise
388 OnConflictDoRaise
389 (Returning $ #messageStorageId `as` #fromOnly)
389 (Returning $ #messageStorageId `as` #fromOnly)
390
390
391+createPhantomMessageQ :: Manipulation Schema (TuplePG (Only GroupChatId)) '[]
392+createPhantomMessageQ = insertRow_ #messages
393+ (
394+ Default `as` #messageStorageId :*
395+ Set null_ `as` #messageStorageAuthorId :*
396+ Set (notNull $ param @1) `as` #messageStorageDestinationGroupId :*
397+ Set null_ `as` #messageStorageDestinationUserId :*
398+ Set null_ `as` #messageStorageBody :*
399+ Set currentTimestamp `as` #messageStorageTime
400+ )
401+
391createGroupChatQ :: Manipulation Schema (TuplePG GroupChatCreation) (RowPG (Only GroupChatId))
402createGroupChatQ :: Manipulation Schema (TuplePG GroupChatCreation) (RowPG (Only GroupChatId))
392createGroupChatQ = insertRow #groupChats
403createGroupChatQ = insertRow #groupChats
393 (
404 (
@@ -463,7 +474,7 @@ getChatsQ lim dir = select (
463 (ifThenElse
474 (ifThenElse
464 (#messageStorageAuthorId .== param @1)
475 (#messageStorageAuthorId .== param @1)
465 #messageStorageDestinationUserId
476 #messageStorageDestinationUserId
466− (notNull #messageStorageAuthorId)) `as` #messageUserId) $
477+ #messageStorageAuthorId) `as` #messageUserId) $
467 from (table #messages) &
478 from (table #messages) &
468 where_ (isNotNull #messageStorageDestinationUserId .&&
479 where_ (isNotNull #messageStorageDestinationUserId .&&
469 (#messageStorageAuthorId .== param @1 .||
480 (#messageStorageAuthorId .== param @1 .||
@@ -974,12 +985,11 @@ sendMessage selfId (MessageCreation gId@Nothing (Just uId) body) = do
974sendMessage _ _ = lift $ S.throwError S.err400
985sendMessage _ _ = lift $ S.throwError S.err400
975
986
976createGroupChat :: UserId -> GroupChatCreation -> StaticPQ GroupChatId
987createGroupChat :: UserId -> GroupChatCreation -> StaticPQ GroupChatId
977−createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = do
988+createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = commitedTransactionallyUpdate $ do
978 let chatUsers' = M.insert uId maxChatPermissions chatUsers
989 let chatUsers' = M.insert uId maxChatPermissions chatUsers
979 (Only chatId) <- manipulateParams createGroupChatQ (GroupChatCreation (Jsonb chatUsers') name)
990 (Only chatId) <- manipulateParams createGroupChatQ (GroupChatCreation (Jsonb chatUsers') name)
980 >>= firstRow >>= fromMaybe500
991 >>= firstRow >>= fromMaybe500
981− -- TODO: Do somehting with this
992+ _ <- manipulateParams createPhantomMessageQ (Only chatId)
982− _ <- sendMessage uId $ MessageCreation (Just chatId) Nothing (Jsonb $ Markdown "henlo")
983 return chatId
993 return chatId
984
994
985updateGroupChat :: UserId -> GroupChat -> StaticPQ ()
995updateGroupChat :: UserId -> GroupChat -> StaticPQ ()
denis/src/Data/Schema.hs+4−4
@@ -151,10 +151,10 @@ type Schema =
151 ] :=>
151 ] :=>
152 '[
152 '[
153 "messageStorageId" ::: 'Def :=> 'NotNull 'PGint8,
153 "messageStorageId" ::: 'Def :=> 'NotNull 'PGint8,
154− "messageStorageAuthorId" ::: 'NoDef :=> 'NotNull 'PGint8,
154+ "messageStorageAuthorId" ::: 'NoDef :=> 'Null 'PGint8,
155 "messageStorageDestinationGroupId" ::: 'NoDef :=> 'Null 'PGint8,
155 "messageStorageDestinationGroupId" ::: 'NoDef :=> 'Null 'PGint8,
156 "messageStorageDestinationUserId" ::: 'NoDef :=> 'Null 'PGint8,
156 "messageStorageDestinationUserId" ::: 'NoDef :=> 'Null 'PGint8,
157− "messageStorageBody" ::: 'NoDef :=> 'NotNull (PG (Jsonb (PostElement Message))),
157+ "messageStorageBody" ::: 'NoDef :=> 'Null (PG (Jsonb (PostElement Message))),
158 "messageStorageTime" ::: 'NoDef :=> 'NotNull 'PGtimestamptz
158 "messageStorageTime" ::: 'NoDef :=> 'NotNull 'PGtimestamptz
159 ]),
159 ]),
160 "tokens" ::: 'Table (
160 "tokens" ::: 'Table (
@@ -279,10 +279,10 @@ createTables = createTable #faculties (
279 primaryKey #groupChatId `as` #pk_group_chats
279 primaryKey #groupChatId `as` #pk_group_chats
280 ) >>> createTable #messages (
280 ) >>> createTable #messages (
281 serial8 `as` #messageStorageId :*
281 serial8 `as` #messageStorageId :*
282− notNullable int8 `as` #messageStorageAuthorId :*
282+ nullable int8 `as` #messageStorageAuthorId :*
283 nullable int8 `as` #messageStorageDestinationGroupId :*
283 nullable int8 `as` #messageStorageDestinationGroupId :*
284 nullable int8 `as` #messageStorageDestinationUserId :*
284 nullable int8 `as` #messageStorageDestinationUserId :*
285− notNullable jsonb `as` #messageStorageBody :*
285+ nullable jsonb `as` #messageStorageBody :*
286 notNullable timestampWithTimeZone `as` #messageStorageTime
286 notNullable timestampWithTimeZone `as` #messageStorageTime
287 ) (
287 ) (
288 primaryKey #messageStorageId `as` #pk_messages :*
288 primaryKey #messageStorageId `as` #pk_messages :*
denis/src/Server/Query/Dialog.hs+10−6
@@ -25,7 +25,7 @@ instance ToSample Dialog where
25 toSamples _ = samples (take 1 (UserDialog <$> [69] <*> map snd (toSamples Proxy)) <>
25 toSamples _ = samples (take 1 (UserDialog <$> [69] <*> map snd (toSamples Proxy)) <>
26 (GroupDialog <$> map snd (toSamples Proxy) <*> map snd (toSamples Proxy)))
26 (GroupDialog <$> map snd (toSamples Proxy) <*> map snd (toSamples Proxy)))
27
27
28−data Dialog = GroupDialog GroupChat Message | UserDialog UserId Message
28+data Dialog = GroupDialog GroupChat (Maybe Message) | UserDialog UserId Message
29 deriving Show
29 deriving Show
30
30
31instance ToJSON Dialog where
31instance ToJSON Dialog where
@@ -44,10 +44,10 @@ instance ToJSON Dialog where
44
44
45data ChatsResponse = ChatsResponse {
45data ChatsResponse = ChatsResponse {
46 chatResponseMessageId :: MessageId,
46 chatResponseMessageId :: MessageId,
47− chatResponseMessageAuthorId :: UserId,
47+ chatResponseMessageAuthorId :: Maybe UserId,
48 chatResponseMessageDestinationGroupId :: Maybe GroupChatId,
48 chatResponseMessageDestinationGroupId :: Maybe GroupChatId,
49 chatResponseMessageDestinationUserId :: Maybe UserId,
49 chatResponseMessageDestinationUserId :: Maybe UserId,
50− chatResponseMessageBody :: Jsonb (PostElement Message),
50+ chatResponseMessageBody :: Maybe (Jsonb (PostElement Message)),
51 chatResponseMessageTime :: UTCTime,
51 chatResponseMessageTime :: UTCTime,
52 chatResponseGroupUsers :: Maybe (Jsonb UserPermissions),
52 chatResponseGroupUsers :: Maybe (Jsonb UserPermissions),
53 chatResponseGroupName :: Maybe Text
53 chatResponseGroupName :: Maybe Text
@@ -60,13 +60,17 @@ responseToDialogs :: UserId -> [ChatsResponse] -> Maybe [Dialog]
60responseToDialogs selfId = sequence . map toDialog
60responseToDialogs selfId = sequence . map toDialog
61 where
61 where
62 toDialog :: ChatsResponse -> Maybe Dialog
62 toDialog :: ChatsResponse -> Maybe Dialog
63− toDialog (ChatsResponse mId uId Nothing (Just dId) (Jsonb mb) mt Nothing Nothing) =
63+ toDialog (ChatsResponse mId (Just uId) Nothing (Just dId) (Just (Jsonb mb)) mt Nothing Nothing) =
64 Just $ UserDialog
64 Just $ UserDialog
65 (if selfId == uId then dId else uId)
65 (if selfId == uId then dId else uId)
66 (Message mId uId (UserChatDestination dId) mb mt)
66 (Message mId uId (UserChatDestination dId) mb mt)
67− toDialog (ChatsResponse mId uId (Just gId) Nothing (Jsonb mb) mt (Just users) (Just name)) =
67+ toDialog (ChatsResponse mId (Just uId) (Just gId) Nothing (Just (Jsonb mb)) mt (Just users) (Just name)) =
68 Just $ GroupDialog
68 Just $ GroupDialog
69 (GroupChat gId users name)
69 (GroupChat gId users name)
70− (Message mId uId (GroupChatDestination gId) mb mt)
70+ (Just $ Message mId uId (GroupChatDestination gId) mb mt)
71+ toDialog (ChatsResponse _ Nothing (Just gId) Nothing Nothing _ (Just users) (Just name)) =
72+ Just $ GroupDialog
73+ (GroupChat gId users name)
74+ Nothing
71 toDialog _ = Nothing
75 toDialog _ = Nothing
72
76