Skip to content

Commit

Permalink
feat(chat): add messages in bulk instead one by one
Browse files Browse the repository at this point in the history
iterates: #3067
  • Loading branch information
osmaczko committed Jan 16, 2023
1 parent a21bebc commit b26565e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ proc init*(self: Controller) =
var args = MessagesArgs(e)
if(self.chatId != args.chatId):
return
for message in args.messages:
self.delegate.messageAdded(message)
self.delegate.messagesAdded(args.messages)

self.events.on(SIGNAL_SENDING_SUCCESS) do(e:Args):
let args = MessageSendingSuccess(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ method onPinMessage*(self: AccessInterface, messageId: string, actionInitiatedBy
method onUnpinMessage*(self: AccessInterface, messageId: string) {.base.} =
raise newException(ValueError, "No implementation available")

method messageAdded*(self: AccessInterface, message: MessageDto) {.base.} =
method messagesAdded*(self: AccessInterface, messages: seq[MessageDto]) {.base.} =
raise newException(ValueError, "No implementation available")

method onSendingMessageSuccess*(self: AccessInterface, message: MessageDto) {.base.} =
Expand Down
137 changes: 71 additions & 66 deletions src/app/modules/main/chat_section/chat_content/messages/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -289,79 +289,84 @@ method newMessagesLoaded*(self: Module, messages: seq[MessageDto], reactions: se

self.view.initialMessagesAreLoaded()

method messageAdded*(self: Module, message: MessageDto) =
let sender = self.controller.getContactDetails(message.`from`)

let renderedMessageText = self.controller.getRenderedText(message.parsedText)

var transactionContract = message.transactionParameters.contract
var transactionValue = message.transactionParameters.value
var isCurrentUser = sender.isCurrentUser
if message.contentType.ContentType == ContentType.Transaction:
(transactionContract, transactionValue) = self.controller.getTransactionDetails(message)
if message.transactionParameters.fromAddress != "":
isCurrentUser = self.currentUserWalletContainsAddress(message.transactionParameters.fromAddress)
# remove a message which has replace parameters filled
let index = self.view.model().findIndexForMessageId(message.replace)
if(index != -1):
self.view.model().removeItem(message.replace)
method messagesAdded*(self: Module, messages: seq[MessageDto]) =
var items: seq[Item]

# https://github.com/status-im/status-desktop/issues/7632 will introduce deleteFroMe feature.
# Now we just skip deleted messages
if message.deleted or message.deletedForMe:
return
for message in messages:
let sender = self.controller.getContactDetails(message.`from`)

var item = initItem(
message.id,
message.communityId,
message.responseTo,
message.`from`,
sender.defaultDisplayName,
sender.optionalName,
sender.icon,
(isCurrentUser and message.contentType.ContentType != ContentType.DiscordMessage),
sender.details.added,
message.outgoingStatus,
renderedMessageText,
message.text,
message.image,
message.containsContactMentions(),
message.seen,
timestamp = message.whisperTimestamp,
clock = message.clock,
message.contentType.ContentType,
message.messageType,
message.contactRequestState,
sticker = message.sticker.url,
message.sticker.pack,
message.links,
newTransactionParametersItem(message.transactionParameters.id,
message.transactionParameters.fromAddress,
message.transactionParameters.address,
transactionContract,
transactionValue,
message.transactionParameters.transactionHash,
message.transactionParameters.commandState,
message.transactionParameters.signature),
message.mentionedUsersPks,
sender.details.trustStatus,
sender.details.ensVerified,
message.discordMessage,
resendError = "",
message.mentioned,
message.quotedMessage.`from`,
message.quotedMessage.text,
self.controller.getRenderedText(message.quotedMessage.parsedText),
message.quotedMessage.contentType,
message.quotedMessage.deleted,
)
let renderedMessageText = self.controller.getRenderedText(message.parsedText)

var transactionContract = message.transactionParameters.contract
var transactionValue = message.transactionParameters.value
var isCurrentUser = sender.isCurrentUser
if message.contentType.ContentType == ContentType.Transaction:
(transactionContract, transactionValue) = self.controller.getTransactionDetails(message)
if message.transactionParameters.fromAddress != "":
isCurrentUser = self.currentUserWalletContainsAddress(message.transactionParameters.fromAddress)
# remove a message which has replace parameters filled
let index = self.view.model().findIndexForMessageId(message.replace)
if(index != -1):
self.view.model().removeItem(message.replace)

# https://github.com/status-im/status-desktop/issues/7632 will introduce deleteFroMe feature.
# Now we just skip deleted messages
if message.deleted or message.deletedForMe:
return

var item = initItem(
message.id,
message.communityId,
message.responseTo,
message.`from`,
sender.defaultDisplayName,
sender.optionalName,
sender.icon,
(isCurrentUser and message.contentType.ContentType != ContentType.DiscordMessage),
sender.details.added,
message.outgoingStatus,
renderedMessageText,
message.text,
message.image,
message.containsContactMentions(),
message.seen,
timestamp = message.whisperTimestamp,
clock = message.clock,
message.contentType.ContentType,
message.messageType,
message.contactRequestState,
sticker = message.sticker.url,
message.sticker.pack,
message.links,
newTransactionParametersItem(message.transactionParameters.id,
message.transactionParameters.fromAddress,
message.transactionParameters.address,
transactionContract,
transactionValue,
message.transactionParameters.transactionHash,
message.transactionParameters.commandState,
message.transactionParameters.signature),
message.mentionedUsersPks,
sender.details.trustStatus,
sender.details.ensVerified,
message.discordMessage,
resendError = "",
message.mentioned,
message.quotedMessage.`from`,
message.quotedMessage.text,
self.controller.getRenderedText(message.quotedMessage.parsedText),
message.quotedMessage.contentType,
message.quotedMessage.deleted,
)

items.add(item)

self.view.model().insertItemBasedOnClock(item)
self.view.model().insertItemsBasedOnClock(items)

method removeNewMessagesMarker*(self: Module)

method onSendingMessageSuccess*(self: Module, message: MessageDto) =
self.messageAdded(message)
self.messagesAdded(@[message])
self.view.emitSendingMessageSuccessSignal()
self.removeNewMessagesMarker()

Expand Down

0 comments on commit b26565e

Please sign in to comment.