core: return user unread counts on ListUsers command (#1763)

* core: return user unread counts on ListUsers command

* split

* tests

* refactor

* viewUserInfo

* refactor

* remove omit nothing

* corrections

* fix

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
JRoberts
2023-01-16 22:57:31 +04:00
committed by GitHub
parent 91a39cae23
commit 2fdc23274d
6 changed files with 35 additions and 10 deletions
+14
View File
@@ -26,6 +26,7 @@ module Simplex.Chat.Store
chatStoreFile,
agentStoreFile,
createUserRecord,
getUsersInfo,
getUsers,
setActiveUser,
getSetActiveUser,
@@ -450,6 +451,19 @@ createUserRecord db (AgentUserId auId) Profile {displayName, fullName, image, pr
DB.execute db "UPDATE users SET contact_id = ? WHERE user_id = ?" (contactId, userId)
pure $ toUser (userId, auId, contactId, profileId, activeUser, displayName, fullName, image, userPreferences)
getUsersInfo :: DB.Connection -> IO [UserInfo]
getUsersInfo db = getUsers db >>= mapM getUserInfo
where
getUserInfo :: User -> IO UserInfo
getUserInfo user@User {userId} = do
count_ <-
maybeFirstRow fromOnly $
DB.query
db
"SELECT COUNT(1) FROM chat_items WHERE user_id = ? AND item_status = ? GROUP BY user_id"
(userId, CISRcvNew)
pure UserInfo {user, unreadCount = fromMaybe 0 count_}
getUsers :: DB.Connection -> IO [User]
getUsers db =
map toUser <$> DB.query_ db userQuery