move handling of message event sending to message.js

pull/1205/head
Audric Ackermann 5 years ago
parent 1a09ba0b35
commit 5610ec5a9f
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -990,7 +990,8 @@
});
},
/* Uploads attachments, previews and quotes.
/**
* Uploads attachments, previews and quotes.
* If body is too long then it is also converted to an attachment.
*
* @returns The uploaded data which includes: body, attachments, preview and quote.
@ -1215,6 +1216,75 @@
return errors[0][0];
},
async handleMessageSentSuccess(sentMessage) {
const sentTo = this.get('sent_to') || [];
const isOurDevice = window.libsession.Protocols.MultiDeviceProtocol.isOurDevice(
sentMessage.device
);
// Handle the sync logic here
if (!isOurDevice && !this.get('synced') && !this.get('sentSync')) {
const contentDecoded = textsecure.protobuf.Content.decode(
sentMessage.plainTextBuffer
);
const { dataMessage } = contentDecoded;
this.sendSyncMessageOnly(dataMessage);
this.set({ sentSync: true });
} else if (isOurDevice && this.get('sentSync')) {
this.set({ synced: true });
}
const primaryPubKey = await libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice(
sentMessage.device
);
this.set({
sent_to: _.union(sentTo, [primaryPubKey.key]),
sent: true,
expirationStartTimestamp: Date.now(),
// unidentifiedDeliveries: result.unidentifiedDeliveries,
});
await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message,
});
this.getConversation().updateLastMessage();
this.trigger('sent', this);
},
async handleMessageSentFailure(sentMessage, error) {
if (error instanceof Error) {
this.saveErrors(error);
if (error.name === 'SignedPreKeyRotationError') {
await window.getAccountManager().rotateSignedPreKey();
} else if (error.name === 'OutgoingIdentityKeyError') {
const c = ConversationController.get(sentMessage.device);
await c.getProfiles();
}
}
const expirationStartTimestamp = Date.now();
if (
sentMessage.device === window.textsecure.storage.user.getNumber() &&
!this.get('sync')
) {
this.set({ sentSync: false });
}
this.set({
sent: true,
expirationStartTimestamp,
// unidentifiedDeliveries: result.unidentifiedDeliveries,
});
await window.Signal.Data.saveMessage(this.attributes, {
Message: Whisper.Message,
});
this.trigger('change', this);
this.getConversation().updateLastMessage();
this.trigger('done');
},
getConversation() {
// This needs to be an unsafe call, because this method is called during
// initial module setup. We may be in the middle of the initial fetch to

@ -10,8 +10,6 @@
Whisper,
textsecure,
Signal,
libsession,
_
*/
// eslint-disable-next-line func-names
@ -299,87 +297,27 @@
const msg = conv.messageCollection.models.find(
convMsg => convMsg.id === tmpMsg.id
);
return { conv, msg };
return { msg };
},
async handleMessageSentSuccess(m) {
const fetchedData = await this.fetchHandleMessageSentData(m);
async handleMessageSentSuccess(sentMessage) {
const fetchedData = await this.fetchHandleMessageSentData(sentMessage);
if (!fetchedData) {
return;
}
const { msg, conv } = fetchedData;
const { msg } = fetchedData;
const sentTo = msg.get('sent_to') || [];
const isOurDevice = window.libsession.Protocols.MultiDeviceProtocol.isOurDevice(
m.device
);
// Handle the sync logic here
if (!isOurDevice && !msg.get('synced') && !msg.get('sentSync')) {
const contentDecoded = textsecure.protobuf.Content.decode(
m.plainTextBuffer
);
const { dataMessage } = contentDecoded;
msg.sendSyncMessageOnly(dataMessage);
msg.set({ sentSync: true });
} else if (isOurDevice && msg.get('sentSync')) {
msg.set({ synced: true });
}
const primaryPubKey = await libsession.Protocols.MultiDeviceProtocol.getPrimaryDevice(
m.device
);
msg.set({
sent_to: _.union(sentTo, [primaryPubKey.key]),
sent: true,
expirationStartTimestamp: Date.now(),
// unidentifiedDeliveries: result.unidentifiedDeliveries,
});
await window.Signal.Data.saveMessage(msg.attributes, {
Message: Whisper.Message,
});
conv.updateLastMessage();
msg.trigger('sent', msg);
msg.handleMessageSentSuccess(sentMessage);
},
async handleMessageSentFailure(m, error) {
const fetchedData = await this.fetchHandleMessageSentData(m);
async handleMessageSentFailure(sentMessage, error) {
const fetchedData = await this.fetchHandleMessageSentData(sentMessage);
if (!fetchedData) {
return;
}
const { msg, conv } = fetchedData;
if (error instanceof Error) {
msg.saveErrors(error);
if (error.name === 'SignedPreKeyRotationError') {
await window.getAccountManager().rotateSignedPreKey();
} else if (error.name === 'OutgoingIdentityKeyError') {
const c = ConversationController.get(m.device);
await c.getProfiles();
}
}
const expirationStartTimestamp = Date.now();
if (
m.device === window.textsecure.storage.user.getNumber() &&
!msg.get('sync')
) {
msg.set({ sentSync: false });
}
msg.set({
sent: true,
expirationStartTimestamp,
// unidentifiedDeliveries: result.unidentifiedDeliveries,
});
await window.Signal.Data.saveMessage(msg.attributes, {
Message: Whisper.Message,
});
msg.trigger('change', msg);
const { msg } = fetchedData;
conv.updateLastMessage();
msg.trigger('done');
await msg.handleMessageSentFailure(sentMessage, error);
},
startConnectionListener() {

Loading…
Cancel
Save