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';
|
|
|
|
import { times } from 'lodash';
|
|
|
|
import { ConversationModel } from '../models/conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationAttributesType } from '../model-types.d';
|
2021-10-26 22:59:08 +00:00
|
|
|
import { UUID } from '../types/UUID';
|
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';
|
|
|
|
|
|
|
|
describe('routineProfileRefresh', () => {
|
|
|
|
let sinonSandbox: sinon.SinonSandbox;
|
2022-02-11 21:38:52 +00:00
|
|
|
let getProfileFn: sinon.SinonStub;
|
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({
|
2021-10-26 22:59:08 +00:00
|
|
|
accessKey: UUID.generate().toString(),
|
2021-07-21 20:45:41 +00:00
|
|
|
active_at: Date.now(),
|
2021-03-18 17:09:27 +00:00
|
|
|
draftAttachments: [],
|
|
|
|
draftBodyRanges: [],
|
|
|
|
draftTimestamp: null,
|
2021-10-26 22:59:08 +00:00
|
|
|
id: UUID.generate().toString(),
|
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,
|
2021-10-26 22:59:08 +00:00
|
|
|
profileKeyCredential: UUID.generate().toString(),
|
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',
|
2021-10-26 22:59:08 +00:00
|
|
|
uuid: UUID.generate().toString(),
|
2021-03-18 17:09:27 +00:00
|
|
|
version: 2,
|
|
|
|
...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],
|
2021-10-26 22:59:08 +00:00
|
|
|
ourConversationId: UUID.generate().toString(),
|
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],
|
2021-10-26 22:59:08 +00:00
|
|
|
ourConversationId: UUID.generate().toString(),
|
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
|
|
|
});
|
|
|
|
|
2021-07-21 20:45:41 +00:00
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2021-07-21 20:45:41 +00:00
|
|
|
conversation1.get('uuid'),
|
|
|
|
conversation1.get('e164')
|
|
|
|
);
|
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2021-07-21 20:45:41 +00:00
|
|
|
conversation2.get('uuid'),
|
|
|
|
conversation2.get('e164')
|
|
|
|
);
|
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],
|
2021-10-26 22:59:08 +00:00
|
|
|
ourConversationId: UUID.generate().toString(),
|
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);
|
2021-07-21 20:45:41 +00:00
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
normal.get('uuid'),
|
|
|
|
normal.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
recentlyFetched.get('uuid'),
|
|
|
|
recentlyFetched.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
unregisteredAndStale.get('uuid'),
|
|
|
|
unregisteredAndStale.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
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
|
|
|
});
|
|
|
|
|
2022-02-11 21:38:52 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, notMe.get('uuid'), notMe.get('e164'));
|
|
|
|
sinon.assert.neverCalledWith(getProfileFn, me.get('uuid'), me.get('e164'));
|
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
|
|
|
});
|
|
|
|
|
2023-08-07 23:12:57 +00:00
|
|
|
sinon.assert.calledWith(getProfileFn, notMe.get('uuid'), notMe.get('e164'));
|
|
|
|
sinon.assert.calledWith(getProfileFn, me.get('uuid'), me.get('e164'));
|
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
|
|
|
],
|
2021-10-26 22:59:08 +00:00
|
|
|
ourConversationId: UUID.generate().toString(),
|
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);
|
2021-07-21 20:45:41 +00:00
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
neverRefreshed.get('uuid'),
|
|
|
|
neverRefreshed.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
2023-08-07 23:12:57 +00:00
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
refreshedToday.get('uuid'),
|
|
|
|
refreshedToday.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
2023-08-07 23:12:57 +00:00
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
refreshedYesterday.get('uuid'),
|
|
|
|
refreshedYesterday.get('e164')
|
2021-07-21 20:45:41 +00:00
|
|
|
);
|
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
refreshedTwoDaysAgo.get('uuid'),
|
|
|
|
refreshedTwoDaysAgo.get('e164')
|
2021-03-18 17:09:27 +00:00
|
|
|
);
|
2023-08-07 23:12:57 +00:00
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2023-08-07 23:12:57 +00:00
|
|
|
refreshedThreeDaysAgo.get('uuid'),
|
|
|
|
refreshedThreeDaysAgo.get('e164')
|
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 => {
|
2021-07-21 20:45:41 +00:00
|
|
|
sinon.assert.calledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2021-07-21 20:45:41 +00:00
|
|
|
conversation.get('uuid'),
|
|
|
|
conversation.get('e164')
|
|
|
|
);
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
[me, ...shouldNotBeIncluded].forEach(conversation => {
|
2021-07-21 20:45:41 +00:00
|
|
|
sinon.assert.neverCalledWith(
|
2022-02-11 21:38:52 +00:00
|
|
|
getProfileFn,
|
2021-07-21 20:45:41 +00:00
|
|
|
conversation.get('uuid'),
|
|
|
|
conversation.get('e164')
|
|
|
|
);
|
2021-03-18 17:09:27 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|