2022-02-11 22:32:51 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import createDebug from 'debug';
|
2023-11-02 19:42:31 +00:00
|
|
|
import type {
|
|
|
|
Group,
|
|
|
|
PrimaryDevice,
|
|
|
|
Server,
|
|
|
|
StorageStateRecord,
|
|
|
|
} from '@signalapp/mock-server';
|
2022-02-11 22:32:51 +00:00
|
|
|
import { StorageState, Proto } from '@signalapp/mock-server';
|
2023-11-02 19:42:31 +00:00
|
|
|
import path from 'path';
|
|
|
|
import fs from 'fs/promises';
|
|
|
|
import { range } from 'lodash';
|
2022-02-11 22:32:51 +00:00
|
|
|
import { App } from '../playwright';
|
|
|
|
import { Bootstrap } from '../bootstrap';
|
2022-04-13 00:50:17 +00:00
|
|
|
import type { BootstrapOptions } from '../bootstrap';
|
2022-11-10 15:51:39 +00:00
|
|
|
import { MY_STORY_ID } from '../../types/Stories';
|
2022-07-21 00:07:09 +00:00
|
|
|
import { uuidToBytes } from '../../util/uuidToBytes';
|
2023-11-02 19:42:31 +00:00
|
|
|
import { artAddStickersRoute } from '../../util/signalRoutes';
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-04-20 19:35:53 +00:00
|
|
|
export const debug = createDebug('mock:test:storage');
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
export { App, Bootstrap };
|
|
|
|
|
|
|
|
const GROUP_SIZE = 8;
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
const IdentifierType = Proto.ManifestRecord.Identifier.Type;
|
|
|
|
|
2022-02-11 22:32:51 +00:00
|
|
|
export type InitStorageResultType = Readonly<{
|
|
|
|
bootstrap: Bootstrap;
|
|
|
|
app: App;
|
|
|
|
group: Group;
|
2022-04-13 00:50:17 +00:00
|
|
|
members: ReadonlyArray<PrimaryDevice>;
|
2022-02-11 22:32:51 +00:00
|
|
|
}>;
|
|
|
|
|
|
|
|
//
|
|
|
|
// This function creates an initial storage service state that includes:
|
|
|
|
//
|
|
|
|
// - All contacts from contact sync (first contact pinned)
|
|
|
|
// - A pinned group with GROUP_SIZE members (from the contacts)
|
|
|
|
// - Account with e164 and profileKey
|
|
|
|
//
|
|
|
|
// In addition to above, this function will queue one incoming message in the
|
|
|
|
// group, and one for the first contact (so that both will appear in the left
|
|
|
|
// pane).
|
2022-04-13 00:50:17 +00:00
|
|
|
export async function initStorage(
|
|
|
|
options?: BootstrapOptions
|
|
|
|
): Promise<InitStorageResultType> {
|
2022-02-11 22:32:51 +00:00
|
|
|
// Creates primary device, contacts
|
2022-04-13 00:50:17 +00:00
|
|
|
const bootstrap = new Bootstrap(options);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
|
|
|
await bootstrap.init();
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
try {
|
|
|
|
// Populate storage service
|
|
|
|
const { contacts, phone } = bootstrap;
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const [firstContact] = contacts;
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const members = [...contacts].slice(0, GROUP_SIZE);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const group = await phone.createGroup({
|
|
|
|
title: 'Mock Group',
|
|
|
|
members: [phone, ...members],
|
|
|
|
});
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
let state = StorageState.getEmpty();
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
state = state.updateAccount({
|
|
|
|
profileKey: phone.profileKey.serialize(),
|
|
|
|
e164: phone.device.number,
|
2022-08-03 17:10:49 +00:00
|
|
|
givenName: phone.profileName,
|
2022-07-08 20:46:25 +00:00
|
|
|
});
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
state = state
|
|
|
|
.addGroup(group, {
|
|
|
|
whitelisted: true,
|
|
|
|
})
|
|
|
|
.pinGroup(group);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
for (const contact of contacts) {
|
|
|
|
state = state.addContact(contact, {
|
|
|
|
identityState: Proto.ContactRecord.IdentityState.VERIFIED,
|
|
|
|
whitelisted: true,
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
identityKey: contact.publicKey.serialize(),
|
|
|
|
profileKey: contact.profileKey.serialize(),
|
2022-08-03 17:10:49 +00:00
|
|
|
givenName: contact.profileName,
|
2022-07-08 20:46:25 +00:00
|
|
|
});
|
|
|
|
}
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
state = state.pin(firstContact);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
state = state.addRecord({
|
|
|
|
type: IdentifierType.STORY_DISTRIBUTION_LIST,
|
|
|
|
record: {
|
|
|
|
storyDistributionList: {
|
|
|
|
allowsReplies: true,
|
2022-11-10 15:51:39 +00:00
|
|
|
identifier: uuidToBytes(MY_STORY_ID),
|
2022-07-21 00:07:09 +00:00
|
|
|
isBlockList: true,
|
2022-11-10 15:51:39 +00:00
|
|
|
name: MY_STORY_ID,
|
2023-08-16 20:54:39 +00:00
|
|
|
recipientServiceIds: [],
|
2022-07-21 00:07:09 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
await phone.setStorageState(state);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
// Link new device
|
|
|
|
const app = await bootstrap.link();
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const { desktop } = bootstrap;
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
// Send a message to the group and the first contact
|
|
|
|
const contactSend = contacts[0].sendText(desktop, 'hello from contact', {
|
|
|
|
timestamp: bootstrap.getTimestamp(),
|
|
|
|
sealed: true,
|
|
|
|
});
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
const groupSend = members[0].sendText(desktop, 'hello in group', {
|
|
|
|
timestamp: bootstrap.getTimestamp(),
|
|
|
|
sealed: true,
|
|
|
|
group,
|
|
|
|
});
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
await Promise.all([contactSend, groupSend]);
|
2022-02-11 22:32:51 +00:00
|
|
|
|
2022-07-08 20:46:25 +00:00
|
|
|
return { bootstrap, app, group, members };
|
|
|
|
} catch (error) {
|
|
|
|
await bootstrap.saveLogs();
|
|
|
|
throw error;
|
|
|
|
}
|
2022-02-11 22:32:51 +00:00
|
|
|
}
|
2023-11-02 19:42:31 +00:00
|
|
|
|
|
|
|
export const FIXTURES = path.join(__dirname, '..', '..', '..', 'fixtures');
|
|
|
|
|
|
|
|
export const EMPTY = new Uint8Array(0);
|
|
|
|
|
|
|
|
export type StickerPackType = Readonly<{
|
|
|
|
id: Buffer;
|
|
|
|
key: Buffer;
|
|
|
|
stickerCount: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export const STICKER_PACKS: ReadonlyArray<StickerPackType> = [
|
|
|
|
{
|
|
|
|
id: Buffer.from('c40ed069cdc2b91eccfccf25e6bcddfc', 'hex'),
|
|
|
|
key: Buffer.from(
|
|
|
|
'cefadd6e81c128680aead1711eb5c92c10f63bdfbc78528a4519ba682de396e4',
|
|
|
|
'hex'
|
|
|
|
),
|
|
|
|
stickerCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: Buffer.from('ae8fedafda4768fd3384d4b3b9db963d', 'hex'),
|
|
|
|
key: Buffer.from(
|
|
|
|
'53f4aa8b95e1c2e75afab2328fe67eb6d7affbcd4f50cd4da89dfc325dbc73ca',
|
|
|
|
'hex'
|
|
|
|
),
|
|
|
|
stickerCount: 1,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
export function getStickerPackLink(pack: StickerPackType): string {
|
|
|
|
return artAddStickersRoute
|
|
|
|
.toWebUrl({
|
|
|
|
packId: pack.id.toString('hex'),
|
|
|
|
packKey: pack.key.toString('hex'),
|
|
|
|
})
|
|
|
|
.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getStickerPackRecordPredicate(
|
|
|
|
pack: StickerPackType
|
|
|
|
): (record: StorageStateRecord) => boolean {
|
|
|
|
return ({ type, record }: StorageStateRecord): boolean => {
|
|
|
|
if (type !== IdentifierType.STICKER_PACK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return pack.id.equals(record.stickerPack?.packId ?? EMPTY);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function storeStickerPacks(
|
|
|
|
server: Server,
|
|
|
|
stickerPacks: ReadonlyArray<StickerPackType>
|
|
|
|
): Promise<void> {
|
|
|
|
await Promise.all(
|
|
|
|
stickerPacks.map(async ({ id, stickerCount }) => {
|
|
|
|
const hexId = id.toString('hex');
|
|
|
|
|
|
|
|
await server.storeStickerPack({
|
|
|
|
id,
|
|
|
|
manifest: await fs.readFile(
|
|
|
|
path.join(FIXTURES, `stickerpack-${hexId}.bin`)
|
|
|
|
),
|
|
|
|
stickers: await Promise.all(
|
|
|
|
range(0, stickerCount).map(async index =>
|
|
|
|
fs.readFile(
|
|
|
|
path.join(FIXTURES, `stickerpack-${hexId}-${index}.bin`)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|