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

View file

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

View file

@ -7,6 +7,7 @@ import Long from 'long';
import { import {
getSafeLongFromTimestamp, getSafeLongFromTimestamp,
getTimestampFromLong, getTimestampFromLong,
getTimestampOrUndefinedFromLong,
} from '../../util/timestampLongUtils'; } from '../../util/timestampLongUtils';
describe('getSafeLongFromTimestamp', () => { describe('getSafeLongFromTimestamp', () => {
@ -46,3 +47,27 @@ describe('getTimestampFromLong', () => {
assert.equal(getTimestampFromLong(null), 0); 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; return num;
} }
export function getTimestampOrUndefinedFromLong(
value?: Long | null
): number | undefined {
if (!value || value.isZero()) {
return undefined;
}
return getTimestampFromLong(value);
}