From 49db4661634b364ea49758666623a2efc3ac7107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 18 May 2024 21:01:59 +0200 Subject: Chatroom messages --- main/Test.hs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'main/Test.hs') diff --git a/main/Test.hs b/main/Test.hs index 5e89c66..d16e141 100644 --- a/main/Test.hs +++ b/main/Test.hs @@ -269,6 +269,7 @@ commands = map (T.pack *** id) , ("chatroom-list-local", cmdChatroomListLocal) , ("chatroom-watch-local", cmdChatroomWatchLocal) , ("chatroom-set-name", cmdChatroomSetName) + , ("chatroom-message-send", cmdChatroomMessageSend) ] cmdStore :: Command @@ -628,14 +629,30 @@ cmdChatroomWatchLocal = do Just diff -> forM_ diff $ \case AddedChatroom room -> outLine out $ unwords $ "chatroom-watched-added" : chatroomInfo room RemovedChatroom room -> outLine out $ unwords $ "chatroom-watched-removed" : chatroomInfo room - UpdatedChatroom oldroom room -> outLine out $ unwords $ concat - [ [ "chatroom-watched-updated" ], chatroomInfo room - , [ "old" ], map (show . refDigest . storedRef) (roomStateData oldroom) - , [ "new" ], map (show . refDigest . storedRef) (roomStateData room) - ] + UpdatedChatroom oldroom room -> do + when (any (not . null . rsdRoom . fromStored) (roomStateData room)) $ do + outLine out $ unwords $ concat + [ [ "chatroom-watched-updated" ], chatroomInfo room + , [ "old" ], map (show . refDigest . storedRef) (roomStateData oldroom) + , [ "new" ], map (show . refDigest . storedRef) (roomStateData room) + ] + when (any (not . null . rsdMessages . fromStored) (roomStateData room)) $ do + forM_ (getMessagesSinceState room oldroom) $ \msg -> do + outLine out $ unwords $ concat + [ [ "chatroom-message-new" ] + , [ show . refDigest . storedRef . head . filterAncestors . concatMap storedRoots . toComponents $ room ] + , [ "from", maybe "" T.unpack $ idName $ cmsgFrom msg ] + , maybe [] (("text":) . (:[]) . T.unpack) $ cmsgText msg + ] chatroomInfo :: ChatroomState -> [String] chatroomInfo room = [ show . refDigest . storedRef . head . filterAncestors . concatMap storedRoots . toComponents $ room , maybe "" T.unpack $ roomName =<< roomStateRoom room ] + +cmdChatroomMessageSend :: Command +cmdChatroomMessageSend = do + [cid, msg] <- asks tiParams + to <- getChatroomStateData cid + void $ chatroomMessageByStateData to msg -- cgit v1.2.3