Only increment timer version once

This commit is contained in:
Fedor Indutny 2024-08-21 14:48:24 -07:00 committed by GitHub
parent a435b21a56
commit 129a22e91d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 10 deletions

View file

@ -81,7 +81,7 @@ export async function sendDirectExpirationTimerUpdate(
expireTimer === undefined expireTimer === undefined
? undefined ? undefined
: DurationInSeconds.fromSeconds(expireTimer), : DurationInSeconds.fromSeconds(expireTimer),
expireTimerVersion: await conversation.incrementAndGetExpireTimerVersion(), expireTimerVersion: conversation.getExpireTimerVersion(),
flags, flags,
profileKey, profileKey,
recipients: conversation.getRecipients(), recipients: conversation.getRecipients(),

View file

@ -224,6 +224,8 @@ const ATTRIBUTES_THAT_DONT_INVALIDATE_PROPS_CACHE = new Set([
'storageUnknownFields', 'storageUnknownFields',
]); ]);
const MAX_EXPIRE_TIMER_VERSION = 0xffffffff;
type CachedIdenticon = { type CachedIdenticon = {
readonly color: AvatarColorType; readonly color: AvatarColorType;
readonly text?: string; readonly text?: string;
@ -4520,6 +4522,7 @@ export class ConversationModel extends window.Backbone
log.info(`${logId}: queuing send job`); log.info(`${logId}: queuing send job`);
// if change wasn't made remotely, send it to the number/group // if change wasn't made remotely, send it to the number/group
try { try {
await this.incrementExpireTimerVersion();
await conversationJobQueue.add({ await conversationJobQueue.add({
type: conversationQueueJobEnum.enum.DirectExpirationTimerUpdate, type: conversationQueueJobEnum.enum.DirectExpirationTimerUpdate,
conversationId: this.id, conversationId: this.id,
@ -5157,33 +5160,32 @@ export class ConversationModel extends window.Backbone
getExpireTimerVersion(): number | undefined { getExpireTimerVersion(): number | undefined {
return isDirectConversation(this.attributes) return isDirectConversation(this.attributes)
? this.get('expireTimerVersion') ? Math.min(this.get('expireTimerVersion') || 0, MAX_EXPIRE_TIMER_VERSION)
: undefined; : undefined;
} }
async incrementAndGetExpireTimerVersion(): Promise<number | undefined> { async incrementExpireTimerVersion(): Promise<void> {
const logId = `incrementAndGetExpireTimerVersion(${this.idForLogging()})`; const logId = `incrementExpireTimerVersion(${this.idForLogging()})`;
if (!isDirectConversation(this.attributes)) { if (!isDirectConversation(this.attributes)) {
return undefined; return;
} }
const { expireTimerVersion, capabilities } = this.attributes; const { expireTimerVersion, capabilities } = this.attributes;
// This should not happen in practice, but be ready to handle // This should not happen in practice, but be ready to handle
const MAX_EXPIRE_TIMER_VERSION = 0xffffffff;
if (expireTimerVersion >= MAX_EXPIRE_TIMER_VERSION) { if (expireTimerVersion >= MAX_EXPIRE_TIMER_VERSION) {
log.warn(`${logId}: expire version overflow`); log.warn(`${logId}: expire version overflow`);
return MAX_EXPIRE_TIMER_VERSION; return;
} }
if (expireTimerVersion <= 2) { if (expireTimerVersion <= 2) {
if (!capabilities?.versionedExpirationTimer) { if (!capabilities?.versionedExpirationTimer) {
log.warn(`${logId}: missing recipient capability`); log.warn(`${logId}: missing recipient capability`);
return expireTimerVersion; return;
} }
const me = window.ConversationController.getOurConversationOrThrow(); const me = window.ConversationController.getOurConversationOrThrow();
if (!me.get('capabilities')?.versionedExpirationTimer) { if (!me.get('capabilities')?.versionedExpirationTimer) {
log.warn(`${logId}: missing sender capability`); log.warn(`${logId}: missing sender capability`);
return expireTimerVersion; return;
} }
// Increment only if sender and receiver are both capable // Increment only if sender and receiver are both capable
@ -5194,7 +5196,6 @@ export class ConversationModel extends window.Backbone
const newVersion = expireTimerVersion + 1; const newVersion = expireTimerVersion + 1;
this.set('expireTimerVersion', newVersion); this.set('expireTimerVersion', newVersion);
await DataWriter.updateConversation(this.attributes); await DataWriter.updateConversation(this.attributes);
return newVersion;
} }
// Set of items to captureChanges on: // Set of items to captureChanges on: