Improve compatibility for max long values in backups

This commit is contained in:
ayumi-signal 2024-12-09 07:30:45 -08:00 committed by GitHub
parent 8b1ceaa1d7
commit 0f66bb13b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 52 additions and 14 deletions

View file

@ -398,7 +398,7 @@ export class BackupExportStream extends Readable {
name,
restrictions: toCallLinkRestrictionsProto(restrictions),
expirationMs: isNumber(expiration)
? Long.fromNumber(expiration)
? getSafeLongFromTimestamp(expiration)
: null,
},
},
@ -848,7 +848,7 @@ export class BackupExportStream extends Readable {
? {
notRegistered: {
unregisteredTimestamp: convo.firstUnregisteredAt
? Long.fromNumber(convo.firstUnregisteredAt)
? getSafeLongFromTimestamp(convo.firstUnregisteredAt)
: null,
},
}
@ -1323,7 +1323,7 @@ export class BackupExportStream extends Readable {
groupCall.state = toGroupCallStateProto(callHistory.status);
groupCall.startedCallTimestamp = Long.fromNumber(callHistory.timestamp);
if (callHistory.endedTimestamp != null) {
groupCall.endedCallTimestamp = Long.fromNumber(
groupCall.endedCallTimestamp = getSafeLongFromTimestamp(
callHistory.endedTimestamp
);
}

View file

@ -55,7 +55,10 @@ import type {
QuotedMessageType,
} from '../../model-types.d';
import { assertDev, strictAssert } from '../../util/assert';
import { getTimestampFromLong } from '../../util/timestampLongUtils';
import {
getTimestampFromLong,
getTimestampOrUndefinedFromLong,
} from '../../util/timestampLongUtils';
import { DurationInSeconds, SECOND } from '../../util/durations';
import { calculateExpirationTimestamp } from '../../util/expirationTimer';
import { dropNull } from '../../util/dropNull';
@ -872,7 +875,9 @@ export class BackupImportStream extends Writable {
}
if (contact.notRegistered) {
const timestamp = contact.notRegistered.unregisteredTimestamp?.toNumber();
const timestamp = getTimestampOrUndefinedFromLong(
contact.notRegistered.unregisteredTimestamp
);
attrs.discoveredUnregisteredAt = timestamp || this.now;
attrs.firstUnregisteredAt = timestamp || undefined;
} else {
@ -1216,10 +1221,9 @@ export class BackupImportStream extends Writable {
? DurationInSeconds.fromMillis(chat.expirationTimerMs.toNumber())
: undefined;
conversation.expireTimerVersion = chat.expireTimerVersion || 1;
conversation.muteExpiresAt =
chat.muteUntilMs && !chat.muteUntilMs.isZero()
? getTimestampFromLong(chat.muteUntilMs)
: undefined;
conversation.muteExpiresAt = getTimestampOrUndefinedFromLong(
chat.muteUntilMs
);
conversation.markedUnread = chat.markedUnread === true;
conversation.dontNotifyForMentionsIfMuted =
chat.dontNotifyForMentionsIfMuted === true;
@ -1298,10 +1302,9 @@ export class BackupImportStream extends Writable {
chatConvo.unreadCount = (chatConvo.unreadCount ?? 0) + 1;
}
const expirationStartTimestamp =
item.expireStartDate && !item.expireStartDate.isZero()
? getTimestampFromLong(item.expireStartDate)
: undefined;
const expirationStartTimestamp = getTimestampOrUndefinedFromLong(
item.expireStartDate
);
const expireTimer =
item.expiresInMs && !item.expiresInMs.isZero()
? DurationInSeconds.fromMillis(item.expiresInMs.toNumber())
@ -2198,7 +2201,7 @@ export class BackupImportStream extends Writable {
peerId: groupId,
direction: isRingerMe ? CallDirection.Outgoing : CallDirection.Incoming,
timestamp: startedCallTimestamp.toNumber(),
endedTimestamp: endedCallTimestamp?.toNumber() || null,
endedTimestamp: getTimestampFromLong(endedCallTimestamp) || null,
};
await this.saveCallHistory(callHistory);

View file

@ -7,6 +7,7 @@ import Long from 'long';
import {
getSafeLongFromTimestamp,
getTimestampFromLong,
getTimestampOrUndefinedFromLong,
} from '../../util/timestampLongUtils';
describe('getSafeLongFromTimestamp', () => {
@ -46,3 +47,27 @@ describe('getTimestampFromLong', () => {
assert.equal(getTimestampFromLong(null), 0);
});
});
describe('getTimestampOrUndefinedFromLong', () => {
it('returns undefined when passed 0 Long', () => {
assert.equal(
getTimestampOrUndefinedFromLong(Long.fromNumber(0)),
undefined
);
});
it('returns Number.MAX_SAFE_INTEGER when passed Long.MAX_VALUE', () => {
assert.equal(
getTimestampOrUndefinedFromLong(Long.MAX_VALUE),
Number.MAX_SAFE_INTEGER
);
});
it('returns a normal number', () => {
assert.equal(getTimestampOrUndefinedFromLong(Long.fromNumber(16)), 16);
});
it('returns undefined for null value', () => {
assert.equal(getTimestampOrUndefinedFromLong(null), undefined);
});
});

View file

@ -24,3 +24,13 @@ export function getTimestampFromLong(value?: Long | null): number {
return num;
}
export function getTimestampOrUndefinedFromLong(
value?: Long | null
): number | undefined {
if (!value || value.isZero()) {
return undefined;
}
return getTimestampFromLong(value);
}