Conversation: savePromises for off-queue post-handleDataMessage work

This commit is contained in:
Scott Nonnenberg 2024-08-28 16:05:08 +10:00 committed by GitHub
parent 059c971ee2
commit 768ab26a20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 145 additions and 62 deletions

View file

@ -157,6 +157,7 @@ import {
copyQuoteContentFromOriginal,
} from '../messages/copyQuote';
import { getRoomIdFromCallLink } from '../util/callLinksRingrtc';
import { explodePromise } from '../util/explodePromise';
/* eslint-disable more/no-then */
@ -2117,7 +2118,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
}
log.info(`${idLog}: Batching save`);
void this.saveAndNotify(conversation, confirm);
drop(this.saveAndNotify(conversation, confirm));
} catch (error) {
const errorForLog = Errors.toLogFormat(error);
log.error(`${idLog}: error:`, errorForLog);
@ -2130,40 +2131,51 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
conversation: ConversationModel,
confirm: () => void
): Promise<void> {
await saveNewMessageBatcher.add(this.attributes);
const { resolve, promise } = explodePromise<void>();
try {
conversation.addSavePromise(promise);
log.info('Message saved', this.get('sent_at'));
await saveNewMessageBatcher.add(this.attributes);
// Once the message is saved to DB, we queue attachment downloads
await this.handleAttachmentDownloadsForNewMessage(conversation);
log.info('Message saved', this.get('sent_at'));
// We'd like to check for deletions before scheduling downloads, but if an edit comes
// in, we want to have kicked off attachment downloads for the original message.
const isFirstRun = false;
const result = await this.modifyTargetMessage(conversation, isFirstRun);
if (result === ModifyTargetMessageResult.Deleted) {
// Once the message is saved to DB, we queue attachment downloads
await this.handleAttachmentDownloadsForNewMessage(conversation);
// We'd like to check for deletions before scheduling downloads, but if an edit
// comes in, we want to have kicked off attachment downloads for the original
// message.
const isFirstRun = false;
const result = await this.modifyTargetMessage(conversation, isFirstRun);
if (result === ModifyTargetMessageResult.Deleted) {
confirm();
return;
}
conversation.trigger('newmessage', this.attributes);
if (await shouldReplyNotifyUser(this.attributes, conversation)) {
await conversation.notify(this.attributes);
}
// Increment the sent message count if this is an outgoing message
if (this.get('type') === 'outgoing') {
conversation.incrementSentMessageCount();
}
window.Whisper.events.trigger('incrementProgress');
confirm();
return;
}
conversation.trigger('newmessage', this.attributes);
if (await shouldReplyNotifyUser(this.attributes, conversation)) {
await conversation.notify(this.attributes);
}
// Increment the sent message count if this is an outgoing message
if (this.get('type') === 'outgoing') {
conversation.incrementSentMessageCount();
}
window.Whisper.events.trigger('incrementProgress');
confirm();
if (!isStory(this.attributes)) {
drop(
conversation.queueJob('updateUnread', () => conversation.updateUnread())
);
if (!isStory(this.attributes)) {
drop(
conversation.queueJob('updateUnread', () =>
conversation.updateUnread()
)
);
}
} finally {
resolve();
conversation.removeSavePromise(promise);
}
}