fix(Communities): ensure community chats exist in memory upon spectating

When a community link is shared with a user and that user visits that community, it turns out that the chat objects for that community have not been created yet.

This results in chat data being malformed/non-existent until another message signal is processed that updates the chats in memory. This update can someimes occur earlier, sometimes later, which is why the issue isn't always reproducible.

This commit ensures we're loading chats from status-go again upon spectating, to ensure they exist in memory when we try to access the data in the UI.

Closes #8361
This commit is contained in:
Pascal Precht 2023-02-09 13:05:38 +01:00 committed by Jonathan Rainville
parent 723bc4f387
commit a97d32346a
2 changed files with 31 additions and 19 deletions

View File

@ -129,6 +129,7 @@ QtObject:
# Forward declarations # Forward declarations
proc updateOrAddChat*(self: Service, chat: ChatDto) proc updateOrAddChat*(self: Service, chat: ChatDto)
proc hydrateChats*(self: Service, data: JsonNode)
proc updateOrAddChannelGroup*(self: Service, channelGroup: ChannelGroupDto) proc updateOrAddChannelGroup*(self: Service, channelGroup: ChannelGroupDto)
proc doConnect(self: Service) = proc doConnect(self: Service) =
@ -163,6 +164,14 @@ QtObject:
proc getChannelGroups*(self: Service): seq[ChannelGroupDto] = proc getChannelGroups*(self: Service): seq[ChannelGroupDto] =
return toSeq(self.channelGroups.values) return toSeq(self.channelGroups.values)
proc loadChats*(self: Service) =
try:
let response = status_chat.getChats()
self.hydrateChats(response.result)
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription
proc asyncGetChats*(self: Service) = proc asyncGetChats*(self: Service) =
let arg = AsyncGetChatsTaskArg( let arg = AsyncGetChatsTaskArg(
tptr: cast[ByteAddress](asyncGetChatsTask), tptr: cast[ByteAddress](asyncGetChatsTask),
@ -176,6 +185,26 @@ QtObject:
if (y[1].channelGroupType == Personal): return 1 if (y[1].channelGroupType == Personal): return 1
return 0 return 0
proc hydrateChats(self: Service, data: JsonNode) =
var chats: seq[ChatDto] = @[]
for (sectionId, section) in data.pairs:
var channelGroup = section.toChannelGroupDto()
channelGroup.id = sectionId
self.channelGroups[sectionId] = channelGroup
for (chatId, chat) in section["chats"].pairs:
chats.add(chat.toChatDto())
# Make the personal channelGroup the first one
self.channelGroups.sort(sortPersonnalChatAsFirst[string, ChannelGroupDto], SortOrder.Ascending)
for chat in chats:
if chat.active and chat.chatType != chat_dto.ChatType.Unknown:
if chat.chatType == chat_dto.ChatType.Public:
# Deactivate old public chats
discard status_chat.deactivateChat(chat.id)
else:
self.chats[chat.id] = chat
proc onAsyncGetChatsResponse*(self: Service, response: string) {.slot.} = proc onAsyncGetChatsResponse*(self: Service, response: string) {.slot.} =
try: try:
let rpcResponseObj = response.parseJson let rpcResponseObj = response.parseJson
@ -183,25 +212,7 @@ QtObject:
if(rpcResponseObj["channelGroups"].kind == JNull): if(rpcResponseObj["channelGroups"].kind == JNull):
raise newException(RpcException, "No channel groups returned") raise newException(RpcException, "No channel groups returned")
var chats: seq[ChatDto] = @[] self.hydrateChats(rpcResponseObj["channelGroups"])
for (sectionId, section) in rpcResponseObj["channelGroups"].pairs:
var channelGroup = section.toChannelGroupDto()
channelGroup.id = sectionId
self.channelGroups[sectionId] = channelGroup
for (chatId, chat) in section["chats"].pairs:
chats.add(chat.toChatDto())
# Make the personal channelGroup the first one
self.channelGroups.sort(sortPersonnalChatAsFirst[string, ChannelGroupDto], SortOrder.Ascending)
for chat in chats:
if chat.active and chat.chatType != chat_dto.ChatType.Unknown:
if chat.chatType == chat_dto.ChatType.Public:
# Deactivate old public chats
discard status_chat.deactivateChat(chat.id)
else:
self.chats[chat.id] = chat
self.events.emit(SIGNAL_CHATS_LOADED, ChannelGroupsArgs(channelGroups: self.getChannelGroups())) self.events.emit(SIGNAL_CHATS_LOADED, ChannelGroupsArgs(channelGroups: self.getChannelGroups()))
except Exception as e: except Exception as e:
let errDesription = e.msg let errDesription = e.msg

View File

@ -702,6 +702,7 @@ QtObject:
updatedCommunity.settings = communitySettings updatedCommunity.settings = communitySettings
self.allCommunities[communityId] = updatedCommunity self.allCommunities[communityId] = updatedCommunity
self.chatService.loadChats()
for k, chat in updatedCommunity.chats: for k, chat in updatedCommunity.chats:
let fullChatId = communityId & chat.id let fullChatId = communityId & chat.id