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
/**
* 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({
providedDisappearingMode,
providedExpireTimer,
@ -820,10 +832,10 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
fromSync?: boolean;
shouldCommit?: boolean;
existingMessage?: MessageModel;
}): Promise<void> {
}): Promise<boolean> {
if (this.isPublic()) {
window.log.warn("updateExpireTimer() Disappearing messages aren't supported in communities");
return;
return false;
}
let expirationMode = providedDisappearingMode;
let expireTimer = providedExpireTimer;
@ -844,7 +856,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
'WIP: updateExpireTimer() This is an outdated disappearing message setting',
`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
@ -858,7 +870,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
fromSync ? 'config/sync ' : ''
}message as we already have the same one set.`
);
return;
return false;
}
const isOutgoing = Boolean(!receivedAt);
@ -935,16 +947,22 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
window.log.debug(
`WIP: updateExpireTimer() We dont send an ExpireTimerUpdate because this was a remote change receivedAt: ${receivedAt} fromSync: ${fromSync}`
);
if (expirationMode !== 'off' && !message.getExpirationStartTimestamp()) {
message.set({
expirationStartTimestamp: setExpirationStartTimestamp(
expirationMode,
message.get('sent_at'),
'updateExpireTimer() remote change'
),
});
if (!message.getExpirationStartTimestamp()) {
const canBeDeleteAfterSend = this.isMe() || this.isGroup();
if (
(canBeDeleteAfterSend && expirationMode === 'legacy') ||
expirationMode === 'deleteAfterSend'
) {
message.set({
expirationStartTimestamp: setExpirationStartTimestamp(
expirationMode,
message.get('sent_at'),
'updateExpireTimer() remote change'
),
});
}
}
return;
return true;
}
const expireUpdate = {
@ -959,7 +977,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// TODO Check that the args are correct
if (expireUpdate.expirationType === 'deleteAfterRead') {
window.log.info('Note to Self messages cannot be delete after read!');
return;
return true;
}
const expirationTimerMessage = new ExpirationTimerUpdateMessage(expireUpdate);
@ -970,7 +988,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
// );
await message?.sendSyncMessageOnly(expirationTimerMessage);
return;
return true;
}
if (this.isPrivate()) {
@ -987,7 +1005,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
expirationTimerMessage,
SnodeNamespaces.UserMessages
);
return;
return true;
}
if (this.isClosedGroup()) {
const expireUpdateForGroup = {
@ -1006,7 +1024,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
message: expirationTimerMessage,
namespace: SnodeNamespaces.ClosedGroupMessage,
});
return;
return true;
}
throw new Error('Communities should not use disappearing messages');
}

@ -231,7 +231,7 @@ async function handleUserProfileUpdate(result: IncomingConfResult): Promise<Inco
if (wrapperNoteToSelfExpirySeconds !== expireTimer) {
// TODO legacy messages support will be removed in a future release
await ourConvo.updateExpireTimer({
const success = await ourConvo.updateExpireTimer({
providedDisappearingMode:
wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
@ -245,16 +245,20 @@ async function handleUserProfileUpdate(result: IncomingConfResult): Promise<Inco
fromSync: true,
shouldCommit: false,
});
changes = true;
window.log.debug(
`WIP: [userProfileWrapper] updating disappearing messages to expiratonMode: ${
wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
? 'deleteAfterSend'
: 'legacy'
: 'off'
} wrapperNoteToSelfExpirySeconds: ${wrapperNoteToSelfExpirySeconds}`
);
changes = success;
if (success) {
window.log.debug(
`WIP: [userProfileWrapper] updating disappearing messages for\nconvoId:${
ourConvo.id
} expiratonMode: ${
wrapperNoteToSelfExpirySeconds && wrapperNoteToSelfExpirySeconds > 0
? ReleasedFeatures.isDisappearMessageV2FeatureReleasedCached()
? '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
@ -391,7 +395,7 @@ async function handleContactsUpdate(result: IncomingConfResult): Promise<Incomin
wrapperConvo.expirationTimerSeconds !== contactConvo.getExpireTimer() ||
wrapperConvo.expirationMode !== contactConvo.getExpirationMode()
) {
await contactConvo.updateExpireTimer({
const success = await contactConvo.updateExpireTimer({
providedDisappearingMode: wrapperConvo.expirationMode,
providedExpireTimer: wrapperConvo.expirationTimerSeconds,
providedChangeTimestamp: result.latestEnvelopeTimestamp,
@ -400,10 +404,12 @@ async function handleContactsUpdate(result: IncomingConfResult): Promise<Incomin
fromSync: true,
shouldCommit: false,
});
changes = true;
window.log.debug(
`WIP: [contactsWrapper] updating disappearing messages for\nconvoId:${wrapperConvo.id} expirationMode: ${wrapperConvo.expirationMode} expirationTimerSeconds: ${wrapperConvo.expirationTimerSeconds}`
);
changes = success;
if (success) {
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.
@ -614,17 +620,44 @@ async function handleLegacyGroupUpdate(latestEnvelopeTimestamp: number) {
legacyGroupConvo.get('active_at') < latestEnvelopeTimestamp
? legacyGroupConvo.get('active_at')
: latestEnvelopeTimestamp,
expirationType:
fromWrapper.disappearingTimerSeconds && fromWrapper.disappearingTimerSeconds > 0
? 'deleteAfterSend'
: 'unknown',
expireTimer: fromWrapper.disappearingTimerSeconds,
};
await ClosedGroup.updateOrCreateClosedGroup(groupDetails);
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 existingJoinedAtSeconds = Math.floor(existingTimestampMs / 1000);
if (existingJoinedAtSeconds !== fromWrapper.joinedAtSeconds) {
@ -671,7 +704,6 @@ async function handleUserGroupsUpdate(result: IncomingConfResult): Promise<Incom
case 'Community':
await handleCommunitiesUpdate();
break;
case 'LegacyGroup':
await handleLegacyGroupUpdate(result.latestEnvelopeTimestamp);
break;

@ -436,7 +436,7 @@ export async function handleMessageJob(
}
const expireTimerUpdate = expirationTimerUpdate?.expireTimer || 0;
const expirationTypeUpdate = changeToDisappearingConversationMode(
const expirationModeUpdate = changeToDisappearingConversationMode(
conversation,
expirationTimerUpdate?.expirationType,
expireTimerUpdate
@ -455,7 +455,7 @@ export async function handleMessageJob(
}
await conversation.updateExpireTimer({
providedDisappearingMode: expirationTypeUpdate,
providedDisappearingMode: expirationModeUpdate,
providedExpireTimer: expireTimerUpdate,
providedChangeTimestamp: lastDisappearingMessageChangeTimestamp,
providedSource: source,
@ -465,6 +465,9 @@ export async function handleMessageJob(
shouldCommit: false,
// 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 {
// this does not commit to db nor UI unless we need to approve a convo
await handleRegularMessage(

Loading…
Cancel
Save