mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-03-31 16:15:55 +00:00
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:
@@ -283,9 +283,7 @@ processChatCommand = \case
|
||||
setActive ActiveNone
|
||||
atomically . writeTVar u $ Just user
|
||||
pure $ CRActiveUser user
|
||||
ListUsers -> do
|
||||
users <- withStore' getUsers
|
||||
pure $ CRUsersList users
|
||||
ListUsers -> CRUsersList <$> withStore' getUsersInfo
|
||||
APISetActiveUser userId -> do
|
||||
u <- asks currentUser
|
||||
user <- withStore $ \db -> getSetActiveUser db userId
|
||||
|
||||
@@ -315,7 +315,7 @@ data ChatCommand
|
||||
|
||||
data ChatResponse
|
||||
= CRActiveUser {user :: User}
|
||||
| CRUsersList {users :: [User]}
|
||||
| CRUsersList {users :: [UserInfo]}
|
||||
| CRChatStarted
|
||||
| CRChatRunning
|
||||
| CRChatStopped
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -115,6 +115,16 @@ data User = User
|
||||
|
||||
instance ToJSON User where toEncoding = J.genericToEncoding J.defaultOptions
|
||||
|
||||
data UserInfo = UserInfo
|
||||
{ user :: User,
|
||||
unreadCount :: Int
|
||||
}
|
||||
deriving (Show, Generic, FromJSON)
|
||||
|
||||
instance ToJSON UserInfo where
|
||||
toJSON = J.genericToJSON J.defaultOptions
|
||||
toEncoding = J.genericToEncoding J.defaultOptions
|
||||
|
||||
type UserId = Int64
|
||||
|
||||
type ContactId = Int64
|
||||
|
||||
@@ -267,12 +267,15 @@ responseToView user_ ChatConfig {logLevel, testView} liveItems ts = \case
|
||||
| muted chat chatItem = []
|
||||
| otherwise = s
|
||||
|
||||
viewUsersList :: [User] -> [StyledString]
|
||||
viewUsersList =
|
||||
let ldn = T.toLower . (localDisplayName :: User -> ContactName)
|
||||
in map (\user@User {profile = LocalProfile {displayName, fullName}} -> ttyFullName displayName fullName <> active user) . sortOn ldn
|
||||
viewUsersList :: [UserInfo] -> [StyledString]
|
||||
viewUsersList = map userInfo . sortOn ldn
|
||||
where
|
||||
active User {activeUser} = if activeUser then highlight' " (active)" else ""
|
||||
ldn (UserInfo User {localDisplayName = n} _) = T.toLower n
|
||||
userInfo (UserInfo User {localDisplayName = n, profile = LocalProfile {fullName}, activeUser} count) =
|
||||
ttyFullName n fullName <> active <> unread
|
||||
where
|
||||
active = if activeUser then highlight' " (active)" else ""
|
||||
unread = if count /= 0 then plain $ " (unread: " <> show count <> ")" else ""
|
||||
|
||||
muted :: ChatInfo c -> ChatItem c d -> Bool
|
||||
muted chat ChatItem {chatDir} = case (chat, chatDir) of
|
||||
|
||||
@@ -4425,7 +4425,7 @@ testCreateSecondUser =
|
||||
showActiveUser alice "alice (Alice)"
|
||||
|
||||
alice ##> "/users"
|
||||
alice <## "alice (Alice) (active)"
|
||||
alice <## "alice (Alice) (active) (unread: 1)"
|
||||
alice <## "alisa"
|
||||
|
||||
alice <##> bob
|
||||
|
||||
Reference in New Issue
Block a user