2022-02-11 21:38:52 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-03-18 17:09:27 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isNil, sortBy } from 'lodash';
|
2021-05-21 19:53:05 +00:00
|
|
|
import PQueue from 'p-queue';
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
import * as log from './logging/log';
|
2022-09-15 19:17:15 +00:00
|
|
|
import { assertDev } from './util/assert';
|
2022-07-08 20:46:25 +00:00
|
|
|
import { sleep } from './util/sleep';
|
2021-03-18 17:09:27 +00:00
|
|
|
import { missingCaseError } from './util/missingCaseError';
|
|
|
|
import { isNormalNumber } from './util/isNormalNumber';
|
2021-05-21 19:53:05 +00:00
|
|
|
import { take } from './util/iterables';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from './models/conversations';
|
|
|
|
import type { StorageInterface } from './types/Storage.d';
|
2022-07-08 20:46:25 +00:00
|
|
|
import * as Errors from './types/errors';
|
2022-02-11 21:38:52 +00:00
|
|
|
import { getProfile } from './util/getProfile';
|
2022-08-04 21:43:47 +00:00
|
|
|
import { MINUTE, HOUR, DAY, WEEK, MONTH } from './util/durations';
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
const STORAGE_KEY = 'lastAttemptedToRefreshProfilesAt';
|
2022-07-08 20:46:25 +00:00
|
|
|
const MAX_AGE_TO_BE_CONSIDERED_ACTIVE = MONTH;
|
|
|
|
const MAX_AGE_TO_BE_CONSIDERED_RECENTLY_REFRESHED = DAY;
|
2021-03-18 17:09:27 +00:00
|
|
|
const MAX_CONVERSATIONS_TO_REFRESH = 50;
|
2022-07-08 20:46:25 +00:00
|
|
|
const MIN_ELAPSED_DURATION_TO_REFRESH_AGAIN = 12 * HOUR;
|
|
|
|
const MIN_REFRESH_DELAY = MINUTE;
|
|
|
|
|
2022-08-04 21:43:47 +00:00
|
|
|
let idCounter = 1;
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
export class RoutineProfileRefresher {
|
2022-08-04 21:43:47 +00:00
|
|
|
private started = false;
|
|
|
|
private id: number;
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly options: {
|
|
|
|
getAllConversations: () => ReadonlyArray<ConversationModel>;
|
|
|
|
getOurConversationId: () => string | undefined;
|
|
|
|
storage: Pick<StorageInterface, 'get' | 'put'>;
|
|
|
|
}
|
2022-08-04 21:43:47 +00:00
|
|
|
) {
|
|
|
|
// We keep track of how many of these classes we create, because we suspect that
|
|
|
|
// there might be too many...
|
|
|
|
idCounter += 1;
|
|
|
|
this.id = idCounter;
|
|
|
|
log.info(
|
|
|
|
`Creating new RoutineProfileRefresher instance with id ${this.id}`
|
|
|
|
);
|
|
|
|
}
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
public async start(): Promise<void> {
|
2022-08-04 21:43:47 +00:00
|
|
|
const logId = `RoutineProfileRefresher.start/${this.id}`;
|
|
|
|
|
|
|
|
if (this.started) {
|
|
|
|
log.warn(`${logId}: already started!`);
|
|
|
|
return;
|
2022-07-08 20:46:25 +00:00
|
|
|
}
|
2022-08-04 21:43:47 +00:00
|
|
|
this.started = true;
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
const { storage, getAllConversations, getOurConversationId } = this.options;
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
while (true) {
|
|
|
|
const refreshInMs = timeUntilNextRefresh(storage);
|
|
|
|
|
2022-08-04 21:43:47 +00:00
|
|
|
log.info(`${logId}: waiting for ${refreshInMs}ms`);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await sleep(refreshInMs);
|
|
|
|
|
|
|
|
const ourConversationId = getOurConversationId();
|
|
|
|
if (!ourConversationId) {
|
2022-08-04 21:43:47 +00:00
|
|
|
log.warn(`${logId}: missing our conversation id`);
|
2022-07-08 20:46:25 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await sleep(MIN_REFRESH_DELAY);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: getAllConversations(),
|
|
|
|
ourConversationId,
|
|
|
|
storage,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: this.id,
|
2022-07-08 20:46:25 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
2022-08-04 21:43:47 +00:00
|
|
|
log.error(`${logId}: failure`, Errors.toLogFormat(error));
|
|
|
|
} finally {
|
2022-07-08 20:46:25 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
await sleep(MIN_REFRESH_DELAY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
export async function routineProfileRefresh({
|
|
|
|
allConversations,
|
|
|
|
ourConversationId,
|
|
|
|
storage,
|
2022-08-04 21:43:47 +00:00
|
|
|
id,
|
2022-02-11 21:38:52 +00:00
|
|
|
// Only for tests
|
|
|
|
getProfileFn = getProfile,
|
2021-03-18 17:09:27 +00:00
|
|
|
}: {
|
2022-07-08 20:46:25 +00:00
|
|
|
allConversations: ReadonlyArray<ConversationModel>;
|
2021-03-18 17:09:27 +00:00
|
|
|
ourConversationId: string;
|
2021-06-15 00:09:37 +00:00
|
|
|
storage: Pick<StorageInterface, 'get' | 'put'>;
|
2022-08-04 21:43:47 +00:00
|
|
|
id: number;
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn?: typeof getProfile;
|
2021-03-18 17:09:27 +00:00
|
|
|
}): Promise<void> {
|
2022-08-04 21:43:47 +00:00
|
|
|
const logId = `routineProfileRefresh/${id}`;
|
|
|
|
log.info(`${logId}: starting`);
|
2021-03-18 17:09:27 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const refreshInMs = timeUntilNextRefresh(storage);
|
|
|
|
if (refreshInMs > 0) {
|
2022-08-04 21:43:47 +00:00
|
|
|
log.info(`${logId}: too soon to refresh. Doing nothing`);
|
2021-03-18 17:09:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-04 21:43:47 +00:00
|
|
|
log.info(`${logId}: updating last refresh time`);
|
2021-03-18 17:09:27 +00:00
|
|
|
await storage.put(STORAGE_KEY, Date.now());
|
|
|
|
|
|
|
|
const conversationsToRefresh = getConversationsToRefresh(
|
|
|
|
allConversations,
|
|
|
|
ourConversationId
|
|
|
|
);
|
|
|
|
|
2022-08-04 21:43:47 +00:00
|
|
|
log.info(`${logId}: starting to refresh conversations`);
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
let totalCount = 0;
|
|
|
|
let successCount = 0;
|
2021-05-21 19:53:05 +00:00
|
|
|
|
|
|
|
async function refreshConversation(
|
|
|
|
conversation: ConversationModel
|
|
|
|
): Promise<void> {
|
2022-08-04 21:43:47 +00:00
|
|
|
log.info(`${logId}: refreshing profile for ${conversation.idForLogging()}`);
|
2021-05-21 19:53:05 +00:00
|
|
|
|
|
|
|
totalCount += 1;
|
|
|
|
try {
|
2022-02-11 21:38:52 +00:00
|
|
|
await getProfileFn(conversation.get('uuid'), conversation.get('e164'));
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-08-04 21:43:47 +00:00
|
|
|
`${logId}: refreshed profile for ${conversation.idForLogging()}`
|
2021-05-21 19:53:05 +00:00
|
|
|
);
|
|
|
|
successCount += 1;
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-08-04 21:43:47 +00:00
|
|
|
`${logId}: refreshed profile for ${conversation.idForLogging()}`,
|
2021-05-21 19:53:05 +00:00
|
|
|
err?.stack || err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:01:03 +00:00
|
|
|
const refreshQueue = new PQueue({
|
|
|
|
concurrency: 5,
|
2022-06-27 16:46:43 +00:00
|
|
|
timeout: MINUTE * 30,
|
2021-11-23 22:01:03 +00:00
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2021-05-21 19:53:05 +00:00
|
|
|
for (const conversation of conversationsToRefresh) {
|
|
|
|
refreshQueue.add(() => refreshConversation(conversation));
|
|
|
|
}
|
|
|
|
await refreshQueue.onIdle();
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
log.info(
|
2022-08-04 21:43:47 +00:00
|
|
|
`${logId}: successfully refreshed ${successCount} out of ${totalCount} conversation(s)`
|
2021-03-18 17:09:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
function timeUntilNextRefresh(storage: Pick<StorageInterface, 'get'>): number {
|
2021-03-18 17:09:27 +00:00
|
|
|
const storedValue = storage.get(STORAGE_KEY);
|
|
|
|
|
|
|
|
if (isNil(storedValue)) {
|
2022-07-08 20:46:25 +00:00
|
|
|
return 0;
|
2021-03-18 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isNormalNumber(storedValue)) {
|
2022-07-08 20:46:25 +00:00
|
|
|
const planned = storedValue + MIN_ELAPSED_DURATION_TO_REFRESH_AGAIN;
|
|
|
|
const now = Date.now();
|
2022-08-04 21:43:47 +00:00
|
|
|
return Math.min(Math.max(0, planned - now), WEEK);
|
2021-03-18 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 19:17:15 +00:00
|
|
|
assertDev(
|
2021-03-18 17:09:27 +00:00
|
|
|
false,
|
|
|
|
`An invalid value was stored in ${STORAGE_KEY}; treating it as nil`
|
|
|
|
);
|
2022-07-08 20:46:25 +00:00
|
|
|
return 0;
|
2021-03-18 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getConversationsToRefresh(
|
|
|
|
conversations: ReadonlyArray<ConversationModel>,
|
|
|
|
ourConversationId: string
|
|
|
|
): Iterable<ConversationModel> {
|
|
|
|
const filteredConversations = getFilteredConversations(
|
|
|
|
conversations,
|
|
|
|
ourConversationId
|
|
|
|
);
|
|
|
|
return take(filteredConversations, MAX_CONVERSATIONS_TO_REFRESH);
|
|
|
|
}
|
|
|
|
|
|
|
|
function* getFilteredConversations(
|
|
|
|
conversations: ReadonlyArray<ConversationModel>,
|
|
|
|
ourConversationId: string
|
|
|
|
): Iterable<ConversationModel> {
|
|
|
|
const sorted = sortBy(conversations, c => c.get('active_at'));
|
|
|
|
|
|
|
|
const conversationIdsSeen = new Set<string>([ourConversationId]);
|
|
|
|
|
|
|
|
for (const conversation of sorted) {
|
|
|
|
const type = conversation.get('type');
|
|
|
|
switch (type) {
|
|
|
|
case 'private':
|
2022-07-08 20:46:25 +00:00
|
|
|
if (
|
|
|
|
conversation.hasProfileKeyCredentialExpired() &&
|
|
|
|
(conversation.id === ourConversationId ||
|
|
|
|
!conversationIdsSeen.has(conversation.id))
|
|
|
|
) {
|
|
|
|
conversationIdsSeen.add(conversation.id);
|
|
|
|
yield conversation;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:09:27 +00:00
|
|
|
if (
|
|
|
|
!conversationIdsSeen.has(conversation.id) &&
|
|
|
|
isConversationActive(conversation) &&
|
|
|
|
!hasRefreshedProfileRecently(conversation)
|
|
|
|
) {
|
|
|
|
conversationIdsSeen.add(conversation.id);
|
|
|
|
yield conversation;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'group':
|
|
|
|
for (const member of conversation.getMembers()) {
|
|
|
|
if (
|
|
|
|
!conversationIdsSeen.has(member.id) &&
|
|
|
|
!hasRefreshedProfileRecently(member)
|
|
|
|
) {
|
|
|
|
conversationIdsSeen.add(member.id);
|
|
|
|
yield member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw missingCaseError(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isConversationActive(
|
|
|
|
conversation: Readonly<ConversationModel>
|
|
|
|
): boolean {
|
|
|
|
const activeAt = conversation.get('active_at');
|
|
|
|
return (
|
|
|
|
isNormalNumber(activeAt) &&
|
|
|
|
activeAt + MAX_AGE_TO_BE_CONSIDERED_ACTIVE > Date.now()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasRefreshedProfileRecently(
|
|
|
|
conversation: Readonly<ConversationModel>
|
|
|
|
): boolean {
|
|
|
|
const profileLastFetchedAt = conversation.get('profileLastFetchedAt');
|
|
|
|
return (
|
|
|
|
isNormalNumber(profileLastFetchedAt) &&
|
|
|
|
profileLastFetchedAt + MAX_AGE_TO_BE_CONSIDERED_RECENTLY_REFRESHED >
|
|
|
|
Date.now()
|
|
|
|
);
|
|
|
|
}
|