perf(@desktop/chat): make mark all messages read async

This commit is contained in:
Anthony Laibe 2021-08-31 10:05:51 +02:00 committed by Iuri Matias
parent 0d5cf71df5
commit e2ca49b097
3 changed files with 38 additions and 12 deletions

View File

@ -93,13 +93,13 @@ QtObject:
if (self.chats.chats.len == 0): return if (self.chats.chats.len == 0): return
let selectedChannel = self.getChannel(channelIndex) let selectedChannel = self.getChannel(channelIndex)
if (selectedChannel == nil): return if (selectedChannel == nil): return
discard self.status.chat.markAllChannelMessagesRead(selectedChannel.id) self.status.chat.asyncMarkAllChannelMessagesRead(selectedChannel.id)
proc markChatItemAsRead*(self: ChannelView, id: string) {.slot.} = proc markChatItemAsRead*(self: ChannelView, id: string) {.slot.} =
if (self.chats.chats.len == 0): return if (self.chats.chats.len == 0): return
let selectedChannel = self.getChannelById(id) let selectedChannel = self.getChannelById(id)
if (selectedChannel == nil): return if (selectedChannel == nil): return
discard self.status.chat.markAllChannelMessagesRead(selectedChannel.id) self.status.chat.asyncMarkAllChannelMessagesRead(selectedChannel.id)
proc clearUnreadIfNeeded*(self: ChannelView, channel: var Chat) = proc clearUnreadIfNeeded*(self: ChannelView, channel: var Chat) =
if (not channel.isNil and (channel.unviewedMessagesCount > 0 or channel.mentionsCount > 0)): if (not channel.isNil and (channel.unviewedMessagesCount > 0 or channel.mentionsCount > 0)):
@ -166,7 +166,7 @@ QtObject:
if not self.communities.activeCommunity.active: if not self.communities.activeCommunity.active:
self.previousActiveChannelIndex = self.chats.chats.findIndexById(self.activeChannel.id) self.previousActiveChannelIndex = self.chats.chats.findIndexById(self.activeChannel.id)
discard self.status.chat.markAllChannelMessagesRead(self.activeChannel.id) self.status.chat.asyncMarkAllChannelMessagesRead(self.activeChannel.id)
self.activeChannelChanged() self.activeChannelChanged()

View File

@ -367,25 +367,37 @@ QtObject:
let reactions = status_chat.removeEmojiReaction(emojiReactionId) let reactions = status_chat.removeEmojiReaction(emojiReactionId)
self.events.emit("reactionsLoaded", ReactionsLoadedArgs(reactions: reactions)) self.events.emit("reactionsLoaded", ReactionsLoadedArgs(reactions: reactions))
proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode = proc onMarkMessagesRead(self: ChatModel, response: string, chatId: string): JsonNode =
var response = status_chat.markAllRead(chatId)
result = parseJson(response) result = parseJson(response)
if self.channels.hasKey(chatId): if self.channels.hasKey(chatId):
self.channels[chatId].unviewedMessagesCount = 0 self.channels[chatId].unviewedMessagesCount = 0
self.channels[chatId].mentionsCount = 0 self.channels[chatId].mentionsCount = 0
self.events.emit("channelUpdate", ChatUpdateArgs(messages: @[], chats: @[self.channels[chatId]], contacts: @[])) self.events.emit("channelUpdate", ChatUpdateArgs(messages: @[], chats: @[self.channels[chatId]], contacts: @[]))
proc onAsyncMarkMessagesRead(self: ChatModel, response: string) {.slot.} =
let parsedResponse = parseJson(response)
discard self.onMarkMessagesRead(parsedResponse{"response"}.getStr, parsedResponse{"chatId"}.getStr)
proc markAllChannelMessagesRead*(self: ChatModel, chatId: string): JsonNode =
var response = status_chat.markAllRead(chatId)
return self.onMarkMessagesRead(response, chatId)
proc asyncMarkAllChannelMessagesRead*(self: ChatModel, chatId: string) =
let arg = AsyncMarkAllReadTaskArg(
tptr: cast[ByteAddress](asyncMarkAllReadTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onAsyncMarkMessagesRead",
chatId: chatId,
)
self.tasks.threadpool.start(arg)
proc markMessagesSeen*(self: ChatModel, chatId: string, messageIds: seq[string]): JsonNode = proc markMessagesSeen*(self: ChatModel, chatId: string, messageIds: seq[string]): JsonNode =
var response = status_chat.markMessagesSeen(chatId, messageIds) var response = status_chat.markMessagesSeen(chatId, messageIds)
result = parseJson(response) return self.onMarkMessagesRead(response, chatId)
if self.channels.hasKey(chatId):
self.channels[chatId].unviewedMessagesCount = 0
self.channels[chatId].mentionsCount = 0
self.events.emit("channelUpdate", ChatUpdateArgs(messages: @[], chats: @[self.channels[chatId]], contacts: @[]))
proc confirmJoiningGroup*(self: ChatModel, chatId: string) = proc confirmJoiningGroup*(self: ChatModel, chatId: string) =
var response = status_chat.confirmJoiningGroup(chatId) var response = status_chat.confirmJoiningGroup(chatId)
self.emitUpdate(response) self.emitUpdate(response)
proc renameGroup*(self: ChatModel, chatId: string, newName: string) = proc renameGroup*(self: ChatModel, chatId: string, newName: string) =
var response = status_chat.renameGroup(chatId, newName) var response = status_chat.renameGroup(chatId, newName)

View File

@ -49,6 +49,20 @@ const asyncSearchMessagesInChatsAndCommunitiesTask: Task = proc(argEncoded: stri
} }
arg.finish(responseJson) arg.finish(responseJson)
#################################################
# Async mark messages read
#################################################
type
AsyncMarkAllReadTaskArg = ref object of QObjectTaskArg
chatId: string
const asyncMarkAllReadTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncMarkAllReadTaskArg](argEncoded)
arg.finish(%*{
"response": status_chat.markAllRead(arg.chatId),
"chatId": arg.chatId,
})
################################################# #################################################
# Async load messages # Async load messages
################################################# #################################################