core: api to load all chat items
This commit is contained in:
parent
b8298aa458
commit
41d7a47b37
@ -284,7 +284,8 @@ processChatCommand = \case
|
|||||||
CTGroup -> CRApiChat . AChat SCTGroup <$> withStore (\db -> getGroupChat db user cId pagination search)
|
CTGroup -> CRApiChat . AChat SCTGroup <$> withStore (\db -> getGroupChat db user cId pagination search)
|
||||||
CTContactRequest -> pure $ chatCmdError "not implemented"
|
CTContactRequest -> pure $ chatCmdError "not implemented"
|
||||||
CTContactConnection -> pure $ chatCmdError "not supported"
|
CTContactConnection -> pure $ chatCmdError "not supported"
|
||||||
APIGetChatItems _pagination -> pure $ chatCmdError "not implemented"
|
APIGetChatItems pagination search -> withUser $ \user -> withStore $ \db ->
|
||||||
|
CRApiChatItems <$> getAllChatItems db user pagination search
|
||||||
APISendMessage (ChatRef cType chatId) (ComposedMessage file_ quotedItemId_ mc) -> withUser $ \user@User {userId} -> withChatLock "sendMessage" $ case cType of
|
APISendMessage (ChatRef cType chatId) (ComposedMessage file_ quotedItemId_ mc) -> withUser $ \user@User {userId} -> withChatLock "sendMessage" $ case cType of
|
||||||
CTDirect -> do
|
CTDirect -> do
|
||||||
ct@Contact {localDisplayName = c, contactUsed} <- withStore $ \db -> getContact db user chatId
|
ct@Contact {localDisplayName = c, contactUsed} <- withStore $ \db -> getContact db user chatId
|
||||||
@ -1035,9 +1036,9 @@ processChatCommand = \case
|
|||||||
processChatCommand . APISendMessage (ChatRef CTGroup groupId) $ ComposedMessage Nothing (Just quotedItemId) mc
|
processChatCommand . APISendMessage (ChatRef CTGroup groupId) $ ComposedMessage Nothing (Just quotedItemId) mc
|
||||||
LastMessages (Just chatName) count search -> withUser $ \user -> do
|
LastMessages (Just chatName) count search -> withUser $ \user -> do
|
||||||
chatRef <- getChatRef user chatName
|
chatRef <- getChatRef user chatName
|
||||||
CRLastMessages . aChatItems . chat <$> processChatCommand (APIGetChat chatRef (CPLast count) search)
|
CRApiChatItems . aChatItems . chat <$> processChatCommand (APIGetChat chatRef (CPLast count) search)
|
||||||
LastMessages Nothing count search -> withUser $ \user -> withStore $ \db ->
|
LastMessages Nothing count search ->
|
||||||
CRLastMessages <$> getAllChatItems db user (CPLast count) search
|
processChatCommand (APIGetChatItems (CPLast count) search)
|
||||||
SendFile chatName f -> withUser $ \user -> do
|
SendFile chatName f -> withUser $ \user -> do
|
||||||
chatRef <- getChatRef user chatName
|
chatRef <- getChatRef user chatName
|
||||||
processChatCommand . APISendMessage chatRef $ ComposedMessage (Just f) Nothing (MCFile "")
|
processChatCommand . APISendMessage chatRef $ ComposedMessage (Just f) Nothing (MCFile "")
|
||||||
@ -3182,7 +3183,7 @@ chatCommandP =
|
|||||||
"/sql agent " *> (ExecAgentStoreSQL <$> textP),
|
"/sql agent " *> (ExecAgentStoreSQL <$> textP),
|
||||||
"/_get chats" *> (APIGetChats <$> (" pcc=on" $> True <|> " pcc=off" $> False <|> pure False)),
|
"/_get chats" *> (APIGetChats <$> (" pcc=on" $> True <|> " pcc=off" $> False <|> pure False)),
|
||||||
"/_get chat " *> (APIGetChat <$> chatRefP <* A.space <*> chatPaginationP <*> optional (" search=" *> stringP)),
|
"/_get chat " *> (APIGetChat <$> chatRefP <* A.space <*> chatPaginationP <*> optional (" search=" *> stringP)),
|
||||||
"/_get items count=" *> (APIGetChatItems <$> A.decimal),
|
"/_get items " *> (APIGetChatItems <$> chatPaginationP <*> optional (" search=" *> stringP)),
|
||||||
"/_send " *> (APISendMessage <$> chatRefP <*> (" json " *> jsonP <|> " text " *> (ComposedMessage Nothing Nothing <$> mcTextP))),
|
"/_send " *> (APISendMessage <$> chatRefP <*> (" json " *> jsonP <|> " text " *> (ComposedMessage Nothing Nothing <$> mcTextP))),
|
||||||
"/_update item " *> (APIUpdateChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> msgContentP),
|
"/_update item " *> (APIUpdateChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> msgContentP),
|
||||||
"/_delete item " *> (APIDeleteChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> ciDeleteMode),
|
"/_delete item " *> (APIDeleteChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> ciDeleteMode),
|
||||||
|
@ -147,7 +147,7 @@ data ChatCommand
|
|||||||
| ExecAgentStoreSQL Text
|
| ExecAgentStoreSQL Text
|
||||||
| APIGetChats {pendingConnections :: Bool}
|
| APIGetChats {pendingConnections :: Bool}
|
||||||
| APIGetChat ChatRef ChatPagination (Maybe String)
|
| APIGetChat ChatRef ChatPagination (Maybe String)
|
||||||
| APIGetChatItems Int
|
| APIGetChatItems ChatPagination (Maybe String)
|
||||||
| APISendMessage ChatRef ComposedMessage
|
| APISendMessage ChatRef ComposedMessage
|
||||||
| APIUpdateChatItem ChatRef ChatItemId MsgContent
|
| APIUpdateChatItem ChatRef ChatItemId MsgContent
|
||||||
| APIDeleteChatItem ChatRef ChatItemId CIDeleteMode
|
| APIDeleteChatItem ChatRef ChatItemId CIDeleteMode
|
||||||
@ -261,7 +261,7 @@ data ChatResponse
|
|||||||
| CRChatSuspended
|
| CRChatSuspended
|
||||||
| CRApiChats {chats :: [AChat]}
|
| CRApiChats {chats :: [AChat]}
|
||||||
| CRApiChat {chat :: AChat}
|
| CRApiChat {chat :: AChat}
|
||||||
| CRLastMessages {chatItems :: [AChatItem]}
|
| CRApiChatItems {chatItems :: [AChatItem]}
|
||||||
| CRApiParsedMarkdown {formattedText :: Maybe MarkdownList}
|
| CRApiParsedMarkdown {formattedText :: Maybe MarkdownList}
|
||||||
| CRUserSMPServers {smpServers :: NonEmpty ServerCfg, presetSMPServers :: NonEmpty SMPServerWithAuth}
|
| CRUserSMPServers {smpServers :: NonEmpty ServerCfg, presetSMPServers :: NonEmpty SMPServerWithAuth}
|
||||||
| CRSmpTestResult {smpTestFailure :: Maybe SMPTestFailure}
|
| CRSmpTestResult {smpTestFailure :: Maybe SMPTestFailure}
|
||||||
|
@ -3707,8 +3707,8 @@ getAllChatItems db user pagination search_ = do
|
|||||||
let search = fromMaybe "" search_
|
let search = fromMaybe "" search_
|
||||||
case pagination of
|
case pagination of
|
||||||
CPLast count -> getAllChatItemsLast_ db user count search
|
CPLast count -> getAllChatItemsLast_ db user count search
|
||||||
CPAfter _afterId _count -> throwError $ SEInternalError "not implemented"
|
CPAfter afterId count -> getAllChatItemsAfter_ db user afterId count search
|
||||||
CPBefore _beforeId _count -> throwError $ SEInternalError "not implemented"
|
CPBefore beforeId count -> getAllChatItemsBefore_ db user beforeId count search
|
||||||
|
|
||||||
getAllChatItemsLast_ :: DB.Connection -> User -> Int -> String -> ExceptT StoreError IO [AChatItem]
|
getAllChatItemsLast_ :: DB.Connection -> User -> Int -> String -> ExceptT StoreError IO [AChatItem]
|
||||||
getAllChatItemsLast_ db user@User {userId} count search = do
|
getAllChatItemsLast_ db user@User {userId} count search = do
|
||||||
@ -3720,13 +3720,59 @@ getAllChatItemsLast_ db user@User {userId} count search = do
|
|||||||
[sql|
|
[sql|
|
||||||
SELECT chat_item_id, contact_id, group_id
|
SELECT chat_item_id, contact_id, group_id
|
||||||
FROM chat_items
|
FROM chat_items
|
||||||
WHERE user_id = ? AND item_text LIKE '%' || ? || '%'
|
WHERE user_id = ? AND item_deleted != 1 AND item_text LIKE '%' || ? || '%'
|
||||||
ORDER BY item_ts DESC, chat_item_id DESC
|
ORDER BY item_ts DESC, chat_item_id DESC
|
||||||
LIMIT ?
|
LIMIT ?
|
||||||
|]
|
|]
|
||||||
(userId, search, count)
|
(userId, search, count)
|
||||||
mapM (uncurry $ getAChatItem_ db user) itemRefs
|
mapM (uncurry $ getAChatItem_ db user) itemRefs
|
||||||
|
|
||||||
|
getAllChatItemsAfter_ :: DB.Connection -> User -> Int64 -> Int -> String -> ExceptT StoreError IO [AChatItem]
|
||||||
|
getAllChatItemsAfter_ db user@User {userId} afterItemId count search = do
|
||||||
|
AChatItem _ _ _ afterItem <- getAChatItem db user afterItemId
|
||||||
|
itemRefs <- liftIO $ getChatItemRefsAfter_ (chatItemTs' afterItem)
|
||||||
|
mapM (uncurry $ getAChatItem_ db user) itemRefs
|
||||||
|
where
|
||||||
|
getChatItemRefsAfter_ afterItemTs =
|
||||||
|
rights . map toChatItemRef
|
||||||
|
<$> DB.query
|
||||||
|
db
|
||||||
|
[sql|
|
||||||
|
SELECT chat_item_id, contact_id, group_id
|
||||||
|
FROM chat_items
|
||||||
|
WHERE user_id = ? AND item_deleted != 1 AND item_text LIKE '%' || ? || '%'
|
||||||
|
AND (item_ts > ? OR (item_ts = ? AND chat_item_id > ?))
|
||||||
|
ORDER BY item_ts ASC, chat_item_id ASC
|
||||||
|
LIMIT ?
|
||||||
|
|]
|
||||||
|
(userId, search, afterItemTs, afterItemTs, afterItemId, count)
|
||||||
|
|
||||||
|
getAllChatItemsBefore_ :: DB.Connection -> User -> Int64 -> Int -> String -> ExceptT StoreError IO [AChatItem]
|
||||||
|
getAllChatItemsBefore_ db user@User {userId} beforeItemId count search = do
|
||||||
|
AChatItem _ _ _ beforeItem <- getAChatItem db user beforeItemId
|
||||||
|
itemRefs <- liftIO $ getChatItemRefsBefore_ (chatItemTs' beforeItem)
|
||||||
|
mapM (uncurry $ getAChatItem_ db user) itemRefs
|
||||||
|
where
|
||||||
|
getChatItemRefsBefore_ beforeItemTs =
|
||||||
|
reverse . rights . map toChatItemRef
|
||||||
|
<$> DB.query
|
||||||
|
db
|
||||||
|
[sql|
|
||||||
|
SELECT chat_item_id, contact_id, group_id
|
||||||
|
FROM chat_items
|
||||||
|
WHERE user_id = ? AND item_deleted != 1 AND item_text LIKE '%' || ? || '%'
|
||||||
|
AND (item_ts < ? OR (item_ts = ? AND chat_item_id < ?))
|
||||||
|
ORDER BY item_ts DESC, chat_item_id DESC
|
||||||
|
LIMIT ?
|
||||||
|
|]
|
||||||
|
(userId, search, beforeItemTs, beforeItemTs, beforeItemId, count)
|
||||||
|
|
||||||
|
getAChatItem :: DB.Connection -> User -> ChatItemId -> ExceptT StoreError IO AChatItem
|
||||||
|
getAChatItem db user@User {userId} itemId = do
|
||||||
|
afterItemRef <- ExceptT $ firstRow' toChatItemRef (SEChatItemNotFound itemId) $
|
||||||
|
DB.query db "SELECT chat_item_id, contact_id, group_id FROM chat_items WHERE user_id = ? AND chat_item_id = ?" (userId, itemId)
|
||||||
|
uncurry (getAChatItem_ db user) afterItemRef
|
||||||
|
|
||||||
getGroupIdByName :: DB.Connection -> User -> GroupName -> ExceptT StoreError IO GroupId
|
getGroupIdByName :: DB.Connection -> User -> GroupName -> ExceptT StoreError IO GroupId
|
||||||
getGroupIdByName db User {userId} gName =
|
getGroupIdByName db User {userId} gName =
|
||||||
ExceptT . firstRow fromOnly (SEGroupNotFoundByName gName) $
|
ExceptT . firstRow fromOnly (SEGroupNotFoundByName gName) $
|
||||||
|
@ -75,7 +75,7 @@ responseToView user_ testView ts = \case
|
|||||||
CRContactSwitch ct progress -> viewContactSwitch ct progress
|
CRContactSwitch ct progress -> viewContactSwitch ct progress
|
||||||
CRGroupMemberSwitch g m progress -> viewGroupMemberSwitch g m progress
|
CRGroupMemberSwitch g m progress -> viewGroupMemberSwitch g m progress
|
||||||
CRNewChatItem (AChatItem _ _ chat item) -> unmuted chat item $ viewChatItem chat item False ts
|
CRNewChatItem (AChatItem _ _ chat item) -> unmuted chat item $ viewChatItem chat item False ts
|
||||||
CRLastMessages chatItems -> concatMap (\(AChatItem _ _ chat item) -> viewChatItem chat item True ts) chatItems
|
CRApiChatItems chatItems -> concatMap (\(AChatItem _ _ chat item) -> viewChatItem chat item True ts) chatItems
|
||||||
CRChatItemStatusUpdated _ -> []
|
CRChatItemStatusUpdated _ -> []
|
||||||
CRChatItemUpdated (AChatItem _ _ chat item) -> unmuted chat item $ viewItemUpdate chat item ts
|
CRChatItemUpdated (AChatItem _ _ chat item) -> unmuted chat item $ viewItemUpdate chat item ts
|
||||||
CRChatItemDeleted (AChatItem _ _ chat deletedItem) (AChatItem _ _ _ toItem) -> unmuted chat deletedItem $ viewItemDelete chat deletedItem toItem ts
|
CRChatItemDeleted (AChatItem _ _ chat deletedItem) (AChatItem _ _ _ toItem) -> unmuted chat deletedItem $ viewItemDelete chat deletedItem toItem ts
|
||||||
|
Loading…
Reference in New Issue
Block a user