Truncate lastHeartbeat to day millis

This commit is contained in:
Fedor Indutny 2021-09-24 10:01:46 -07:00 committed by GitHub
parent 7adfd1a4e7
commit af66a5b265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View file

@ -28,7 +28,7 @@ import { filter } from './util/iterables';
import { isNotNil } from './util/isNotNil';
import { senderCertificateService } from './services/senderCertificate';
import { routineProfileRefresh } from './routineProfileRefresh';
import { isMoreRecentThan, isOlderThan } from './util/timestamp';
import { isMoreRecentThan, isOlderThan, toDayMillis } from './util/timestamp';
import { isValidReactionEmoji } from './reactions/isValidReactionEmoji';
import { ConversationModel } from './models/conversations';
import { getMessageById } from './messages/getMessageById';
@ -672,7 +672,7 @@ export async function startApp(): Promise<void> {
webFrame.setZoomFactor(window.Events.getZoomFactor());
// How long since we were last running?
const lastHeartbeat = window.storage.get('lastHeartbeat', 0);
const lastHeartbeat = toDayMillis(window.storage.get('lastHeartbeat', 0));
const previousLastStartup = window.storage.get('lastStartup');
await window.storage.put('lastStartup', Date.now());
@ -685,10 +685,10 @@ export async function startApp(): Promise<void> {
}
// Start heartbeat timer
window.storage.put('lastHeartbeat', Date.now());
window.storage.put('lastHeartbeat', toDayMillis(Date.now()));
const TWELVE_HOURS = 12 * 60 * 60 * 1000;
setInterval(
() => window.storage.put('lastHeartbeat', Date.now()),
() => window.storage.put('lastHeartbeat', toDayMillis(Date.now())),
TWELVE_HOURS
);

View file

@ -3,7 +3,11 @@
import { assert } from 'chai';
import { isOlderThan, isMoreRecentThan } from '../../util/timestamp';
import {
isOlderThan,
isMoreRecentThan,
toDayMillis,
} from '../../util/timestamp';
const ONE_HOUR = 3600 * 1000;
const ONE_DAY = 24 * ONE_HOUR;
@ -34,4 +38,17 @@ describe('timestamp', () => {
);
});
});
describe('toDayMillis', () => {
const now = new Date();
const today = new Date(toDayMillis(now.valueOf()));
assert.strictEqual(today.getUTCMilliseconds(), 0);
assert.strictEqual(today.getUTCHours(), 0);
assert.strictEqual(today.getUTCMinutes(), 0);
assert.strictEqual(today.getUTCSeconds(), 0);
assert.strictEqual(today.getUTCDate(), now.getUTCDate());
assert.strictEqual(today.getUTCMonth(), now.getUTCMonth());
assert.strictEqual(today.getUTCFullYear(), now.getUTCFullYear());
});
});

View file

@ -1,6 +1,8 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const ONE_DAY = 24 * 3600 * 1000;
export function isMoreRecentThan(timestamp: number, delta: number): boolean {
return timestamp > Date.now() - delta;
}
@ -16,3 +18,7 @@ export function isInPast(timestamp: number): boolean {
export function isInFuture(timestamp: number): boolean {
return isMoreRecentThan(timestamp, 0);
}
export function toDayMillis(timestamp: number): number {
return timestamp - (timestamp % ONE_DAY);
}