fix(Timeline): make reactions works

This commit makes reactions in the status timeline work.
There are two things prior to this commit that are broken:

1. The logic that opens the reaction context menu always expects
   and instance of `chatsView` because it tries to calculate a users
   nickname. Such an instance isn't always available in that context, so
   the nickname logic has been moved to `appMain` for now, removing that
   dependency and therefore making it work in both, the chat view as well
   as the status view.
2. While 1) makes the context menu work, it turns out that adding and
   removing reactions inside the status timeline is still not working.
   The reason for that is, that the reactions component maintains its own
   `messageList`, which isn't aware of the fact that reactions for messages
   coming from chats of `ChatType.Profile`, need to go into a dedicated
   message list for `ChatType.Timeline`.

In other words, reactions are sent and removed from message in messagelists
that don't actually exist.

This commit fixes both of these things by ensuring the message lists
maintained by reactions are timeline aware. Also ensuring updates are
done correctly.
This commit is contained in:
Pascal Precht 2021-01-14 17:03:00 +01:00 committed by Iuri Matias
parent 9facb14808
commit 7102596b3f
9 changed files with 39 additions and 23 deletions

View File

@ -461,6 +461,13 @@ QtObject:
self.messageList[chatId].delete
self.messageList.del(chatId)
proc toggleReaction*(self: ChatsView, messageId: string, emojiId: int) {.slot.} =
if self.activeChannel.id == status_utils.getTimelineChatId():
let message = self.messageList[status_utils.getTimelineChatId()].getMessageById(messageId)
self.reactions.toggle(messageId, message.chatId, emojiId)
else:
self.reactions.toggle(messageId, self.activeChannel.id, emojiId)
proc removeMessagesFromTimeline*(self: ChatsView, chatId: string) =
self.messageList[status_utils.getTimelineChatId()].deleteMessagesByChatId(chatId)
self.activeChannelChanged()

View File

@ -1,7 +1,8 @@
import NimQml, tables, json, chronicles
import ../../../status/[status, chat/message]
import ../../../status/[status, chat/message, chat/chat]
import message_list, chat_item
import ../../../status/libstatus/settings as status_settings
import ../../../status/libstatus/utils as status_utils
import ../../../status/libstatus/types
logScope:
@ -32,6 +33,11 @@ QtObject:
self.pubKey = status_settings.getSetting[string](Setting.PublicKey, "0x0")
proc messageEmojiReactionId(self: ReactionView, chatId: string, messageId: string, emojiId: int): string =
let chat = self.status.chat.channels[chatId]
var chatId = chatId
if chat.chatType == ChatType.Profile:
chatId = status_utils.getTimelineChatId()
if (self.messageList[][chatId].getReactions(messageId) == "") :
return ""
@ -42,17 +48,20 @@ QtObject:
return pair[0]
return ""
proc toggle*(self: ReactionView, messageId: string, emojiId: int) {.slot.} =
let emojiReactionId = self.messageEmojiReactionId(self.activeChannel.id, messageId, emojiId)
proc toggle*(self: ReactionView, messageId: string, chatId: string, emojiId: int) {.slot.} =
let emojiReactionId = self.messageEmojiReactionId(chatId, messageId, emojiId)
if (emojiReactionId == ""):
self.status.chat.addEmojiReaction(self.activeChannel.id, messageId, emojiId)
self.status.chat.addEmojiReaction(chatId, messageId, emojiId)
else:
self.status.chat.removeEmojiReaction(emojiReactionId)
proc push*(self: ReactionView, reactions: var seq[Reaction]) =
let t = reactions.len
for reaction in reactions.mitems:
let messageList = self.messageList[][reaction.chatId]
let chat = self.status.chat.channels[reaction.chatId]
var messageList = self.messageList[][reaction.chatId]
if chat.chatType == ChatType.Profile:
messageList = self.messageList[][status_utils.getTimelineChatId()]
var emojiReactions = messageList.getReactions(reaction.messageId)
var oldReactions: JsonNode
if (emojiReactions == "") :

View File

@ -90,7 +90,7 @@ Item {
SelectedMessage.set(messageId, fromAuthor);
}
// Get contact nickname
let nickname = chatView.getUserNickname(fromAuthor)
let nickname = appMain.getUserNickname(fromAuthor)
messageContextMenu.isProfile = !!isProfileClick
messageContextMenu.isSticker = isSticker
messageContextMenu.emojiOnly = emojiOnly

View File

@ -132,7 +132,7 @@ Item {
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
chatsModel.reactions.toggle(messageId, modelData.emojiId)
chatsModel.toggleReaction(messageId, modelData.emojiId)
}
}

View File

@ -47,18 +47,6 @@ SplitView {
return contacts
}
function getUserNickname(pubKey) {
// Get contact nickname
const contactList = profileModel.contacts.list
const contactCount = contactList.rowCount()
for (let i = 0; i < contactCount; i++) {
if (contactList.rowData(i, 'pubKey') === pubKey) {
return contactList.rowData(i, 'localNickname')
}
}
return ""
}
Connections {
target: applicationWindow
onSettingsLoaded: {

View File

@ -80,7 +80,7 @@ ModalPopup {
width: parent.width
height: identicon.height
property string nickname: chatView.getUserNickname(model.pubKey)
property string nickname: appMain.getUserNickname(model.pubKey)
StatusImageIdenticon {
id: identicon

View File

@ -13,9 +13,8 @@ SVGImage {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
chatsModel.reactions.toggle(SelectedMessage.messageId, emojiId)
chatsModel.toggleReaction(SelectedMessage.messageId, emojiId)
reactionImage.closeModal()
}
}
}

View File

@ -210,7 +210,7 @@ ModalPopup {
width: parent.width
height: identicon.height
property string nickname: chatView.getUserNickname(model.pubKey)
property string nickname: appMain.getUserNickname(model.pubKey)
StatusImageIdenticon {
id: identicon

View File

@ -34,6 +34,19 @@ RowLayout {
return popup
}
function getUserNickname(pubKey) {
// Get contact nickname
const contactList = profileModel.contacts.list
const contactCount = contactList.rowCount()
for (let i = 0; i < contactCount; i++) {
if (contactList.rowData(i, 'pubKey') === pubKey) {
return contactList.rowData(i, 'localNickname')
}
}
return ""
}
function openLink(link) {
if (appSettings.showBrowserSelector) {
appMain.openPopup(chooseBrowserPopupComponent, {link: link})