2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-03-18 17:09:27 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import * as sinon from 'sinon';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { v4 as generateUuid } from 'uuid';
|
|
|
|
|
2021-03-18 17:09:27 +00:00
|
|
|
import { times } from 'lodash';
|
|
|
|
import { ConversationModel } from '../models/conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationAttributesType } from '../model-types.d';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../types/ServiceId';
|
2023-08-07 23:12:57 +00:00
|
|
|
import { DAY, HOUR, MINUTE, MONTH } from '../util/durations';
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
import { routineProfileRefresh } from '../routineProfileRefresh';
|
2024-10-10 17:57:22 +00:00
|
|
|
import type { getProfile } from '../util/getProfile';
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
describe('routineProfileRefresh', () => {
|
|
|
|
let sinonSandbox: sinon.SinonSandbox;
|
2024-10-10 17:57:22 +00:00
|
|
|
let getProfileFn: sinon.SinonStub<
|
|
|
|
Parameters<typeof getProfile>,
|
|
|
|
ReturnType<typeof getProfile>
|
|
|
|
>;
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sinonSandbox = sinon.createSandbox();
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn = sinon.stub();
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sinonSandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
function makeConversation(
|
|
|
|
overrideAttributes: Partial<ConversationAttributesType> = {}
|
|
|
|
): ConversationModel {
|
|
|
|
const result = new ConversationModel({
|
2023-08-10 16:43:33 +00:00
|
|
|
accessKey: generateUuid(),
|
2021-07-21 20:45:41 +00:00
|
|
|
active_at: Date.now(),
|
2021-03-18 17:09:27 +00:00
|
|
|
draftAttachments: [],
|
|
|
|
draftBodyRanges: [],
|
|
|
|
draftTimestamp: null,
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateUuid(),
|
2021-03-18 17:09:27 +00:00
|
|
|
inbox_position: 0,
|
|
|
|
isPinned: false,
|
|
|
|
lastMessageDeletedForEveryone: false,
|
|
|
|
lastMessageStatus: 'sent',
|
2021-07-21 20:45:41 +00:00
|
|
|
left: false,
|
2021-03-18 17:09:27 +00:00
|
|
|
markedUnread: false,
|
|
|
|
messageCount: 2,
|
|
|
|
messageCountBeforeMessageRequests: 0,
|
|
|
|
messageRequestResponseType: 0,
|
|
|
|
muteExpiresAt: 0,
|
|
|
|
profileAvatar: undefined,
|
2023-08-10 16:43:33 +00:00
|
|
|
profileKeyCredential: generateUuid(),
|
2022-07-08 20:46:25 +00:00
|
|
|
profileKeyCredentialExpiration: Date.now() + 2 * DAY,
|
2021-07-21 20:45:41 +00:00
|
|
|
profileSharing: true,
|
2021-03-18 17:09:27 +00:00
|
|
|
quotedMessageId: null,
|
|
|
|
sealedSender: 1,
|
|
|
|
sentMessageCount: 1,
|
|
|
|
sharedGroupNames: [],
|
|
|
|
timestamp: Date.now(),
|
2021-07-21 20:45:41 +00:00
|
|
|
type: 'private',
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId: generateAci(),
|
2021-03-18 17:09:27 +00:00
|
|
|
version: 2,
|
2024-08-21 16:03:28 +00:00
|
|
|
expireTimerVersion: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
...overrideAttributes,
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeGroup(
|
|
|
|
groupMembers: Array<ConversationModel>
|
|
|
|
): ConversationModel {
|
|
|
|
const result = makeConversation({ type: 'group' });
|
|
|
|
// This is easier than setting up all of the scaffolding for `getMembers`.
|
|
|
|
sinonSandbox.stub(result, 'getMembers').returns(groupMembers);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
function makeStorage(lastAttemptAt?: number) {
|
2021-03-18 17:09:27 +00:00
|
|
|
return {
|
|
|
|
get: sinonSandbox
|
|
|
|
.stub()
|
|
|
|
.withArgs('lastAttemptedToRefreshProfilesAt')
|
|
|
|
.returns(lastAttemptAt),
|
|
|
|
put: sinonSandbox.stub().resolves(undefined),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
it('does nothing when the last refresh time is less one hour', async () => {
|
2021-03-18 17:09:27 +00:00
|
|
|
const conversation1 = makeConversation();
|
|
|
|
const conversation2 = makeConversation();
|
2023-08-07 23:12:57 +00:00
|
|
|
const storage = makeStorage(Date.now() - 47 * MINUTE);
|
2021-03-18 17:09:27 +00:00
|
|
|
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: [conversation1, conversation2],
|
2023-08-10 16:43:33 +00:00
|
|
|
ourConversationId: generateUuid(),
|
2021-03-18 17:09:27 +00:00
|
|
|
storage,
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2022-02-11 21:38:52 +00:00
|
|
|
sinon.assert.notCalled(getProfileFn);
|
2021-03-18 17:09:27 +00:00
|
|
|
sinon.assert.notCalled(storage.put);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('asks conversations to get their profiles', async () => {
|
|
|
|
const conversation1 = makeConversation();
|
|
|
|
const conversation2 = makeConversation();
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: [conversation1, conversation2],
|
2023-08-10 16:43:33 +00:00
|
|
|
ourConversationId: generateUuid(),
|
2021-03-18 17:09:27 +00:00
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: conversation1.getServiceId() ?? null,
|
|
|
|
e164: conversation1.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: conversation2.getServiceId() ?? null,
|
|
|
|
e164: conversation2.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
it('skips unregistered conversations and those fetched in the last three days', async () => {
|
|
|
|
const normal = makeConversation();
|
|
|
|
const recentlyFetched = makeConversation({
|
|
|
|
profileLastFetchedAt: Date.now() - DAY * 2 - HOUR * 3,
|
|
|
|
});
|
|
|
|
const unregisteredAndStale = makeConversation({
|
|
|
|
firstUnregisteredAt: Date.now() - 2 * MONTH,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
2023-08-07 23:12:57 +00:00
|
|
|
allConversations: [normal, recentlyFetched, unregisteredAndStale],
|
2023-08-10 16:43:33 +00:00
|
|
|
ourConversationId: generateUuid(),
|
2021-03-18 17:09:27 +00:00
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2022-02-11 21:38:52 +00:00
|
|
|
sinon.assert.calledOnce(getProfileFn);
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: normal.getServiceId() ?? null,
|
|
|
|
e164: normal.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: recentlyFetched.getServiceId() ?? null,
|
|
|
|
e164: recentlyFetched.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: unregisteredAndStale.getServiceId() ?? null,
|
|
|
|
e164: unregisteredAndStale.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('skips your own conversation', async () => {
|
|
|
|
const notMe = makeConversation();
|
|
|
|
const me = makeConversation();
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: [notMe, me],
|
|
|
|
ourConversationId: me.id,
|
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: notMe.getServiceId() ?? null,
|
|
|
|
e164: notMe.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: me.getServiceId() ?? null,
|
|
|
|
e164: me.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
it('includes your own conversation if profileKeyCredential is expired', async () => {
|
|
|
|
const notMe = makeConversation();
|
|
|
|
const me = makeConversation({
|
|
|
|
profileKey: 'fakeProfileKey',
|
|
|
|
profileKeyCredential: undefined,
|
|
|
|
profileKeyCredentialExpiration: undefined,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
2023-08-07 23:12:57 +00:00
|
|
|
allConversations: [notMe, me],
|
|
|
|
ourConversationId: me.id,
|
2021-03-18 17:09:27 +00:00
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: notMe.getServiceId() ?? null,
|
|
|
|
e164: notMe.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: me.getServiceId() ?? null,
|
|
|
|
e164: me.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
it('skips conversations that were refreshed in last three days', async () => {
|
|
|
|
const neverRefreshed = makeConversation();
|
|
|
|
const refreshedToday = makeConversation({
|
|
|
|
profileLastFetchedAt: Date.now() - HOUR * 5,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
2023-08-07 23:12:57 +00:00
|
|
|
const refreshedYesterday = makeConversation({
|
|
|
|
profileLastFetchedAt: Date.now() - DAY,
|
|
|
|
});
|
|
|
|
const refreshedTwoDaysAgo = makeConversation({
|
|
|
|
profileLastFetchedAt: Date.now() - DAY * 2,
|
|
|
|
});
|
|
|
|
const refreshedThreeDaysAgo = makeConversation({
|
|
|
|
profileLastFetchedAt: Date.now() - DAY * 3 - 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: [
|
2023-08-07 23:12:57 +00:00
|
|
|
neverRefreshed,
|
|
|
|
refreshedToday,
|
|
|
|
refreshedYesterday,
|
|
|
|
refreshedTwoDaysAgo,
|
|
|
|
refreshedThreeDaysAgo,
|
2021-03-18 17:09:27 +00:00
|
|
|
],
|
2023-08-10 16:43:33 +00:00
|
|
|
ourConversationId: generateUuid(),
|
2021-03-18 17:09:27 +00:00
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
sinon.assert.calledTwice(getProfileFn);
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: neverRefreshed.getServiceId() ?? null,
|
|
|
|
e164: neverRefreshed.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: refreshedToday.getServiceId() ?? null,
|
|
|
|
e164: refreshedToday.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: refreshedYesterday.getServiceId() ?? null,
|
|
|
|
e164: refreshedYesterday.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: refreshedTwoDaysAgo.getServiceId() ?? null,
|
|
|
|
e164: refreshedTwoDaysAgo.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: refreshedThreeDaysAgo.getServiceId() ?? null,
|
|
|
|
e164: refreshedThreeDaysAgo.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
it('only refreshes profiles for the 50 conversations with the oldest profileLastFetchedAt', async () => {
|
2021-03-18 17:09:27 +00:00
|
|
|
const me = makeConversation();
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
const normalConversations = times(25, () => makeConversation());
|
|
|
|
const neverFetched = times(10, () =>
|
2021-03-18 17:09:27 +00:00
|
|
|
makeConversation({
|
2023-08-07 23:12:57 +00:00
|
|
|
profileLastFetchedAt: undefined,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const unregisteredUsers = times(10, () =>
|
|
|
|
makeConversation({
|
|
|
|
firstUnregisteredAt: Date.now() - MONTH * 2,
|
2021-03-18 17:09:27 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const shouldNotBeIncluded = [
|
|
|
|
// Recently-active groups with no other members
|
|
|
|
makeGroup([]),
|
|
|
|
makeGroup([me]),
|
2023-08-07 23:12:57 +00:00
|
|
|
...unregisteredUsers,
|
2021-03-18 17:09:27 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
await routineProfileRefresh({
|
|
|
|
allConversations: [
|
|
|
|
me,
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
...unregisteredUsers,
|
|
|
|
...normalConversations,
|
|
|
|
...neverFetched,
|
2021-03-18 17:09:27 +00:00
|
|
|
],
|
|
|
|
ourConversationId: me.id,
|
|
|
|
storage: makeStorage(),
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2022-08-04 21:43:47 +00:00
|
|
|
id: 1,
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
[...normalConversations, ...neverFetched].forEach(conversation => {
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, {
|
|
|
|
serviceId: conversation.getServiceId() ?? null,
|
|
|
|
e164: conversation.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
[me, ...shouldNotBeIncluded].forEach(conversation => {
|
2024-10-10 17:57:22 +00:00
|
|
|
sinon.assert.neverCalledWith(getProfileFn, {
|
|
|
|
serviceId: conversation.getServiceId() ?? null,
|
|
|
|
e164: conversation.get('e164') ?? null,
|
|
|
|
groupId: null,
|
|
|
|
});
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|