Add timestamp utilities with helpful names

This commit is contained in:
Fedor Indutny 2021-03-22 14:08:52 -07:00 committed by GitHub
parent 9fa3359477
commit a75402d290
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 17 deletions

View file

@ -17,8 +17,10 @@ import ProvisioningCipher from './ProvisioningCipher';
import WebSocketResource, {
IncomingWebSocketRequest,
} from './WebsocketResources';
import { isMoreRecentThan, isOlderThan } from '../util/timestamp';
const ARCHIVE_AGE = 30 * 24 * 60 * 60 * 1000;
const PREKEY_ROTATION_AGE = 24 * 60 * 60 * 1000;
function getIdentifier(id: string) {
if (!id || !id.length) {
@ -321,10 +323,9 @@ export default class AccountManager extends EventTarget {
existingKeys.sort((a, b) => (b.created_at || 0) - (a.created_at || 0));
const confirmedKeys = existingKeys.filter(key => key.confirmed);
const ONE_DAY_AGO = Date.now() - 24 * 60 * 60 * 1000;
if (
confirmedKeys.length >= 3 &&
confirmedKeys[0].created_at > ONE_DAY_AGO
isMoreRecentThan(confirmedKeys[0].created_at, PREKEY_ROTATION_AGE)
) {
window.log.warn(
'rotateSignedPreKey: 3+ confirmed keys, most recent is less than a day old. Cancelling rotation.'
@ -437,9 +438,8 @@ export default class AccountManager extends EventTarget {
return;
}
const createdAt = key.created_at || 0;
const age = Date.now() - createdAt;
if (age > ARCHIVE_AGE) {
if (isOlderThan(createdAt, ARCHIVE_AGE)) {
window.log.info(
'Removing confirmed signed prekey:',
key.keyId,
@ -463,8 +463,7 @@ export default class AccountManager extends EventTarget {
}
const createdAt = key.created_at || 0;
const age = Date.now() - createdAt;
if (age > ARCHIVE_AGE) {
if (isOlderThan(createdAt, ARCHIVE_AGE)) {
window.log.info(
'Removing unconfirmed signed prekey:',
key.keyId,