feat: updateExpireTimer now returns success

we use this to decide on commiting updates from sync messages to the db
pull/2971/head
William Grant 2 years ago
parent 5b7afe0a29
commit 27a634b268

@ -802,6 +802,18 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
} }
// tslint:disable: cyclomatic-complexity // tslint:disable: cyclomatic-complexity
/**
* Updates the disappearing message settings for this conversation and sends an ExpirationTimerUpdate message if required
* @param providedDisappearingMode
* @param providedExpireTimer
* @param providedChangeTimestamp the timestamp of when the change was made
* @param providedSource the pubkey of the user who made the change
* @param receivedAt the timestamp of when the change was received
* @param fromSync if the change was made from a sync message
* @param shouldCommit if the change should be committed to the DB
* @param existingMessage if we have an existing message model to update
* @returns true, if the change was made or false if it was ignored
*/
public async updateExpireTimer({ public async updateExpireTimer({
providedDisappearingMode, providedDisappearingMode,
providedExpireTimer, providedExpireTimer,
@ -820,10 +832,10 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
fromSync?: boolean; fromSync?: boolean;
shouldCommit?: boolean; shouldCommit?: boolean;
existingMessage?: MessageModel; existingMessage?: MessageModel;
}): Promise<void> { }): Promise<boolean> {
if (this.isPublic()) { if (this.isPublic()) {
window.log.warn("updateExpireTimer() Disappearing messages aren't supported in communities"); window.log.warn("updateExpireTimer() Disappearing messages aren't supported in communities");
return; return false;
} }
let expirationMode = providedDisappearingMode; let expirationMode = providedDisappearingMode;
let expireTimer = providedExpireTimer; let expireTimer = providedExpireTimer;
@ -844,7 +856,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
'WIP: updateExpireTimer() This is an outdated disappearing message setting', 'WIP: updateExpireTimer() This is an outdated disappearing message setting',
`fromSync: ${fromSync}` `fromSync: ${fromSync}`
); );
return; return false;
} }
// NOTE: We don' mind if the message is the same, we still want to update the conversation because we want to show visible control messages we receive an ExpirationTimerUpdate // NOTE: We don' mind if the message is the same, we still want to update the conversation because we want to show visible control messages we receive an ExpirationTimerUpdate
@ -858,7 +870,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
fromSync ? 'config/sync ' : '' fromSync ? 'config/sync ' : ''
}message as we already have the same one set.` }message as we already have the same one set.`
); );
return; return false;
} }
const isOutgoing = Boolean(!receivedAt); const isOutgoing = Boolean(!receivedAt);
@ -935,16 +947,22 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
window.log.debug( window.log.debug(
`WIP: updateExpireTimer() We dont send an ExpireTimerUpdate because this was a remote change receivedAt: ${receivedAt} fromSync: ${fromSync}` `WIP: updateExpireTimer() We dont send an ExpireTimerUpdate because this was a remote change receivedAt: ${receivedAt} fromSync: ${fromSync}`
); );
if (expirationMode !== 'off' && !message.getExpirationStartTimestamp()) { if (!message.getExpirationStartTimestamp()) {
message.set({ const canBeDeleteAfterSend = this.isMe() || this.isGroup();
expirationStartTimestamp: setExpirationStartTimestamp( if (
expirationMode, (canBeDeleteAfterSend && expirationMode === 'legacy') ||
message.get('sent_at'), expirationMode === 'deleteAfterSend'
'updateExpireTimer() remote change' ) {
), message.set({
}); expirationStartTimestamp: setExpirationStartTimestamp(
expirationMode,
message.get('sent_at'),
'updateExpireTimer() remote change'
),
});
}
} }
return; return true;
} }
const expireUpdate = { const expireUpdate = {
@ -959,7 +977,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// TODO Check that the args are correct // TODO Check that the args are correct
if (expireUpdate.expirationType === 'deleteAfterRead') { if (expireUpdate.expirationType === 'deleteAfterRead') {
window.log.info('Note to Self messages cannot be delete after read!'); window.log.info('Note to Self messages cannot be delete after read!');
return; return true;
} }
const expirationTimerMessage = new ExpirationTimerUpdateMessage(expireUpdate); const expirationTimerMessage = new ExpirationTimerUpdateMessage(expireUpdate);
@ -970,7 +988,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// ); // );
await message?.sendSyncMessageOnly(expirationTimerMessage); await message?.sendSyncMessageOnly(expirationTimerMessage);
return; return true;
} }
if (this.isPrivate()) { if (this.isPrivate()) {
@ -987,7 +1005,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
expirationTimerMessage, expirationTimerMessage,
SnodeNamespaces.UserMessages SnodeNamespaces.UserMessages
); );
return; return true;
} }
if (this.isClosedGroup()) { if (this.isClosedGroup()) {
const expireUpdateForGroup = { const expireUpdateForGroup = {
@ -1006,7 +1024,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
message: expirationTimerMessage, message: expirationTimerMessage,
namespace: SnodeNamespaces.ClosedGroupMessage, namespace: SnodeNamespaces.ClosedGroupMessage,
}); });
return; return true;
} }
throw new Error('Communities should not use disappearing messages'); throw new Error('Communities should not use disappearing messages');
} }

@ -231,7 +231,7 @@ async function handleUserProfileUpdate(result: IncomingConfResult): Promise<Inco
if (wrapperNoteToSelfExpirySeconds !== expireTimer) { if (wrapperNoteToSelfExpirySeconds !== expireTimer) {
// TODO legacy messages support will be removed in a future release // TODO legacy messages support will be removed in a future release
await ourConvo.updateExpireTimer({ const success = await ourConvo.updateExpireTimer({
providedDisappearingMode: providedDisappearingMode:
wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0 wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached() ? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
@ -245,16 +245,20 @@ async function handleUserProfileUpdate(result: IncomingConfResult): Promise<Inco
fromSync: true, fromSync: true,
shouldCommit: false, shouldCommit: false,
}); });
changes = true; changes = success;
window.log.debug( if (success) {
`WIP: [userProfileWrapper] updating disappearing messages to expiratonMode: ${ window.log.debug(
wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0 `WIP: [userProfileWrapper] updating disappearing messages for\nconvoId:${
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached() ourConvo.id
? 'deleteAfterSend' } expiratonMode: ${
: 'legacy' wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0
: 'off' ? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
} wrapperNoteToSelfExpirySeconds: ${wrapperNoteToSelfExpirySeconds}` ? 'deleteAfterSend'
); : 'legacy'
: 'off'
} wrapperNoteToSelfExpirySeconds: ${wrapperNoteToSelfExpirySeconds}`
);
}
} }
// make sure to write the changes to the database now as the `AvatarDownloadJob` triggered by updateOurProfileLegacyOrViaLibSession might take some time before getting run // make sure to write the changes to the database now as the `AvatarDownloadJob` triggered by updateOurProfileLegacyOrViaLibSession might take some time before getting run
@ -391,7 +395,7 @@ async function handleContactsUpdate(result: IncomingConfResult): Promise<Incomin
wrapperConvo.expirationTimerSeconds !== contactConvo.getExpireTimer() || wrapperConvo.expirationTimerSeconds !== contactConvo.getExpireTimer() ||
wrapperConvo.expirationMode !== contactConvo.getExpirationMode() wrapperConvo.expirationMode !== contactConvo.getExpirationMode()
) { ) {
await contactConvo.updateExpireTimer({ const success = await contactConvo.updateExpireTimer({
providedDisappearingMode: wrapperConvo.expirationMode, providedDisappearingMode: wrapperConvo.expirationMode,
providedExpireTimer: wrapperConvo.expirationTimerSeconds, providedExpireTimer: wrapperConvo.expirationTimerSeconds,
providedChangeTimestamp: result.latestEnvelopeTimestamp, providedChangeTimestamp: result.latestEnvelopeTimestamp,
@ -400,10 +404,12 @@ async function handleContactsUpdate(result: IncomingConfResult): Promise<Incomin
fromSync: true, fromSync: true,
shouldCommit: false, shouldCommit: false,
}); });
changes = true; changes = success;
window.log.debug( if (success) {
`WIP: [contactsWrapper] updating disappearing messages for\nconvoId:${wrapperConvo.id} expirationMode: ${wrapperConvo.expirationMode} expirationTimerSeconds: ${wrapperConvo.expirationTimerSeconds}` window.log.debug(
); `WIP: [contactsWrapper] updating disappearing messages for\nconvoId:${wrapperConvo.id} expirationMode: ${wrapperConvo.expirationMode} expirationTimerSeconds: ${wrapperConvo.expirationTimerSeconds}`
);
}
} }
// we want to set the active_at to the created_at timestamp if active_at is unset, so that it shows up in our list. // we want to set the active_at to the created_at timestamp if active_at is unset, so that it shows up in our list.
@ -614,17 +620,44 @@ async function handleLegacyGroupUpdate(latestEnvelopeTimestamp: number) {
legacyGroupConvo.get('active_at') < latestEnvelopeTimestamp legacyGroupConvo.get('active_at') < latestEnvelopeTimestamp
? legacyGroupConvo.get('active_at') ? legacyGroupConvo.get('active_at')
: latestEnvelopeTimestamp, : latestEnvelopeTimestamp,
expirationType:
fromWrapper.disappearingTimerSeconds && fromWrapper.disappearingTimerSeconds > 0
? 'deleteAfterSend'
: 'unknown',
expireTimer: fromWrapper.disappearingTimerSeconds,
}; };
await ClosedGroup.updateOrCreateClosedGroup(groupDetails); await ClosedGroup.updateOrCreateClosedGroup(groupDetails);
let changes = await legacyGroupConvo.setPriorityFromWrapper(fromWrapper.priority, false); let changes = await legacyGroupConvo.setPriorityFromWrapper(fromWrapper.priority, false);
if (fromWrapper.disappearingTimerSeconds !== legacyGroupConvo.getExpireTimer()) {
// TODO legacy messages support will be removed in a future release
const success = await legacyGroupConvo.updateExpireTimer({
providedDisappearingMode:
fromWrapper.disappearingTimerSeconds && fromWrapper.disappearingTimerSeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
? 'deleteAfterSend'
: 'legacy'
: 'off',
providedExpireTimer: fromWrapper.disappearingTimerSeconds,
providedChangeTimestamp: latestEnvelopeTimestamp,
providedSource: legacyGroupConvo.id,
receivedAt: latestEnvelopeTimestamp,
fromSync: true,
shouldCommit: false,
});
changes = success;
if (success) {
window.log.debug(
`WIP: [userGroupWrapper] updating disappearing messages for\nconvoId:${
legacyGroupConvo.id
} expiratonMode: ${
fromWrapper.disappearingTimerSeconds && fromWrapper.disappearingTimerSeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
? 'deleteAfterSend'
: 'legacy'
: 'off'
} wrapperNoteToSelfExpirySeconds: ${fromWrapper.disappearingTimerSeconds}`
);
}
}
const existingTimestampMs = legacyGroupConvo.get('lastJoinedTimestamp'); const existingTimestampMs = legacyGroupConvo.get('lastJoinedTimestamp');
const existingJoinedAtSeconds = Math.floor(existingTimestampMs / 1000); const existingJoinedAtSeconds = Math.floor(existingTimestampMs / 1000);
if (existingJoinedAtSeconds !== fromWrapper.joinedAtSeconds) { if (existingJoinedAtSeconds !== fromWrapper.joinedAtSeconds) {
@ -671,7 +704,6 @@ async function handleUserGroupsUpdate(result: IncomingConfResult): Promise<Incom
case 'Community': case 'Community':
await handleCommunitiesUpdate(); await handleCommunitiesUpdate();
break; break;
case 'LegacyGroup': case 'LegacyGroup':
await handleLegacyGroupUpdate(result.latestEnvelopeTimestamp); await handleLegacyGroupUpdate(result.latestEnvelopeTimestamp);
break; break;

@ -436,7 +436,7 @@ export async function handleMessageJob(
} }
const expireTimerUpdate = expirationTimerUpdate?.expireTimer || 0; const expireTimerUpdate = expirationTimerUpdate?.expireTimer || 0;
const expirationTypeUpdate = changeToDisappearingConversationMode( const expirationModeUpdate = changeToDisappearingConversationMode(
conversation, conversation,
expirationTimerUpdate?.expirationType, expirationTimerUpdate?.expirationType,
expireTimerUpdate expireTimerUpdate
@ -455,7 +455,7 @@ export async function handleMessageJob(
} }
await conversation.updateExpireTimer({ await conversation.updateExpireTimer({
providedDisappearingMode: expirationTypeUpdate, providedDisappearingMode: expirationModeUpdate,
providedExpireTimer: expireTimerUpdate, providedExpireTimer: expireTimerUpdate,
providedChangeTimestamp: lastDisappearingMessageChangeTimestamp, providedChangeTimestamp: lastDisappearingMessageChangeTimestamp,
providedSource: source, providedSource: source,
@ -465,6 +465,9 @@ export async function handleMessageJob(
shouldCommit: false, shouldCommit: false,
// NOTE we don't commit yet because we want to get the message id, see below // NOTE we don't commit yet because we want to get the message id, see below
}); });
window.log.debug(
`WIP: [handleMessageJob] updating disappearing messages to expiratonModeUpdate: ${expirationModeUpdate} expireTimerUpdate: ${expireTimerUpdate}`
);
} else { } else {
// this does not commit to db nor UI unless we need to approve a convo // this does not commit to db nor UI unless we need to approve a convo
await handleRegularMessage( await handleRegularMessage(

Loading…
Cancel
Save