From bf3c918d9081171cd196a0c825da52d60035576e Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Fri, 5 Sep 2025 16:57:18 +0400 Subject: [PATCH] corrections --- src/Simplex/Chat/Library/Commands.hs | 30 +++++++++----------- src/Simplex/Chat/Store/Groups.hs | 13 ++++++--- src/Simplex/Chat/Store/Messages.hs | 42 +++++++++++++++++----------- 3 files changed, 47 insertions(+), 38 deletions(-) diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index 7301635d83..e1b0779e1f 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -529,8 +529,8 @@ processChatCommand vr nm = \case checkSupportChatAttention :: User -> Chat 'CTGroup -> CM (Chat 'CTGroup) checkSupportChatAttention user groupChat@Chat {chatInfo, chatItems} = case chatInfo of - GroupChat gInfo (Just GCSIMemberSupport {groupMember_ = Just scopeMem}) -> do - case correctedMemAttention scopeMem chatItems of + GroupChat gInfo (Just GCSIMemberSupport {groupMember_ = Just scopeMem@GroupMember {supportChat = Just suppChat}}) -> do + case correctedMemAttention (groupMemberId' scopeMem) suppChat chatItems of Just newMemAttention -> do (gInfo', scopeMem') <- withFastStore' $ \db -> setSupportChatMemberAttention db vr user gInfo scopeMem newMemAttention @@ -538,18 +538,14 @@ processChatCommand vr nm = \case Nothing -> pure groupChat _ -> pure groupChat where - correctedMemAttention :: GroupMember -> [CChatItem 'CTGroup] -> Maybe Int64 - correctedMemAttention scopeMem@GroupMember {supportChat} items = + correctedMemAttention :: GroupMemberId -> GroupSupportChat -> [CChatItem 'CTGroup] -> Maybe Int64 + correctedMemAttention scopeGMId GroupSupportChat {memberAttention} items = let numNewFromMember = fromIntegral . length . takeWhile newFromMember $ reverse items - memAttention = maybe 0 memberAttention supportChat - in - if numNewFromMember /= memAttention - then Just numNewFromMember - else Nothing + in if numNewFromMember == memberAttention then Nothing else Just numNewFromMember where newFromMember :: CChatItem 'CTGroup -> Bool - newFromMember (CChatItem _ ChatItem {chatDir = CIGroupRcv m, meta = CIMeta {itemStatus = CISRcvNew}}) - | groupMemberId' m == groupMemberId' scopeMem = True + newFromMember (CChatItem _ ChatItem {chatDir = CIGroupRcv m, meta = CIMeta {itemStatus = CISRcvNew}}) = + groupMemberId' m == scopeGMId newFromMember _ = False APIGetChatItems pagination search -> withUser $ \user -> do chatItems <- withFastStore $ \db -> getAllChatItems db vr user pagination search @@ -1025,7 +1021,7 @@ processChatCommand vr nm = \case pure $ prefix <> formattedDate <> ext APIUserRead userId -> withUserId userId $ \user -> withFastStore' (`setUserChatsRead` user) >> ok user UserRead -> withUser $ \User {userId} -> processChatCommand vr nm $ APIUserRead userId - APIChatRead chatRef@(ChatRef cType chatId scope) -> withUser $ \_ -> case cType of + APIChatRead chatRef@(ChatRef cType chatId scope_) -> withUser $ \_ -> case cType of CTDirect -> do user <- withFastStore $ \db -> getUserByContactId db chatId ts <- liftIO getCurrentTime @@ -1041,18 +1037,18 @@ processChatCommand vr nm = \case gInfo <- getGroupInfo db vr user chatId pure (user, gInfo) ts <- liftIO getCurrentTime - chatScopeInfo <- mapM (getChatScopeInfo vr user) scope - case chatScopeInfo of + case scope_ of Nothing -> do timedItems <- withFastStore' $ \db -> do - timedItems <- getGroupUnreadTimedItems db user chatId scope + timedItems <- getGroupUnreadTimedItems db user chatId Nothing updateGroupChatItemsRead db user gInfo setGroupChatItemsDeleteAt db user chatId timedItems ts forM_ timedItems $ \(itemId, deleteAt) -> startProximateTimedItemThread user (chatRef, itemId) deleteAt ok user - Just scopeInfo -> do + Just scope -> do + scopeInfo <- getChatScopeInfo vr user scope (gInfo', m', timedItems) <- withFastStore' $ \db -> do - timedItems <- getGroupUnreadTimedItems db user chatId scope + timedItems <- getGroupUnreadTimedItems db user chatId (Just scope) (gInfo', m') <- updateSupportChatItemsRead db vr user gInfo scopeInfo timedItems' <- setGroupChatItemsDeleteAt db user chatId timedItems ts pure (gInfo', m', timedItems') diff --git a/src/Simplex/Chat/Store/Groups.hs b/src/Simplex/Chat/Store/Groups.hs index c46936e6b1..1b218df56d 100644 --- a/src/Simplex/Chat/Store/Groups.hs +++ b/src/Simplex/Chat/Store/Groups.hs @@ -1495,14 +1495,19 @@ decreaseGroupMembersRequireAttention :: DB.Connection -> User -> GroupInfo -> IO decreaseGroupMembersRequireAttention db User {userId} g@GroupInfo {groupId, membersRequireAttention} = do DB.execute db +#if defined(dbPostgres) [sql| UPDATE groups - SET members_require_attention = CASE - WHEN members_require_attention >= 1 THEN members_require_attention - 1 - ELSE 0 - END + SET members_require_attention = GREATEST(0, members_require_attention - 1) WHERE user_id = ? AND group_id = ? |] +#else + [sql| + UPDATE groups + SET members_require_attention = MAX(0, members_require_attention - 1) + WHERE user_id = ? AND group_id = ? + |] +#endif (userId, groupId) pure g {membersRequireAttention = max 0 (membersRequireAttention - 1)} diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index ae2b6f46cc..c152bdc4bf 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -425,15 +425,21 @@ updateChatTsStats db vr user@User {userId} chatDirection chatTs chatStats_ = cas | not nowRequires && didRequire -> do DB.execute db +#if defined(dbPostgres) [sql| UPDATE groups SET chat_ts = ?, - members_require_attention = CASE - WHEN members_require_attention >= 1 THEN members_require_attention - 1 - ELSE 0 - END + members_require_attention = GREATEST(0, members_require_attention - 1) WHERE user_id = ? AND group_id = ? |] +#else + [sql| + UPDATE groups + SET chat_ts = ?, + members_require_attention = MAX(0, members_require_attention - 1) + WHERE user_id = ? AND group_id = ? + |] +#endif (chatTs, userId, groupId) pure $ GroupChat g {membersRequireAttention = max 0 (membersRequireAttention - 1), chatTs = Just chatTs} (Just $ GCSIMemberSupport (Just member')) | otherwise -> do @@ -2172,24 +2178,26 @@ updateGroupScopeUnreadStats db vr user g@GroupInfo {membership} scopeInfo (unrea currentTs <- getCurrentTime DB.execute db +#if defined(dbPostgres) [sql| UPDATE group_members - SET support_chat_items_unread = CASE - WHEN support_chat_items_unread >= ? THEN support_chat_items_unread - ? - ELSE 0 - END, - support_chat_items_member_attention = CASE - WHEN support_chat_items_member_attention >= ? THEN support_chat_items_member_attention - ? - ELSE 0 - END, - support_chat_items_mentions = CASE - WHEN support_chat_items_mentions >= ? THEN support_chat_items_mentions - ? - ELSE 0 - END, + SET support_chat_items_unread = GREATEST(0, support_chat_items_unread - ?), + support_chat_items_member_attention = GREATEST(0, support_chat_items_member_attention - ?), + support_chat_items_mentions = GREATEST(0, support_chat_items_mentions - ?), updated_at = ? WHERE group_member_id = ? |] - (unread, unread, unanswered, unanswered, mentions, mentions, currentTs, groupMemberId) +#else + [sql| + UPDATE group_members + SET support_chat_items_unread = MAX(0, support_chat_items_unread - ?), + support_chat_items_member_attention = MAX(0, support_chat_items_member_attention - ?), + support_chat_items_mentions = MAX(0, support_chat_items_mentions - ?), + updated_at = ? + WHERE group_member_id = ? + |] +#endif + (unread, unanswered, mentions, currentTs, groupMemberId) m_ <- runExceptT $ getGroupMemberById db vr user groupMemberId pure $ either (const m) id m_ -- Left shouldn't happen, but types require it