2021-03-18 17:09:27 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-05-21 19:53:05 +00:00
|
|
|
// We use `for ... of` to deal with iterables in several places in this file.
|
|
|
|
/* eslint-disable no-restricted-syntax */
|
|
|
|
|
2021-03-18 17:09:27 +00:00
|
|
|
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';
|
|
|
|
import { assert } from './util/assert';
|
|
|
|
import { missingCaseError } from './util/missingCaseError';
|
|
|
|
import { isNormalNumber } from './util/isNormalNumber';
|
2021-05-21 19:53:05 +00:00
|
|
|
import { take } from './util/iterables';
|
2021-03-22 21:08:52 +00:00
|
|
|
import { isOlderThan } from './util/timestamp';
|
2021-03-18 17:09:27 +00:00
|
|
|
import { ConversationModel } from './models/conversations';
|
2021-06-15 00:09:37 +00:00
|
|
|
import { StorageInterface } from './types/Storage.d';
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
const STORAGE_KEY = 'lastAttemptedToRefreshProfilesAt';
|
|
|
|
const MAX_AGE_TO_BE_CONSIDERED_ACTIVE = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
const MAX_AGE_TO_BE_CONSIDERED_RECENTLY_REFRESHED = 1 * 24 * 60 * 60 * 1000;
|
|
|
|
const MAX_CONVERSATIONS_TO_REFRESH = 50;
|
2021-03-22 21:08:52 +00:00
|
|
|
const MIN_ELAPSED_DURATION_TO_REFRESH_AGAIN = 12 * 3600 * 1000;
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
export async function routineProfileRefresh({
|
|
|
|
allConversations,
|
|
|
|
ourConversationId,
|
|
|
|
storage,
|
|
|
|
}: {
|
|
|
|
allConversations: Array<ConversationModel>;
|
|
|
|
ourConversationId: string;
|
2021-06-15 00:09:37 +00:00
|
|
|
storage: Pick<StorageInterface, 'get' | 'put'>;
|
2021-03-18 17:09:27 +00:00
|
|
|
}): Promise<void> {
|
|
|
|
log.info('routineProfileRefresh: starting');
|
|
|
|
|
|
|
|
if (!hasEnoughTimeElapsedSinceLastRefresh(storage)) {
|
|
|
|
log.info('routineProfileRefresh: too soon to refresh. Doing nothing');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info('routineProfileRefresh: updating last refresh time');
|
|
|
|
await storage.put(STORAGE_KEY, Date.now());
|
|
|
|
|
|
|
|
const conversationsToRefresh = getConversationsToRefresh(
|
|
|
|
allConversations,
|
|
|
|
ourConversationId
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info('routineProfileRefresh: starting to refresh conversations');
|
|
|
|
|
|
|
|
let totalCount = 0;
|
|
|
|
let successCount = 0;
|
2021-05-21 19:53:05 +00:00
|
|
|
|
|
|
|
async function refreshConversation(
|
|
|
|
conversation: ConversationModel
|
|
|
|
): Promise<void> {
|
|
|
|
window.log.info(
|
|
|
|
`routineProfileRefresh: refreshing profile for ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
|
|
|
|
totalCount += 1;
|
|
|
|
try {
|
|
|
|
await conversation.getProfile(
|
|
|
|
conversation.get('uuid'),
|
|
|
|
conversation.get('e164')
|
|
|
|
);
|
|
|
|
window.log.info(
|
|
|
|
`routineProfileRefresh: refreshed profile for ${conversation.idForLogging()}`
|
|
|
|
);
|
|
|
|
successCount += 1;
|
|
|
|
} catch (err) {
|
|
|
|
window.log.error(
|
|
|
|
`routineProfileRefresh: refreshed profile for ${conversation.idForLogging()}`,
|
|
|
|
err?.stack || err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshQueue = new PQueue({ concurrency: 5, timeout: 1000 * 60 * 2 });
|
|
|
|
for (const conversation of conversationsToRefresh) {
|
|
|
|
refreshQueue.add(() => refreshConversation(conversation));
|
|
|
|
}
|
|
|
|
await refreshQueue.onIdle();
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
log.info(
|
|
|
|
`routineProfileRefresh: successfully refreshed ${successCount} out of ${totalCount} conversation(s)`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
function hasEnoughTimeElapsedSinceLastRefresh(
|
|
|
|
storage: Pick<StorageInterface, 'get'>
|
|
|
|
): boolean {
|
2021-03-18 17:09:27 +00:00
|
|
|
const storedValue = storage.get(STORAGE_KEY);
|
|
|
|
|
|
|
|
if (isNil(storedValue)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNormalNumber(storedValue)) {
|
2021-03-22 21:08:52 +00:00
|
|
|
return isOlderThan(storedValue, MIN_ELAPSED_DURATION_TO_REFRESH_AGAIN);
|
2021-03-18 17:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(
|
|
|
|
false,
|
|
|
|
`An invalid value was stored in ${STORAGE_KEY}; treating it as nil`
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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':
|
|
|
|
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()
|
|
|
|
);
|
|
|
|
}
|