on master help sign in
Resolved #65
commit974b5e1eea70024abf67d256aecb40a6417604fb authoriko <ilyakooo0@gmail.com> dateSun 12 May 2019, 12:50 (7 years ago) parentfc4e27d
4 files changed+36−21
denis/src/Data/Message.hs+6−5 denis/src/Data/Query.hs+16−6 denis/src/Data/Schema.hs+4−4 denis/src/Server/Query/Dialog.hs+10−6
denis/src/Data/Message.hs+6−5
@@ -81,10 +81,10 @@ instance FromJSON Message where
8181
8282data MessageStorage = MessageStorage {
8383 messageStorageId :: MessageId,
84− messageStorageAuthorId :: UserId,
84+ messageStorageAuthorId :: Maybe UserId,
8585 messageStorageDestinationGroupId :: Maybe GroupChatId,
8686 messageStorageDestinationUserId :: Maybe UserId,
87− messageStorageBody :: Jsonb (PostElement Message),
87+ messageStorageBody :: Maybe (Jsonb (PostElement Message)),
8888 messageStorageTime :: UTCTime
8989} deriving GHC.Generic
9090
@@ -97,17 +97,18 @@ infixl 4 <>>
9797f <>> a = f <*> pure a
9898
9999restoreMessage :: 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) =
101101 Message mId aId <$> case (gId, uId) of
102102 (Just gId', Nothing) -> Just $ GroupChatDestination gId'
103103 (Nothing, Just uId') -> Just $ UserChatDestination uId'
104104 _ -> Nothing
105105 <>> pb <>> mt
106+restoreMessage _ = Nothing
106107
107108storeMessage :: Message -> MessageStorage
108109storeMessage (Message mId aId dId pb mt) = case dId of
109− GroupChatDestination gId -> MessageStorage mId aId (Just gId) Nothing (Jsonb pb) mt
110− UserChatDestination uId -> MessageStorage mId aId Nothing (Just uId) (Jsonb pb) mt
110+ GroupChatDestination gId -> MessageStorage mId (Just aId) (Just gId) Nothing (Just (Jsonb pb)) mt
111+ UserChatDestination uId -> MessageStorage mId (Just aId) Nothing (Just uId) (Just (Jsonb pb)) mt
111112
112113instance ToSample MessageCreation where
113114 toSamples _ = samples $ (MessageCreation Nothing (Just 69) <$>
denis/src/Data/Query.hs+16−6
@@ -379,15 +379,26 @@ createMessageQ :: Manipulation
379379createMessageQ = insertRow #messages
380380 (
381381 Default `as` #messageStorageId :*
382− Set (param @1) `as` #messageStorageAuthorId :*
382+ Set (notNull $ param @1) `as` #messageStorageAuthorId :*
383383 Set (param @2) `as` #messageStorageDestinationGroupId :*
384384 Set (param @3) `as` #messageStorageDestinationUserId :*
385− Set (param @4) `as` #messageStorageBody :*
385+ Set (notNull $ param @4) `as` #messageStorageBody :*
386386 Set currentTimestamp `as` #messageStorageTime
387387 )
388388 OnConflictDoRaise
389389 (Returning $ #messageStorageId `as` #fromOnly)
390390
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+
391402createGroupChatQ :: Manipulation Schema (TuplePG GroupChatCreation) (RowPG (Only GroupChatId))
392403createGroupChatQ = insertRow #groupChats
393404 (
@@ -463,7 +474,7 @@ getChatsQ lim dir = select (
463474 (ifThenElse
464475 (#messageStorageAuthorId .== param @1)
465476 #messageStorageDestinationUserId
466− (notNull #messageStorageAuthorId)) `as` #messageUserId) $
477+ #messageStorageAuthorId) `as` #messageUserId) $
467478 from (table #messages) &
468479 where_ (isNotNull #messageStorageDestinationUserId .&&
469480 (#messageStorageAuthorId .== param @1 .||
@@ -974,12 +985,11 @@ sendMessage selfId (MessageCreation gId@Nothing (Just uId) body) = do
974985sendMessage _ _ = lift $ S.throwError S.err400
975986
976987createGroupChat :: UserId -> GroupChatCreation -> StaticPQ GroupChatId
977−createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = do
988+createGroupChat uId (GroupChatCreation (Jsonb chatUsers) name) = commitedTransactionallyUpdate $ do
978989 let chatUsers' = M.insert uId maxChatPermissions chatUsers
979990 (Only chatId) <- manipulateParams createGroupChatQ (GroupChatCreation (Jsonb chatUsers') name)
980991 >>= firstRow >>= fromMaybe500
981− -- TODO: Do somehting with this
982− _ <- sendMessage uId $ MessageCreation (Just chatId) Nothing (Jsonb $ Markdown "henlo")
992+ _ <- manipulateParams createPhantomMessageQ (Only chatId)
983993 return chatId
984994
985995updateGroupChat :: UserId -> GroupChat -> StaticPQ ()
denis/src/Data/Schema.hs+4−4
@@ -151,10 +151,10 @@ type Schema =
151151 ] :=>
152152 '[
153153 "messageStorageId" ::: 'Def :=> 'NotNull 'PGint8,
154− "messageStorageAuthorId" ::: 'NoDef :=> 'NotNull 'PGint8,
154+ "messageStorageAuthorId" ::: 'NoDef :=> 'Null 'PGint8,
155155 "messageStorageDestinationGroupId" ::: 'NoDef :=> 'Null 'PGint8,
156156 "messageStorageDestinationUserId" ::: 'NoDef :=> 'Null 'PGint8,
157− "messageStorageBody" ::: 'NoDef :=> 'NotNull (PG (Jsonb (PostElement Message))),
157+ "messageStorageBody" ::: 'NoDef :=> 'Null (PG (Jsonb (PostElement Message))),
158158 "messageStorageTime" ::: 'NoDef :=> 'NotNull 'PGtimestamptz
159159 ]),
160160 "tokens" ::: 'Table (
@@ -279,10 +279,10 @@ createTables = createTable #faculties (
279279 primaryKey #groupChatId `as` #pk_group_chats
280280 ) >>> createTable #messages (
281281 serial8 `as` #messageStorageId :*
282− notNullable int8 `as` #messageStorageAuthorId :*
282+ nullable int8 `as` #messageStorageAuthorId :*
283283 nullable int8 `as` #messageStorageDestinationGroupId :*
284284 nullable int8 `as` #messageStorageDestinationUserId :*
285− notNullable jsonb `as` #messageStorageBody :*
285+ nullable jsonb `as` #messageStorageBody :*
286286 notNullable timestampWithTimeZone `as` #messageStorageTime
287287 ) (
288288 primaryKey #messageStorageId `as` #pk_messages :*
denis/src/Server/Query/Dialog.hs+10−6
@@ -25,7 +25,7 @@ instance ToSample Dialog where
2525 toSamples _ = samples (take 1 (UserDialog <$> [69] <*> map snd (toSamples Proxy)) <>
2626 (GroupDialog <$> map snd (toSamples Proxy) <*> map snd (toSamples Proxy)))
2727
28−data Dialog = GroupDialog GroupChat Message | UserDialog UserId Message
28+data Dialog = GroupDialog GroupChat (Maybe Message) | UserDialog UserId Message
2929 deriving Show
3030
3131instance ToJSON Dialog where
@@ -44,10 +44,10 @@ instance ToJSON Dialog where
4444
4545data ChatsResponse = ChatsResponse {
4646 chatResponseMessageId :: MessageId,
47− chatResponseMessageAuthorId :: UserId,
47+ chatResponseMessageAuthorId :: Maybe UserId,
4848 chatResponseMessageDestinationGroupId :: Maybe GroupChatId,
4949 chatResponseMessageDestinationUserId :: Maybe UserId,
50− chatResponseMessageBody :: Jsonb (PostElement Message),
50+ chatResponseMessageBody :: Maybe (Jsonb (PostElement Message)),
5151 chatResponseMessageTime :: UTCTime,
5252 chatResponseGroupUsers :: Maybe (Jsonb UserPermissions),
5353 chatResponseGroupName :: Maybe Text
@@ -60,13 +60,17 @@ responseToDialogs :: UserId -> [ChatsResponse] -> Maybe [Dialog]
6060responseToDialogs selfId = sequence . map toDialog
6161 where
6262 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) =
6464 Just $ UserDialog
6565 (if selfId == uId then dId else uId)
6666 (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)) =
6868 Just $ GroupDialog
6969 (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
7175 toDialog _ = Nothing
7276