Skip to content

Commit

Permalink
refactor(messages): create a helper MessageItem init function
Browse files Browse the repository at this point in the history
Fixes #14039

Creates a util function to create MessageItems.
  • Loading branch information
jrainville committed Jan 6, 2025
1 parent cdeaf56 commit 4c18af4
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 398 deletions.
71 changes: 14 additions & 57 deletions src/app/modules/main/activity_center/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import NimQml, Tables, json, sequtils
import ./io_interface, ./view, ./controller, ./token_data_item
import ../io_interface as delegate_interface
import ./item as notification_item
import ../../shared_models/message_item as msg_item
import ../../shared_models/message_model
import ../../shared_models/message_item_qobject as msg_item_qobj
import ../../shared_models/message_transaction_parameters_item
import ../../../global/global_singleton
import ../../../global/app_sections_config as conf
import ../../../core/eventemitter
Expand Down Expand Up @@ -100,69 +99,27 @@ proc createMessageItemFromDto(self: Module, message: MessageDto, communityId: st
else:
quotedMessageAuthorDetails = self.controller.getContactDetails(message.quotedMessage.`from`)

var imagesAlbum: seq[string]
var albumImages: seq[string]
var albumMessageIds: seq[string]
if message.albumId != "":
for msg in albumMessages:
imagesAlbum.add(msg.image)
albumImages.add(msg.image)
albumMessageIds.add(msg.id)

return msg_item_qobj.newMessageItem(msg_item.initMessageItem(
message.id,
communityId, # we don't received community id via `activityCenterNotifications` api call
message.chatId,
message.responseTo,
message.`from`,
contactDetails.defaultDisplayName,
contactDetails.optionalName,
contactDetails.icon,
contactDetails.colorHash,
let messageItem = message_model.createMessageItemFromDtos(
message,
communityId,
contactDetails,
contactDetails.isCurrentUser,
contactDetails.dto.added,
message.outgoingStatus,
self.controller.getRenderedText(message.parsedText, communityChats),
self.controller.replacePubKeysWithDisplayNames(message.text),
message.parsedText,
message.image,
message.containsContactMentions(),
message.seen,
timestamp = message.timestamp,
clock = message.clock,
message.contentType,
message.messageType,
message.contactRequestState,
message.sticker.url,
message.sticker.pack,
message.links,
message.linkPreviews,
newTransactionParametersItem("","","","","","",-1,""),
message.mentionedUsersPks,
contactDetails.dto.trustStatus,
contactDetails.dto.ensVerified,
message.discordMessage,
resendError = "",
message.deleted,
message.deletedBy,
renderedMessageText = self.controller.getRenderedText(message.parsedText, communityChats),
clearText = self.controller.replacePubKeysWithDisplayNames(message.text),
albumImages,
albumMessageIds,
deletedByContactDetails = ContactDetails(),
message.pinnedBy,
message.mentioned,
message.quotedMessage.`from`,
message.quotedMessage.text,
self.controller.getRenderedText(message.quotedMessage.parsedText, communityChats),
message.quotedMessage.contentType,
message.quotedMessage.deleted,
message.quotedMessage.discordMessage,
quotedMessageAuthorDetails,
message.quotedMessage.albumImages,
message.quotedMessage.albumImagesCount,
message.albumId,
imagesAlbum,
albumMessageIds,
message.albumImagesCount,
message.bridgeMessage,
message.quotedMessage.bridgeMessage,
message.paymentRequests
))
quotedRenderedMessageText = self.controller.getRenderedText(message.quotedMessage.parsedText, communityChats),
)
return msg_item_qobj.newMessageItem(messageItem)

method convertToItems*(
self: Module,
Expand Down
218 changes: 47 additions & 171 deletions src/app/modules/main/chat_section/chat_content/messages/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import NimQml, chronicles, sequtils, uuids, sets, times, tables, system
import io_interface
import ../io_interface as delegate_interface
import view, controller
import ../../../../shared_models/message_model
import ../../../../shared_models/message_item
import ../../../../shared_models/[message_model, message_item]
import ../../../../shared_models/message_reaction_item
import ../../../../shared_models/message_transaction_parameters_item
import ../../../../shared_models/link_preview_model
import ../../../../../global/global_singleton
import ../../../../../core/eventemitter
Expand All @@ -16,6 +14,7 @@ import ../../../../../../app_service/service/chat/service as chat_service
import ../../../../../../app_service/service/message/service as message_service
import ../../../../../../app_service/service/mailservers/service as mailservers_service
import ../../../../../../app_service/service/shared_urls/service as shared_urls_service
import ../../../../../../app_service/service/contacts/dto/contact_details
import ../../../../../../app_service/common/types
import ../../../../../global/utils as utils
import ../../../../../global/global_singleton
Expand Down Expand Up @@ -149,69 +148,21 @@ proc createMessageItemsFromMessageDtos(self: Module, messages: seq[MessageDto],
if message.transactionParameters.fromAddress != "":
isCurrentUser = self.currentUserWalletContainsAddress(message.transactionParameters.fromAddress)

var item = initMessageItem(
message.id,
message.chatId,
var item = message_model.createMessageItemFromDtos(
message,
message.communityId,
message.responseTo,
message.`from`,
sender.defaultDisplayName,
sender.optionalName,
sender.icon,
sender.colorHash,
(isCurrentUser and message.contentType != ContentType.DiscordMessage),
sender.dto.added,
message.outgoingStatus,
sender,
isCurrentUser,
renderedMessageText,
message.text,
message.parsedText,
message.image,
message.containsContactMentions(),
message.seen,
timestamp = message.timestamp,
clock = message.clock,
message.contentType,
message.messageType,
message.contactRequestState,
sticker = message.sticker.url,
message.sticker.pack,
message.links,
message.linkPreviews,
newTransactionParametersItem(message.transactionParameters.id,
message.transactionParameters.fromAddress,
message.transactionParameters.address,
transactionContract,
transactionValue,
message.transactionParameters.transactionHash,
message.transactionParameters.commandState,
message.transactionParameters.signature),
message.mentionedUsersPks(),
sender.dto.trustStatus,
sender.dto.ensVerified,
message.discordMessage,
resendError = "",
message.deleted,
message.deletedBy,
clearText = message.text,
albumImages = @[],
albumMessageIds = @[],
deletedByContactDetails,
message.pinnedBy,
message.mentioned,
message.quotedMessage.`from`,
message.quotedMessage.text,
self.controller.getRenderedText(message.quotedMessage.parsedText, communityChats),
message.quotedMessage.contentType,
message.quotedMessage.deleted,
message.quotedMessage.discordMessage,
quotedMessageAuthorDetails,
message.quotedMessage.albumImages,
message.quotedMessage.albumImagesCount,
message.albumId,
if (len(message.albumId) == 0): @[] else: @[message.image],
if (len(message.albumId) == 0): @[] else: @[message.id],
message.albumImagesCount,
message.bridgeMessage,
message.quotedMessage.bridgeMessage,
message.paymentRequests
)
self.controller.getRenderedText(message.quotedMessage.parsedText, communityChats),
transactionContract,
transactionValue,
)

self.updateLinkPreviewsContacts(item, requestFromMailserver = item.seen)
self.updateLinkPreviewsCommunities(item, requestFromMailserver = item.seen)
Expand Down Expand Up @@ -239,61 +190,21 @@ proc createMessageItemsFromMessageDtos(self: Module, messages: seq[MessageDto],

proc createFetchMoreMessagesItem(self: Module): Item =
let chatDto = self.controller.getChatDetails()
result = initMessageItem(
FETCH_MORE_MESSAGES_MESSAGE_ID,
result = message_model.createMessageItemFromDtos(
message = MessageDto(
id: FETCH_MORE_MESSAGES_MESSAGE_ID,
clock: FETCH_MORE_MESSAGES_CLOCK,
contentType: ContentType.FetchMoreMessagesButton,
),
communityId = "",
chatId = "",
responseToMessageWithId = "",
senderId = chatDto.id,
senderDisplayName = "",
senderOptionalName = "",
senderIcon = "",
senderColorHash = "",
amISender = false,
senderIsAdded = false,
outgoingStatus = "",
text = "",
unparsedText = "",
parsedText = @[],
image = "",
messageContainsMentions = false,
seen = true,
timestamp = 0,
clock = FETCH_MORE_MESSAGES_CLOCK,
ContentType.FetchMoreMessagesButton,
messageType = -1,
contactRequestState = 0,
sticker = "",
stickerPack = -1,
links = @[],
linkPreviews = @[],
transactionParameters = newTransactionParametersItem("","","","","","",-1,""),
mentionedUsersPks = @[],
senderTrustStatus = TrustStatus.Unknown,
senderEnsVerified = false,
DiscordMessage(),
resendError = "",
deleted = false,
deletedBy = "",
deletedByContactDetails = ContactDetails(),
pinnedBy = "",
mentioned = false,
quotedMessageFrom = "",
quotedMessageText = "",
quotedMessageParsedText = "",
quotedMessageContentType = ContentType.Unknown,
quotedMessageDeleted = false,
quotedMessageDiscordMessage = DiscordMessage(),
quotedMessageAuthorDetails = ContactDetails(),
quotedMessageAlbumMessageImages = @[],
quotedMessageAlbumImagesCount = 0,
albumId = "",
albumMessageImages = @[],
albumMessageIds = @[],
albumImagesCount = 0,
BridgeMessage(),
BridgeMessage(),
paymentRequests = @[]
sender = ContactDetails(
dto: ContactsDto(
id: chatDto.id,
),
),
isCurrentUser = false,
renderedMessageText = "",
clearText = "",
)

proc createChatIdentifierItem(self: Module): Item =
Expand All @@ -303,67 +214,32 @@ proc createChatIdentifierItem(self: Module): Item =
var chatIcon = ""
var senderColorHash = ""
var senderIsAdded = false
if(chatDto.chatType == ChatType.OneToOne):
if chatDto.chatType == ChatType.OneToOne:
let sender = self.controller.getContactDetails(chatDto.id)
senderIsAdded = sender.dto.added
(chatName, smallImage, chatIcon) = self.controller.getOneToOneChatNameAndImage()
senderColorHash = sender.colorHash

result = initMessageItem(
CHAT_IDENTIFIER_MESSAGE_ID,
result = message_model.createMessageItemFromDtos(
message = MessageDto(
id: CHAT_IDENTIFIER_MESSAGE_ID,
clock: CHAT_IDENTIFIER_CLOCK,
contentType: ContentType.ChatIdentifier,
seen: true,
),
communityId = "",
chatId = "",
responseToMessageWithId = "",
senderId = chatDto.id,
senderDisplayName = chatName,
senderOptionalName = "",
senderIcon = chatIcon,
senderColorHash = senderColorHash,
amISender = false,
senderIsAdded,
outgoingStatus = "",
text = "",
unparsedText = "",
parsedText = @[],
image = "",
messageContainsMentions = false,
seen = true,
timestamp = 0,
clock = CHAT_IDENTIFIER_CLOCK,
ContentType.ChatIdentifier,
messageType = -1,
contactRequestState = 0,
sticker = "",
stickerPack = -1,
links = @[],
linkPreviews = @[],
transactionParameters = newTransactionParametersItem("","","","","","",-1,""),
mentionedUsersPks = @[],
senderTrustStatus = TrustStatus.Unknown,
senderEnsVerified = false,
DiscordMessage(),
resendError = "",
deleted = false,
deletedBy = "",
deletedByContactDetails = ContactDetails(),
pinnedBy = "",
mentioned = false,
quotedMessageFrom = "",
quotedMessageText = "",
quotedMessageParsedText = "",
quotedMessageContentType = ContentType.Unknown,
quotedMessageDeleted = false,
quotedMessageDiscordMessage = DiscordMessage(),
quotedMessageAuthorDetails = ContactDetails(),
quotedMessageAlbumMessageImages = @[],
quotedMessageAlbumImagesCount = 0,
albumId = "",
albumMessageImages = @[],
albumMessageIds = @[],
albumImagesCount = 0,
bridgeMessage = BridgeMessage(),
quotedBridgeMessage = BridgeMessage(),
paymentRequests = @[]
sender = ContactDetails(
defaultDisplayName: chatName,
icon: chatIcon,
colorHash: senderColorHash,
dto: ContactsDto(
id: chatDto.id,
added: senderIsAdded,
),
),
isCurrentUser = false,
renderedMessageText = "",
clearText = "",
)

proc checkIfMessageLoadedAndScroll(self: Module) =
Expand Down
Loading

0 comments on commit 4c18af4

Please sign in to comment.