From fd3303a4e8d3f886cba431dffb29dc6a30e79154 Mon Sep 17 00:00:00 2001 From: Sale Djenic Date: Thu, 4 Nov 2021 16:03:24 +0100 Subject: [PATCH] refactor(@desktop/chat-communities): pinned messages added Option to pin/unpin message added on the backend side. Pinned model added to the chat content module. Message service updated accordingly. --- .../chat_section/chat_content/controller.nim | 25 +++++++-- .../chat_content/controller_interface.nim | 10 +++- .../main/chat_section/chat_content/item.nim | 21 -------- .../chat_content/messages/controller.nim | 18 ++++++- .../messages/controller_interface.nim | 3 ++ .../chat_content/messages/module.nim | 19 +++++-- .../module_controller_delegate_interface.nim | 8 ++- .../module_view_delegate_interface.nim | 3 ++ .../chat_content/messages/view.nim | 11 ++-- .../main/chat_section/chat_content/model.nim | 17 ------ .../main/chat_section/chat_content/module.nim | 47 +++++++++++++++- .../module_controller_delegate_interface.nim | 10 ++++ .../module_view_delegate_interface.nim | 3 ++ .../main/chat_section/chat_content/view.nim | 21 ++++++-- .../message_item.nim} | 8 +++ .../message_model.nim} | 50 ++++++++++++++--- .../{pinnedMessage.nim => pinned_message.nim} | 0 src/app_service/service/message/service.nim | 54 +++++++++++++++++-- 18 files changed, 255 insertions(+), 73 deletions(-) delete mode 100644 src/app/modules/main/chat_section/chat_content/item.nim delete mode 100644 src/app/modules/main/chat_section/chat_content/model.nim rename src/app/modules/{main/chat_section/chat_content/messages/item.nim => shared_models/message_item.nim} (93%) rename src/app/modules/{main/chat_section/chat_content/messages/model.nim => shared_models/message_model.nim} (79%) rename src/app_service/service/message/dto/{pinnedMessage.nim => pinned_message.nim} (100%) diff --git a/src/app/modules/main/chat_section/chat_content/controller.nim b/src/app/modules/main/chat_section/chat_content/controller.nim index 93dc0330e3..98c1562846 100644 --- a/src/app/modules/main/chat_section/chat_content/controller.nim +++ b/src/app/modules/main/chat_section/chat_content/controller.nim @@ -42,12 +42,29 @@ method init*(self: Controller) = let args = MessagesLoadedArgs(e) if(self.chatId != args.chatId): return - - # We should handle only pinned messages within this module. - echo "ChatContent...RECEIVED MESSAGES: ", repr(args) + self.delegate.newPinnedMessagesLoaded(args.pinnedMessages) + + self.events.on(SIGNAL_MESSAGE_PINNED) do(e:Args): + let args = MessagePinUnpinArgs(e) + if(self.chatId != args.chatId): + return + self.delegate.onPinMessage(args.messageId) + + self.events.on(SIGNAL_MESSAGE_UNPINNED) do(e:Args): + let args = MessagePinUnpinArgs(e) + if(self.chatId != args.chatId): + return + self.delegate.onUnpinMessage(args.messageId) method getChatId*(self: Controller): string = return self.chatId method belongsToCommunity*(self: Controller): bool = - return self.belongsToCommunity \ No newline at end of file + return self.belongsToCommunity + +method unpinMessage*(self: Controller, messageId: string) = + self.messageService.pinUnpinMessage(self.chatId, messageId, false) + +method getMessageDetails*(self: Controller, messageId: string): + tuple[message: MessageDto, reactions: seq[ReactionDto], error: string] = + return self.messageService.getDetailsForMessage(self.chatId, messageId) \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/controller_interface.nim b/src/app/modules/main/chat_section/chat_content/controller_interface.nim index 773b5450da..c9f32099e5 100644 --- a/src/app/modules/main/chat_section/chat_content/controller_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/controller_interface.nim @@ -1,5 +1,4 @@ -import ../../../../../app_service/service/chat/service_interface as chat_service -import ../../../../../app_service/service/community/service_interface as community_service +import ../../../../../app_service/service/message/dto/[message, reaction] type AccessInterface* {.pure inheritable.} = ref object of RootObj @@ -15,4 +14,11 @@ method getChatId*(self: AccessInterface): string {.base.} = raise newException(ValueError, "No implementation available") method belongsToCommunity*(self: AccessInterface): bool {.base.} = + raise newException(ValueError, "No implementation available") + +method unpinMessage*(self: AccessInterface, messageId: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method getMessageDetails*(self: AccessInterface, messageId: string): + tuple[message: MessageDto, reactions: seq[ReactionDto], error: string] {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/item.nim b/src/app/modules/main/chat_section/chat_content/item.nim deleted file mode 100644 index 2544cee951..0000000000 --- a/src/app/modules/main/chat_section/chat_content/item.nim +++ /dev/null @@ -1,21 +0,0 @@ -import NimQml - -QtObject: - type - Item* = ref object of QObject - - proc setup(self: Item) = - self.QObject.setup - - proc delete*(self: Item) = - self.QObject.delete - - proc newItem*(): Item = - new(result, delete) - result.setup() - - proc id*(self: Item): string {.slot.} = - self.id - - QtProperty[string] id: - read = id \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller.nim b/src/app/modules/main/chat_section/chat_content/messages/controller.nim index b2b6b42818..c02a9f30ef 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller.nim @@ -40,8 +40,19 @@ method init*(self: Controller) = let args = MessagesLoadedArgs(e) if(self.chatId != args.chatId): return + self.delegate.newMessagesLoaded(args.messages, args.reactions, args.pinnedMessages) - self.delegate.newMessagesLoaded(args.messages, args.reactions) + self.events.on(SIGNAL_MESSAGE_PINNED) do(e:Args): + let args = MessagePinUnpinArgs(e) + if(self.chatId != args.chatId): + return + self.delegate.onPinUnpinMessage(args.messageId, true) + + self.events.on(SIGNAL_MESSAGE_UNPINNED) do(e:Args): + let args = MessagePinUnpinArgs(e) + if(self.chatId != args.chatId): + return + self.delegate.onPinUnpinMessage(args.messageId, false) method getChatId*(self: Controller): string = return self.chatId @@ -63,4 +74,7 @@ method removeReaction*(self: Controller, messageId: string, reactionId: string) error "an error has occurred while removing reaction: ", err return - self.delegate.onReactionRemoved(messageId, reactionId) \ No newline at end of file + self.delegate.onReactionRemoved(messageId, reactionId) + +method pinUnpinMessage*(self: Controller, messageId: string, pin: bool) = + self.messageService.pinUnpinMessage(self.chatId, messageId, pin) \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim index 07823cad86..dfdd420e3e 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/controller_interface.nim @@ -20,4 +20,7 @@ method addReaction*(self: AccessInterface, messageId: string, emojiId: int) {.ba raise newException(ValueError, "No implementation available") method removeReaction*(self: AccessInterface, messageId: string, reactionId: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method pinUnpinMessage*(self: AccessInterface, messageId: string, pin: bool) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/module.nim b/src/app/modules/main/chat_section/chat_content/messages/module.nim index 650899c4d3..83a6321bed 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/module.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/module.nim @@ -1,7 +1,9 @@ import NimQml import io_interface import ../io_interface as delegate_interface -import view, controller, model, item +import view, controller +import ../../../../shared_models/message_model +import ../../../../shared_models/message_item import ../../../../../core/global_singleton import ../../../../../../app_service/service/chat/service as chat_service @@ -53,7 +55,8 @@ method viewDidLoad*(self: Module) = method getModuleAsVariant*(self: Module): QVariant = return self.viewVariant -method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: seq[ReactionDto]) = +method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: seq[ReactionDto], + pinnedMessages: seq[PinnedMessageDto]) = var viewItems: seq[Item] for m in messages: var item = initItem(m.id, m.`from`, m.alias, m.identicon, m.outgoingStatus, m.text, m.seen, m.timestamp, @@ -64,6 +67,10 @@ method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: se # m.`from` should be replaced by appropriate ens/alias when we have that part refactored item.addReaction(r.emojiId, m.`from`, r.id) + for p in pinnedMessages: + if(p.message.id == m.id): + item.pinned = true + # messages are sorted from the most recent to the least recent one viewItems = item & viewItems @@ -83,4 +90,10 @@ method onReactionAdded*(self: Module, messageId: string, emojiId: int, reactionI self.view.model.addReaction(messageId, emojiId, myName, reactionId) method onReactionRemoved*(self: Module, messageId: string, reactionId: string) = - self.view.model.removeReaction(messageId, reactionId) \ No newline at end of file + self.view.model.removeReaction(messageId, reactionId) + +method pinUnpinMessage*(self: Module, messageId: string, pin: bool) = + self.controller.pinUnpinMessage(messageId, pin) + +method onPinUnpinMessage*(self: Module, messageId: string, pin: bool) = + self.view.model.pinUnpinMessage(messageId, pin) \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim index 1a43bb755d..d37f9acf90 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_controller_delegate_interface.nim @@ -1,10 +1,14 @@ -import ../../../../../../../app_service/service/message/dto/[message, reaction] +import ../../../../../../../app_service/service/message/dto/[message, reaction, pinned_message] -method newMessagesLoaded*(self: AccessInterface, messages: seq[MessageDto], reactions: seq[ReactionDto]) {.base.} = +method newMessagesLoaded*(self: AccessInterface, messages: seq[MessageDto], reactions: seq[ReactionDto], + pinnedMessages: seq[PinnedMessageDto]) {.base.} = raise newException(ValueError, "No implementation available") method onReactionAdded*(self: AccessInterface, messageId: string, emojiId: int, reactionId: string) {.base.} = raise newException(ValueError, "No implementation available") method onReactionRemoved*(self: AccessInterface, messageId: string, reactionId: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method onPinUnpinMessage*(self: AccessInterface, messageId: string, pin: bool) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim index 23129bc66a..fd61471703 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/private_interfaces/module_view_delegate_interface.nim @@ -2,4 +2,7 @@ method viewDidLoad*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") method toggleReaction*(self: AccessInterface, messageId: string, emojiId: int) {.base.} = + raise newException(ValueError, "No implementation available") + +method pinUnpinMessage*(self: AccessInterface, messageId: string, pin: bool) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/view.nim b/src/app/modules/main/chat_section/chat_content/messages/view.nim index 2d820ffbb8..46b2b4c05a 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/view.nim +++ b/src/app/modules/main/chat_section/chat_content/messages/view.nim @@ -1,5 +1,5 @@ import NimQml, json -import model +import ../../../../shared_models/message_model import io_interface QtObject: @@ -29,10 +29,15 @@ QtObject: QtProperty[QVariant] model: read = getModel - notify = modelChanged proc toggleReaction*(self: View, messageId: string, emojiId: int) {.slot.} = self.delegate.toggleReaction(messageId, emojiId) proc getNamesForReaction*(self: View, messageId: string, emojiId: int): string {.slot.} = - return $(%* self.model.getNamesForReaction(messageId, emojiId)) \ No newline at end of file + return $(%* self.model.getNamesForReaction(messageId, emojiId)) + + proc pinMessage*(self: View, messageId: string) {.slot.} = + self.delegate.pinUnpinMessage(messageId, true) + + proc unpinMessage*(self: View, messageId: string) {.slot.} = + self.delegate.pinUnpinMessage(messageId, false) \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/model.nim b/src/app/modules/main/chat_section/chat_content/model.nim deleted file mode 100644 index 5bbede6cf7..0000000000 --- a/src/app/modules/main/chat_section/chat_content/model.nim +++ /dev/null @@ -1,17 +0,0 @@ -import NimQml -import item - -QtObject: - type - Model* = ref object of QAbstractListModel - sections: seq[Item] - - proc setup(self: Model) = - self.QAbstractListModel.setup - - proc delete*(self: Model) = - self.QAbstractListModel.delete - - proc newModel*(): Model = - new(result, delete) - result.setup \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/module.nim b/src/app/modules/main/chat_section/chat_content/module.nim index 115ffbd8af..64570216af 100644 --- a/src/app/modules/main/chat_section/chat_content/module.nim +++ b/src/app/modules/main/chat_section/chat_content/module.nim @@ -1,7 +1,9 @@ import NimQml, chronicles import io_interface import ../io_interface as delegate_interface -import view, controller, item, model +import view, controller +import ../../../shared_models/message_model as pinned_msg_model +import ../../../shared_models/message_item as pinned_msg_item import ../../../../core/global_singleton import input_area/module as input_area_module @@ -104,4 +106,45 @@ method getMessagesModule*(self: Module): QVariant = return self.messagesModule.getModuleAsVariant() method getUsersModule*(self: Module): QVariant = - return self.usersModule.getModuleAsVariant() \ No newline at end of file + return self.usersModule.getModuleAsVariant() + +proc buildPinnedMessageItem(self: Module, messageId: string, item: var pinned_msg_item.Item): bool = + let (m, reactions, err) = self.controller.getMessageDetails(messageId) + if(err.len > 0): + return false + + item = initItem(m.id, m.`from`, m.alias, m.identicon, m.outgoingStatus, m.text, m.seen, m.timestamp, + m.contentType.ContentType, m.messageType) + item.pinned = true + + for r in reactions: + if(r.messageId == m.id): + # m.`from` should be replaced by appropriate ens/alias when we have that part refactored + item.addReaction(r.emojiId, m.`from`, r.id) + + return true + +method newPinnedMessagesLoaded*(self: Module, pinnedMessages: seq[PinnedMessageDto]) = + var viewItems: seq[Item] + for p in pinnedMessages: + var item: pinned_msg_item.Item + if(not self.buildPinnedMessageItem(p.message.id, item)): + continue + + viewItems = item & viewItems # messages are sorted from the most recent to the least recent one + + self.view.model.prependItems(viewItems) + +method unpinMessage*(self: Module, messageId: string) = + self.controller.unpinMessage(messageId) + +method onUnpinMessage*(self: Module, messageId: string) = + self.view.model.removeItem(messageId) + +method onPinMessage*(self: Module, messageId: string) = + var item: pinned_msg_item.Item + if(not self.buildPinnedMessageItem(messageId, item)): + return + + self.view.model.appendItem(item) + \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/private_interfaces/module_controller_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/private_interfaces/module_controller_delegate_interface.nim index e69de29bb2..79122ed423 100644 --- a/src/app/modules/main/chat_section/chat_content/private_interfaces/module_controller_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/private_interfaces/module_controller_delegate_interface.nim @@ -0,0 +1,10 @@ +import ../../../../../../app_service/service/message/dto/pinned_message + +method newPinnedMessagesLoaded*(self: AccessInterface, pinnedMessages: seq[PinnedMessageDto]) {.base.} = + raise newException(ValueError, "No implementation available") + +method onUnpinMessage*(self: AccessInterface, messageId: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method onPinMessage*(self: AccessInterface, mmessageId: string) {.base.} = + raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/private_interfaces/module_view_delegate_interface.nim b/src/app/modules/main/chat_section/chat_content/private_interfaces/module_view_delegate_interface.nim index 5f306a82eb..079d82dbf1 100644 --- a/src/app/modules/main/chat_section/chat_content/private_interfaces/module_view_delegate_interface.nim +++ b/src/app/modules/main/chat_section/chat_content/private_interfaces/module_view_delegate_interface.nim @@ -10,4 +10,7 @@ method getMessagesModule*(self: AccessInterface): QVariant {.base.} = raise newException(ValueError, "No implementation available") method getUsersModule*(self: AccessInterface): QVariant {.base.} = + raise newException(ValueError, "No implementation available") + +method unpinMessage*(self: AccessInterface, messageId: string) {.base.} = raise newException(ValueError, "No implementation available") \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/view.nim b/src/app/modules/main/chat_section/chat_content/view.nim index 490937525c..9b0d7cdd3c 100644 --- a/src/app/modules/main/chat_section/chat_content/view.nim +++ b/src/app/modules/main/chat_section/chat_content/view.nim @@ -1,22 +1,25 @@ import NimQml -import model +import ../../../shared_models/message_model as pinned_msg_model import io_interface QtObject: type View* = ref object of QObject delegate: io_interface.AccessInterface - model: Model + model*: pinned_msg_model.Model + modelVariant: QVariant proc delete*(self: View) = self.model.delete + self.modelVariant.delete self.QObject.delete proc newView*(delegate: io_interface.AccessInterface): View = new(result, delete) result.QObject.setup result.delegate = delegate - result.model = newModel() + result.model = pinned_msg_model.newModel() + result.modelVariant = newQVariant(result.model) proc load*(self: View) = self.delegate.viewDidLoad() @@ -39,4 +42,14 @@ QtObject: return self.delegate.getUsersModule() QtProperty[QVariant] usersModule: - read = getUsersModule \ No newline at end of file + read = getUsersModule + + + proc getModel(self: View): QVariant {.slot.} = + return self.modelVariant + + QtProperty[QVariant] model: + read = getModel + + proc unpinMessage*(self: View, messageId: string) {.slot.} = + self.delegate.unpinMessage(messageId) \ No newline at end of file diff --git a/src/app/modules/main/chat_section/chat_content/messages/item.nim b/src/app/modules/shared_models/message_item.nim similarity index 93% rename from src/app/modules/main/chat_section/chat_content/messages/item.nim rename to src/app/modules/shared_models/message_item.nim index 6fe6920e86..7f5020bd26 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/item.nim +++ b/src/app/modules/shared_models/message_item.nim @@ -36,6 +36,7 @@ type messageType: int reactions: OrderedTable[int, seq[tuple[name: string, reactionId: string]]] # [emojiId, list of [names reacted with the emojiId, reaction id]] reactionIds: seq[string] + pinned: bool proc initItem*(id, `from`, alias, identicon, outgoingStatus, text: string, seen: bool, timestamp: int64, contentType: ContentType, messageType: int): Item = @@ -50,6 +51,7 @@ proc initItem*(id, `from`, alias, identicon, outgoingStatus, text: string, seen: result.timestamp = timestamp result.contentType = contentType result.messageType = messageType + result.pinned = false proc id*(self: Item): string {.inline.} = self.id @@ -81,6 +83,12 @@ proc contentType*(self: Item): ContentType {.inline.} = proc messageType*(self: Item): int {.inline.} = self.messageType +proc pinned*(self: Item): bool {.inline.} = + self.pinned + +proc `pinned=`*(self: Item, value: bool) {.inline.} = + self.pinned = value + proc shouldAddReaction*(self: Item, emojiId: int, name: string): bool = for k, values in self.reactions: if(k != emojiId): diff --git a/src/app/modules/main/chat_section/chat_content/messages/model.nim b/src/app/modules/shared_models/message_model.nim similarity index 79% rename from src/app/modules/main/chat_section/chat_content/messages/model.nim rename to src/app/modules/shared_models/message_model.nim index dcd59d7af5..d703424472 100644 --- a/src/app/modules/main/chat_section/chat_content/messages/model.nim +++ b/src/app/modules/shared_models/message_model.nim @@ -1,6 +1,6 @@ -import NimQml, Tables, json, strutils, strformat +import NimQml, Tables, json, strutils -import item +import message_item type ModelRole {.pure.} = enum @@ -19,6 +19,7 @@ type # Image # GapFrom # GapTo + Pinned CountsForReactions QtObject: @@ -57,6 +58,7 @@ QtObject: # ModelRole.Image.int:"image", # ModelRole.GapFrom.int:"gapFrom", # ModelRole.GapTo.int:"gapTo", + ModelRole.Pinned.int:"pinned", ModelRole.CountsForReactions.int:"countsForReactions", }.toTable @@ -101,9 +103,18 @@ QtObject: # result = newQVariant(item.gapFrom) # of ModelRole.GapTo: # result = newQVariant(item.gapTo) + of ModelRole.Pinned: + result = newQVariant(item.pinned) of ModelRole.CountsForReactions: result = newQVariant($(%* item.getCountsForReactions)) + proc findIndexForMessageId(self: Model, messageId: string): int = + for i in 0 ..< self.items.len: + if(self.items[i].id == messageId): + return i + + return -1 + proc prependItems*(self: Model, items: seq[Item]) = let parentModelIndex = newQModelIndex() defer: parentModelIndex.delete @@ -114,12 +125,25 @@ QtObject: self.items = items & self.items self.endInsertRows() - proc findIndexForMessageId(self: Model, messageId: string): int = - for i in 0 ..< self.items.len: - if(self.items[i].id == messageId): - return i + proc appendItem*(self: Model, item: Item) = + let parentModelIndex = newQModelIndex() + defer: parentModelIndex.delete - return -1 + self.beginInsertRows(parentModelIndex, self.items.len, self.items.len) + self.items.add(item) + self.endInsertRows() + + proc removeItem*(self: Model, messageId: string) = + let ind = self.findIndexForMessageId(messageId) + if(ind == -1): + return + + let parentModelIndex = newQModelIndex() + defer: parentModelIndex.delete + + self.beginRemoveRows(parentModelIndex, ind, ind) + self.items.delete(ind) + self.endRemoveRows() proc getItemWithMessageId*(self: Model, messageId: string): Item = let ind = self.findIndexForMessageId(messageId) @@ -151,4 +175,14 @@ QtObject: proc getNamesForReaction*(self: Model, messageId: string, emojiId: int): seq[string] = for i in 0 ..< self.items.len: if(self.items[i].id == messageId): - return self.items[i].getNamesForReactions(emojiId) \ No newline at end of file + return self.items[i].getNamesForReactions(emojiId) + + proc pinUnpinMessage*(self: Model, messageId: string, pin: bool) = + let ind = self.findIndexForMessageId(messageId) + if(ind == -1): + return + + self.items[ind].pinned = pin + + let index = self.createIndex(ind, 0, nil) + self.dataChanged(index, index, @[ModelRole.Pinned.int]) \ No newline at end of file diff --git a/src/app_service/service/message/dto/pinnedMessage.nim b/src/app_service/service/message/dto/pinned_message.nim similarity index 100% rename from src/app_service/service/message/dto/pinnedMessage.nim rename to src/app_service/service/message/dto/pinned_message.nim diff --git a/src/app_service/service/message/service.nim b/src/app_service/service/message/service.nim index 71f60008bb..2254d31167 100644 --- a/src/app_service/service/message/service.nim +++ b/src/app_service/service/message/service.nim @@ -6,7 +6,7 @@ import ../../tasks/[qt, threadpool] import status/statusgo_backend_new/messages as status_go import ./dto/message as message_dto -import ./dto/pinnedMessage as pinned_msg_dto +import ./dto/pinned_message as pinned_msg_dto import ./dto/reaction as reaction_dto export message_dto @@ -21,7 +21,9 @@ logScope: const MESSAGES_PER_PAGE = 20 # Signals which may be emitted by this service: -const SIGNAL_MESSAGES_LOADED* = "new-messagesLoaded" #Once we are done with refactoring we should remove "new-" from this signal name +const SIGNAL_MESSAGES_LOADED* = "new-messagesLoaded" #Once we are done with refactoring we should remove "new-" from all signals +const SIGNAL_MESSAGE_PINNED* = "new-messagePinned" +const SIGNAL_MESSAGE_UNPINNED* = "new-messageUnpinned" type MessagesLoadedArgs* = ref object of Args @@ -30,6 +32,10 @@ type pinnedMessages*: seq[PinnedMessageDto] reactions*: seq[ReactionDto] + MessagePinUnpinArgs* = ref object of Args + chatId*: string + messageId*: string + QtObject: type Service* = ref object of QObject events: EventEmitter @@ -150,10 +156,10 @@ QtObject: result.result = reactions[0].id except Exception as e: + result.error = e.msg error "error: ", methodName="addReaction", errName = e.name, errDesription = e.msg - proc removeReaction*(self: Service, reactionId: string): - tuple[result: string, error: string] = + proc removeReaction*(self: Service, reactionId: string): tuple[result: string, error: string] = try: let response = status_go.removeReaction(reactionId) @@ -163,4 +169,42 @@ QtObject: return except Exception as e: - error "error: ", methodName="removeReaction", errName = e.name, errDesription = e.msg \ No newline at end of file + result.error = e.msg + error "error: ", methodName="removeReaction", errName = e.name, errDesription = e.msg + + proc pinUnpinMessage*(self: Service, chatId: string, messageId: string, pin: bool) = + try: + let response = status_go.pinUnpinMessage(messageId, chatId, pin) + + var pinMessagesObj: JsonNode + if(response.result.getProp("pinMessages", pinMessagesObj)): + let data = MessagePinUnpinArgs(chatId: chatId, messageId: messageId) + var pinned = false + if(pinMessagesObj.getProp("pinned", pinned)): + if(pinned and pin): + self.events.emit(SIGNAL_MESSAGE_PINNED, data) + else: + if(not pinned and not pin): + self.events.emit(SIGNAL_MESSAGE_UNPINNED, data) + + except Exception as e: + error "error: ", methodName="pinUnpinMessage", errName = e.name, errDesription = e.msg + + proc getDetailsForMessage*(self: Service, chatId: string, messageId: string): + tuple[message: MessageDto, reactions: seq[ReactionDto], error: string] = + try: + let msgResponse = status_go.fetchMessageByMessageId(messageId) + if(msgResponse.error.isNil): + result.message = msgResponse.result.toMessageDto() + + if(result.message.id.len == 0): + result.error = "message with id: " & messageId & " doesn't exist" + return + + let reactionsResponse = status_go.fetchReactionsForMessageWithId(chatId, messageId) + if(reactionsResponse.error.isNil): + result.reactions = map(reactionsResponse.result.getElems(), proc(x: JsonNode): ReactionDto = x.toReactionDto()) + + except Exception as e: + result.error = e.msg + error "error: ", methodName="getDetailsForMessage", errName = e.name, errDesription = e.msg \ No newline at end of file