getChats returns [Chat] with 0-1 item instead of [ChatPreview] (#240)

This commit is contained in:
Evgeny Poberezkin
2022-01-30 21:51:23 +00:00
committed by GitHub
parent 3b19aaf1d1
commit e29ea99d2c
3 changed files with 21 additions and 44 deletions
+1 -1
View File
@@ -117,7 +117,7 @@ data ChatCommand
deriving (Show) deriving (Show)
data ChatResponse data ChatResponse
= CRApiChats {chats :: [AChatPreview]} = CRApiChats {chats :: [AChat]}
| CRApiChat {chat :: AChat} | CRApiChat {chat :: AChat}
| CRNewChatItem {chatItem :: AChatItem} | CRNewChatItem {chatItem :: AChatItem}
| CRCmdAccepted {corr :: CorrId} | CRCmdAccepted {corr :: CorrId}
-23
View File
@@ -146,13 +146,6 @@ instance ToJSON (Chat c) where
toJSON = J.genericToJSON J.defaultOptions toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions toEncoding = J.genericToEncoding J.defaultOptions
data ChatPreview c = ChatPreview {chatInfo :: ChatInfo c, lastChatItem :: Maybe (CChatItem c)}
deriving (Show, Generic)
instance ToJSON (ChatPreview c) where
toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions
data AChat = forall c. AChat (SChatType c) (Chat c) data AChat = forall c. AChat (SChatType c) (Chat c)
deriving instance Show AChat deriving instance Show AChat
@@ -161,22 +154,6 @@ instance ToJSON AChat where
toJSON (AChat _ c) = J.toJSON c toJSON (AChat _ c) = J.toJSON c
toEncoding (AChat _ c) = J.toEncoding c toEncoding (AChat _ c) = J.toEncoding c
-- | type to show the list of chats, with one last message in each
data AChatPreview = forall c. AChatPreview (SChatType c) (ChatInfo c) (Maybe (CChatItem c))
deriving instance Show AChatPreview
instance ToJSON AChatPreview where
toJSON (AChatPreview _ chat ccItem_) = J.toJSON $ JSONAnyChatPreview chat ccItem_
toEncoding (AChatPreview _ chat ccItem_) = J.toEncoding $ J.toJSON $ JSONAnyChatPreview chat ccItem_
data JSONAnyChatPreview c d = JSONAnyChatPreview {chatInfo :: ChatInfo c, chatItem :: Maybe (CChatItem c)}
deriving (Generic)
instance ToJSON (JSONAnyChatPreview c d) where
toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions
-- | type to show a mix of messages from multiple chats -- | type to show a mix of messages from multiple chats
data AChatItem = forall c d. AChatItem (SChatType c) (SMsgDirection d) (ChatInfo c) (ChatItem c d) data AChatItem = forall c d. AChatItem (SChatType c) (SMsgDirection d) (ChatInfo c) (ChatItem c d)
+20 -20
View File
@@ -1823,18 +1823,18 @@ createNewChatItem st userId chatDirection NewChatItem {createdByMsgId, itemSent,
CDGroupSnd GroupInfo {groupId} -> (Nothing, Just groupId, Nothing) CDGroupSnd GroupInfo {groupId} -> (Nothing, Just groupId, Nothing)
CDGroupRcv GroupInfo {groupId} GroupMember {groupMemberId} -> (Nothing, Just groupId, Just groupMemberId) CDGroupRcv GroupInfo {groupId} GroupMember {groupMemberId} -> (Nothing, Just groupId, Just groupMemberId)
getChatPreviews :: MonadUnliftIO m => SQLiteStore -> User -> m [AChatPreview] getChatPreviews :: MonadUnliftIO m => SQLiteStore -> User -> m [AChat]
getChatPreviews st user = getChatPreviews st user =
liftIO . withTransaction st $ \db -> do liftIO . withTransaction st $ \db -> do
directChatPreviews <- getDirectChatPreviews_ db user directChatPreviews <- getDirectChatPreviews_ db user
groupChatPreviews <- getGroupChatPreviews_ db user groupChatPreviews <- getGroupChatPreviews_ db user
pure $ sortOn (Down . ts) (directChatPreviews <> groupChatPreviews) pure $ sortOn (Down . ts) (directChatPreviews <> groupChatPreviews)
where where
ts :: AChatPreview -> UTCTime ts :: AChat -> UTCTime
ts (AChatPreview _ _ Nothing) = UTCTime (fromGregorian 2122 1 29) (secondsToDiffTime 0) -- TODO Contact/GroupInfo createdAt ts (AChat _ (Chat _ [])) = UTCTime (fromGregorian 2122 1 29) (secondsToDiffTime 0) -- TODO Contact/GroupInfo createdAt
ts (AChatPreview _ _ (Just (CChatItem _ (ChatItem _ CIMeta {itemTs} _)))) = itemTs ts (AChat _ (Chat _ (CChatItem _ (ChatItem _ CIMeta {itemTs} _) : _))) = itemTs
getDirectChatPreviews_ :: DB.Connection -> User -> IO [AChatPreview] getDirectChatPreviews_ :: DB.Connection -> User -> IO [AChat]
getDirectChatPreviews_ db User {userId} = do getDirectChatPreviews_ db User {userId} = do
tz <- getCurrentTimeZone tz <- getCurrentTimeZone
map (toDirectChatPreview tz) map (toDirectChatPreview tz)
@@ -1865,13 +1865,13 @@ getDirectChatPreviews_ db User {userId} = do
|] |]
(Only userId) (Only userId)
where where
toDirectChatPreview :: TimeZone -> ContactRow :. MaybeChatItemRow -> AChatPreview toDirectChatPreview :: TimeZone -> ContactRow :. MaybeChatItemRow -> AChat
toDirectChatPreview tz (contactRow :. ciRow_) = toDirectChatPreview tz (contactRow :. ciRow_) =
let contact = toContact' contactRow let contact = toContact' contactRow
ci_ = toMaybeDirectChatItem tz ciRow_ ci_ = toDirectChatItemList tz ciRow_
in AChatPreview SCTDirect (DirectChat contact) ci_ in AChat SCTDirect $ Chat (DirectChat contact) ci_
getGroupChatPreviews_ :: DB.Connection -> User -> IO [AChatPreview] getGroupChatPreviews_ :: DB.Connection -> User -> IO [AChat]
getGroupChatPreviews_ db User {userId, userContactId} = do getGroupChatPreviews_ db User {userId, userContactId} = do
tz <- getCurrentTimeZone tz <- getCurrentTimeZone
map (toGroupChatPreview tz) map (toGroupChatPreview tz)
@@ -1910,12 +1910,12 @@ getGroupChatPreviews_ db User {userId, userContactId} = do
|] |]
(userId, userContactId) (userId, userContactId)
where where
toGroupChatPreview :: TimeZone -> GroupInfoRow :. MaybeGroupChatItemRow -> AChatPreview toGroupChatPreview :: TimeZone -> GroupInfoRow :. MaybeGroupChatItemRow -> AChat
toGroupChatPreview tz (((groupId, localDisplayName, displayName, fullName) :. userMemberRow) :. ciRow_) = toGroupChatPreview tz (((groupId, localDisplayName, displayName, fullName) :. userMemberRow) :. ciRow_) =
let membership = toGroupMember userContactId userMemberRow let membership = toGroupMember userContactId userMemberRow
groupInfo = GroupInfo {groupId, localDisplayName, groupProfile = GroupProfile {displayName, fullName}, membership} groupInfo = GroupInfo {groupId, localDisplayName, groupProfile = GroupProfile {displayName, fullName}, membership}
ci_ = toMaybeGroupChatItem tz userContactId ciRow_ ci_ = toGroupChatItemList tz userContactId ciRow_
in AChatPreview SCTGroup (GroupChat groupInfo) ci_ in AChat SCTGroup $ Chat (GroupChat groupInfo) ci_
getDirectChat :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> m (Chat 'CTDirect) getDirectChat :: StoreMonad m => SQLiteStore -> UserId -> Int64 -> m (Chat 'CTDirect)
getDirectChat st userId contactId = getDirectChat st userId contactId =
@@ -2051,10 +2051,10 @@ toDirectChatItem tz (itemId, itemTs, itemContent, itemText, createdAt) =
ACIContent d@SMDSnd ciContent -> CChatItem d $ ChatItem CIDirectSnd ciMeta ciContent ACIContent d@SMDSnd ciContent -> CChatItem d $ ChatItem CIDirectSnd ciMeta ciContent
ACIContent d@SMDRcv ciContent -> CChatItem d $ ChatItem CIDirectRcv ciMeta ciContent ACIContent d@SMDRcv ciContent -> CChatItem d $ ChatItem CIDirectRcv ciMeta ciContent
toMaybeDirectChatItem :: TimeZone -> MaybeChatItemRow -> Maybe (CChatItem 'CTDirect) toDirectChatItemList :: TimeZone -> MaybeChatItemRow -> [CChatItem 'CTDirect]
toMaybeDirectChatItem tz (Just itemId, Just itemTs, Just itemContent, Just itemText, Just createdAt) = toDirectChatItemList tz (Just itemId, Just itemTs, Just itemContent, Just itemText, Just createdAt) =
Just $ toDirectChatItem tz (itemId, itemTs, itemContent, itemText, createdAt) [toDirectChatItem tz (itemId, itemTs, itemContent, itemText, createdAt)]
toMaybeDirectChatItem _ _ = Nothing toDirectChatItemList _ _ = []
type GroupChatItemRow = ChatItemRow :. MaybeGroupMemberRow type GroupChatItemRow = ChatItemRow :. MaybeGroupMemberRow
@@ -2069,10 +2069,10 @@ toGroupChatItem tz userContactId ((itemId, itemTs, itemContent, itemText, create
(ACIContent d@SMDRcv ciContent, Just member) -> Right $ CChatItem d (ChatItem (CIGroupRcv member) ciMeta ciContent) (ACIContent d@SMDRcv ciContent, Just member) -> Right $ CChatItem d (ChatItem (CIGroupRcv member) ciMeta ciContent)
_ -> Left $ SEBadChatItem itemId _ -> Left $ SEBadChatItem itemId
toMaybeGroupChatItem :: TimeZone -> Int64 -> MaybeGroupChatItemRow -> Maybe (CChatItem 'CTGroup) toGroupChatItemList :: TimeZone -> Int64 -> MaybeGroupChatItemRow -> [CChatItem 'CTGroup]
toMaybeGroupChatItem tz userContactId ((Just itemId, Just itemTs, Just itemContent, Just itemText, Just createdAt) :. memberRow_) = toGroupChatItemList tz userContactId ((Just itemId, Just itemTs, Just itemContent, Just itemText, Just createdAt) :. memberRow_) =
eitherToMaybe $ toGroupChatItem tz userContactId ((itemId, itemTs, itemContent, itemText, createdAt) :. memberRow_) either (const []) (: []) $ toGroupChatItem tz userContactId ((itemId, itemTs, itemContent, itemText, createdAt) :. memberRow_)
toMaybeGroupChatItem _ _ _ = Nothing toGroupChatItemList _ _ _ = []
-- | Saves unique local display name based on passed displayName, suffixed with _N if required. -- | Saves unique local display name based on passed displayName, suffixed with _N if required.
-- This function should be called inside transaction. -- This function should be called inside transaction.