New MessageController as the single place for in-memory messages

This commit is contained in:
Scott Nonnenberg 2019-03-25 18:10:30 -07:00
parent 274949b247
commit 74cb808763
11 changed files with 169 additions and 105 deletions

View file

@ -4,6 +4,7 @@
Backbone,
libphonenumber,
ConversationController,
MessageController,
libsignal,
storage,
textsecure,
@ -673,14 +674,6 @@
},
async onReadMessage(message, readAt) {
const existing = this.messageCollection.get(message.id);
if (existing) {
const fetched = await window.Signal.Data.getMessageById(existing.id, {
Message: Whisper.Message,
});
existing.merge(fetched);
}
// We mark as read everything older than this message - to clean up old stuff
// still marked unread in the database. If the user generally doesn't read in
// the desktop app, so the desktop app only gets read syncs, we can very
@ -883,10 +876,23 @@
recipients,
});
const message = this.addSingleMessage(messageWithSchema);
if (this.isPrivate()) {
messageWithSchema.destination = destination;
}
const attributes = {
...messageWithSchema,
id: window.getGuid(),
};
const model = this.addSingleMessage(attributes);
const message = MessageController.register(model.id, model);
await window.Signal.Data.saveMessage(message.attributes, {
forceSave: true,
Message: Whisper.Message,
});
this.set({
lastMessage: message.getNotificationText(),
lastMessage: model.getNotificationText(),
lastMessageStatus: 'sending',
active_at: now,
timestamp: now,
@ -896,15 +902,6 @@
Conversation: Whisper.Conversation,
});
if (this.isPrivate()) {
message.set({ destination });
}
const id = await window.Signal.Data.saveMessage(message.attributes, {
Message: Whisper.Message,
});
message.set({ id });
// We're offline!
if (!textsecure.messaging) {
const errors = this.contactCollection.map(contact => {
@ -1424,11 +1421,9 @@
let read = await Promise.all(
_.map(oldUnread, async providedM => {
let m = providedM;
const m = MessageController.register(providedM.id, providedM);
if (this.messageCollection.get(m.id)) {
m = this.messageCollection.get(m.id);
} else {
if (!this.messageCollection.get(m.id)) {
window.log.warn(
'Marked a message as read in the database, but ' +
'it was not in messageCollection.'

View file

@ -1,13 +1,16 @@
/* global _: false */
/* global Backbone: false */
/* global storage: false */
/* global filesize: false */
/* global ConversationController: false */
/* global getAccountManager: false */
/* global i18n: false */
/* global Signal: false */
/* global textsecure: false */
/* global Whisper: false */
/* global
_,
Backbone,
storage,
filesize,
ConversationController,
MessageController,
getAccountManager,
i18n,
Signal,
textsecure,
Whisper
*/
/* eslint-disable more/no-then */
@ -261,6 +264,7 @@
this.cleanup();
},
async cleanup() {
MessageController.unregister(this.id);
this.unload();
await deleteExternalMessageFiles(this.attributes);
},
@ -1394,17 +1398,18 @@
const collection = await window.Signal.Data.getMessagesBySentAt(id, {
MessageCollection: Whisper.MessageCollection,
});
const queryMessage = collection.find(item => {
const found = collection.find(item => {
const messageAuthor = item.getContact();
return messageAuthor && author === messageAuthor.id;
});
if (!queryMessage) {
if (!found) {
quote.referencedMessageNotFound = true;
return message;
}
const queryMessage = MessageController.register(found.id, found);
quote.text = queryMessage.get('body');
if (firstAttachment) {
firstAttachment.thumbnail = null;
@ -1730,6 +1735,7 @@
Message: Whisper.Message,
});
message.set({ id });
MessageController.register(message.id, message);
// Note that this can save the message again, if jobs were queued. We need to
// call it after we have an id for this message, because the jobs refer back
@ -1933,7 +1939,9 @@
}
);
const models = messages.filter(message => Boolean(message.id));
const models = messages
.filter(message => Boolean(message.id))
.map(message => MessageController.register(message.id, message));
const eliminated = messages.length - models.length;
if (eliminated > 0) {
window.log.warn(