Sync my stories with primary device
This commit is contained in:
parent
7554d8326a
commit
9155784d56
67 changed files with 2954 additions and 1238 deletions
|
@ -293,6 +293,7 @@ const dataInterface: ClientInterface = {
|
|||
getStoryDistributionWithMembers,
|
||||
modifyStoryDistribution,
|
||||
modifyStoryDistributionMembers,
|
||||
modifyStoryDistributionWithMembers,
|
||||
deleteStoryDistribution,
|
||||
|
||||
_getAllStoryReads,
|
||||
|
@ -1634,6 +1635,15 @@ async function modifyStoryDistributionMembers(
|
|||
): Promise<void> {
|
||||
await channels.modifyStoryDistributionMembers(id, options);
|
||||
}
|
||||
async function modifyStoryDistributionWithMembers(
|
||||
distribution: StoryDistributionType,
|
||||
options: {
|
||||
toAdd: Array<UUIDStringType>;
|
||||
toRemove: Array<UUIDStringType>;
|
||||
}
|
||||
): Promise<void> {
|
||||
await channels.modifyStoryDistributionWithMembers(distribution, options);
|
||||
}
|
||||
async function deleteStoryDistribution(id: UUIDStringType): Promise<void> {
|
||||
await channels.deleteStoryDistribution(id);
|
||||
}
|
||||
|
|
|
@ -233,10 +233,15 @@ export type DeleteSentProtoRecipientOptionsType = Readonly<{
|
|||
export type StoryDistributionType = Readonly<{
|
||||
id: UUIDStringType;
|
||||
name: string;
|
||||
|
||||
avatarUrlPath: string;
|
||||
avatarKey: Uint8Array;
|
||||
deletedAtTimestamp?: number;
|
||||
allowsReplies: boolean;
|
||||
isBlockList: boolean;
|
||||
senderKeyInfo: SenderKeyInfoType | undefined;
|
||||
|
||||
storageID: string;
|
||||
storageVersion: number;
|
||||
storageUnknownFields?: Uint8Array | null;
|
||||
storageNeedsSync: boolean;
|
||||
}>;
|
||||
export type StoryDistributionMemberType = Readonly<{
|
||||
listId: UUIDStringType;
|
||||
|
@ -559,7 +564,14 @@ export type DataInterface = {
|
|||
): Promise<StoryDistributionWithMembersType | undefined>;
|
||||
modifyStoryDistribution(distribution: StoryDistributionType): Promise<void>;
|
||||
modifyStoryDistributionMembers(
|
||||
id: string,
|
||||
listId: string,
|
||||
options: {
|
||||
toAdd: Array<UUIDStringType>;
|
||||
toRemove: Array<UUIDStringType>;
|
||||
}
|
||||
): Promise<void>;
|
||||
modifyStoryDistributionWithMembers(
|
||||
distribution: StoryDistributionType,
|
||||
options: {
|
||||
toAdd: Array<UUIDStringType>;
|
||||
toRemove: Array<UUIDStringType>;
|
||||
|
|
158
ts/sql/Server.ts
158
ts/sql/Server.ts
|
@ -287,6 +287,7 @@ const dataInterface: ServerInterface = {
|
|||
getStoryDistributionWithMembers,
|
||||
modifyStoryDistribution,
|
||||
modifyStoryDistributionMembers,
|
||||
modifyStoryDistributionWithMembers,
|
||||
deleteStoryDistribution,
|
||||
|
||||
_getAllStoryReads,
|
||||
|
@ -4026,8 +4027,19 @@ async function getAllBadgeImageFileLocalPaths(): Promise<Set<string>> {
|
|||
|
||||
type StoryDistributionForDatabase = Readonly<
|
||||
{
|
||||
allowsReplies: 0 | 1;
|
||||
deletedAtTimestamp: number | null;
|
||||
isBlockList: 0 | 1;
|
||||
senderKeyInfoJson: string | null;
|
||||
} & Omit<StoryDistributionType, 'senderKeyInfo'>
|
||||
storageNeedsSync: 0 | 1;
|
||||
} & Omit<
|
||||
StoryDistributionType,
|
||||
| 'allowsReplies'
|
||||
| 'deletedAtTimestamp'
|
||||
| 'isBlockList'
|
||||
| 'senderKeyInfo'
|
||||
| 'storageNeedsSync'
|
||||
>
|
||||
>;
|
||||
|
||||
function hydrateStoryDistribution(
|
||||
|
@ -4035,9 +4047,14 @@ function hydrateStoryDistribution(
|
|||
): StoryDistributionType {
|
||||
return {
|
||||
...omit(fromDatabase, 'senderKeyInfoJson'),
|
||||
allowsReplies: Boolean(fromDatabase.allowsReplies),
|
||||
deletedAtTimestamp: fromDatabase.deletedAtTimestamp || undefined,
|
||||
isBlockList: Boolean(fromDatabase.isBlockList),
|
||||
senderKeyInfo: fromDatabase.senderKeyInfoJson
|
||||
? JSON.parse(fromDatabase.senderKeyInfoJson)
|
||||
: undefined,
|
||||
storageNeedsSync: Boolean(fromDatabase.storageNeedsSync),
|
||||
storageUnknownFields: fromDatabase.storageUnknownFields || undefined,
|
||||
};
|
||||
}
|
||||
function freezeStoryDistribution(
|
||||
|
@ -4045,9 +4062,14 @@ function freezeStoryDistribution(
|
|||
): StoryDistributionForDatabase {
|
||||
return {
|
||||
...omit(story, 'senderKeyInfo'),
|
||||
allowsReplies: story.allowsReplies ? 1 : 0,
|
||||
deletedAtTimestamp: story.deletedAtTimestamp || null,
|
||||
isBlockList: story.isBlockList ? 1 : 0,
|
||||
senderKeyInfoJson: story.senderKeyInfo
|
||||
? JSON.stringify(story.senderKeyInfo)
|
||||
: null,
|
||||
storageNeedsSync: story.storageNeedsSync ? 1 : 0,
|
||||
storageUnknownFields: story.storageUnknownFields || null,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -4087,15 +4109,25 @@ async function createNewStoryDistribution(
|
|||
INSERT INTO storyDistributions(
|
||||
id,
|
||||
name,
|
||||
avatarUrlPath,
|
||||
avatarKey,
|
||||
senderKeyInfoJson
|
||||
deletedAtTimestamp,
|
||||
allowsReplies,
|
||||
isBlockList,
|
||||
senderKeyInfoJson,
|
||||
storageID,
|
||||
storageVersion,
|
||||
storageUnknownFields,
|
||||
storageNeedsSync
|
||||
) VALUES (
|
||||
$id,
|
||||
$name,
|
||||
$avatarUrlPath,
|
||||
$avatarKey,
|
||||
$senderKeyInfoJson
|
||||
$deletedAtTimestamp,
|
||||
$allowsReplies,
|
||||
$isBlockList,
|
||||
$senderKeyInfoJson,
|
||||
$storageID,
|
||||
$storageVersion,
|
||||
$storageUnknownFields,
|
||||
$storageNeedsSync
|
||||
);
|
||||
`
|
||||
).run(payload);
|
||||
|
@ -4163,24 +4195,91 @@ async function getStoryDistributionWithMembers(
|
|||
members: members.map(({ uuid }) => uuid),
|
||||
};
|
||||
}
|
||||
async function modifyStoryDistribution(
|
||||
distribution: StoryDistributionType
|
||||
): Promise<void> {
|
||||
const payload = freezeStoryDistribution(distribution);
|
||||
const db = getInstance();
|
||||
function modifyStoryDistributionSync(
|
||||
db: Database,
|
||||
payload: StoryDistributionForDatabase
|
||||
): void {
|
||||
prepare(
|
||||
db,
|
||||
`
|
||||
UPDATE storyDistributions
|
||||
SET
|
||||
name = $name,
|
||||
avatarUrlPath = $avatarUrlPath,
|
||||
avatarKey = $avatarKey,
|
||||
senderKeyInfoJson = $senderKeyInfoJson
|
||||
deletedAtTimestamp = $deletedAtTimestamp,
|
||||
allowsReplies = $allowsReplies,
|
||||
isBlockList = $isBlockList,
|
||||
senderKeyInfoJson = $senderKeyInfoJson,
|
||||
storageID = $storageID,
|
||||
storageVersion = $storageVersion,
|
||||
storageUnknownFields = $storageUnknownFields,
|
||||
storageNeedsSync = $storageNeedsSync
|
||||
WHERE id = $id
|
||||
`
|
||||
).run(payload);
|
||||
}
|
||||
function modifyStoryDistributionMembersSync(
|
||||
db: Database,
|
||||
listId: string,
|
||||
{
|
||||
toAdd,
|
||||
toRemove,
|
||||
}: { toAdd: Array<UUIDStringType>; toRemove: Array<UUIDStringType> }
|
||||
) {
|
||||
const memberInsertStatement = prepare(
|
||||
db,
|
||||
`
|
||||
INSERT OR REPLACE INTO storyDistributionMembers (
|
||||
listId,
|
||||
uuid
|
||||
) VALUES (
|
||||
$listId,
|
||||
$uuid
|
||||
);
|
||||
`
|
||||
);
|
||||
|
||||
for (const uuid of toAdd) {
|
||||
memberInsertStatement.run({
|
||||
listId,
|
||||
uuid,
|
||||
});
|
||||
}
|
||||
|
||||
batchMultiVarQuery(db, toRemove, (uuids: Array<UUIDStringType>) => {
|
||||
db.prepare<ArrayQuery>(
|
||||
`
|
||||
DELETE FROM storyDistributionMembers
|
||||
WHERE listId = ? AND uuid IN ( ${uuids.map(() => '?').join(', ')} );
|
||||
`
|
||||
).run([listId, ...uuids]);
|
||||
});
|
||||
}
|
||||
async function modifyStoryDistributionWithMembers(
|
||||
distribution: StoryDistributionType,
|
||||
{
|
||||
toAdd,
|
||||
toRemove,
|
||||
}: { toAdd: Array<UUIDStringType>; toRemove: Array<UUIDStringType> }
|
||||
): Promise<void> {
|
||||
const payload = freezeStoryDistribution(distribution);
|
||||
const db = getInstance();
|
||||
|
||||
if (toAdd.length || toRemove.length) {
|
||||
db.transaction(() => {
|
||||
modifyStoryDistributionSync(db, payload);
|
||||
modifyStoryDistributionMembersSync(db, payload.id, { toAdd, toRemove });
|
||||
})();
|
||||
} else {
|
||||
modifyStoryDistributionSync(db, payload);
|
||||
}
|
||||
}
|
||||
async function modifyStoryDistribution(
|
||||
distribution: StoryDistributionType
|
||||
): Promise<void> {
|
||||
const payload = freezeStoryDistribution(distribution);
|
||||
const db = getInstance();
|
||||
modifyStoryDistributionSync(db, payload);
|
||||
}
|
||||
async function modifyStoryDistributionMembers(
|
||||
listId: string,
|
||||
{
|
||||
|
@ -4191,34 +4290,7 @@ async function modifyStoryDistributionMembers(
|
|||
const db = getInstance();
|
||||
|
||||
db.transaction(() => {
|
||||
const memberInsertStatement = prepare(
|
||||
db,
|
||||
`
|
||||
INSERT OR REPLACE INTO storyDistributionMembers (
|
||||
listId,
|
||||
uuid
|
||||
) VALUES (
|
||||
$listId,
|
||||
$uuid
|
||||
);
|
||||
`
|
||||
);
|
||||
|
||||
for (const uuid of toAdd) {
|
||||
memberInsertStatement.run({
|
||||
listId,
|
||||
uuid,
|
||||
});
|
||||
}
|
||||
|
||||
batchMultiVarQuery(db, toRemove, (uuids: Array<UUIDStringType>) => {
|
||||
db.prepare<ArrayQuery>(
|
||||
`
|
||||
DELETE FROM storyDistributionMembers
|
||||
WHERE listId = ? AND uuid IN ( ${uuids.map(() => '?').join(', ')} );
|
||||
`
|
||||
).run([listId, ...uuids]);
|
||||
});
|
||||
modifyStoryDistributionMembersSync(db, listId, { toAdd, toRemove });
|
||||
})();
|
||||
}
|
||||
async function deleteStoryDistribution(id: UUIDStringType): Promise<void> {
|
||||
|
|
44
ts/sql/migrations/61-distribution-list-storage.ts
Normal file
44
ts/sql/migrations/61-distribution-list-storage.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2022 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { Database } from 'better-sqlite3';
|
||||
|
||||
import type { LoggerType } from '../../types/Logging';
|
||||
|
||||
export default function updateToSchemaVersion61(
|
||||
currentVersion: number,
|
||||
db: Database,
|
||||
logger: LoggerType
|
||||
): void {
|
||||
if (currentVersion >= 61) {
|
||||
return;
|
||||
}
|
||||
|
||||
db.transaction(() => {
|
||||
db.exec(
|
||||
`
|
||||
ALTER TABLE storyDistributions DROP COLUMN avatarKey;
|
||||
ALTER TABLE storyDistributions DROP COLUMN avatarUrlPath;
|
||||
|
||||
ALTER TABLE storyDistributions ADD COLUMN deletedAtTimestamp INTEGER;
|
||||
ALTER TABLE storyDistributions ADD COLUMN allowsReplies INTEGER;
|
||||
ALTER TABLE storyDistributions ADD COLUMN isBlockList INTEGER;
|
||||
|
||||
ALTER TABLE storyDistributions ADD COLUMN storageID STRING;
|
||||
ALTER TABLE storyDistributions ADD COLUMN storageVersion INTEGER;
|
||||
ALTER TABLE storyDistributions ADD COLUMN storageUnknownFields BLOB;
|
||||
ALTER TABLE storyDistributions ADD COLUMN storageNeedsSync INTEGER;
|
||||
|
||||
ALTER TABLE messages ADD COLUMN storyDistributionListId STRING;
|
||||
|
||||
CREATE INDEX messages_by_distribution_list
|
||||
ON messages(storyDistributionListId, received_at)
|
||||
WHERE storyDistributionListId IS NOT NULL;
|
||||
`
|
||||
);
|
||||
|
||||
db.pragma('user_version = 61');
|
||||
})();
|
||||
|
||||
logger.info('updateToSchemaVersion61: success!');
|
||||
}
|
|
@ -36,6 +36,7 @@ import updateToSchemaVersion57 from './57-rm-message-history-unsynced';
|
|||
import updateToSchemaVersion58 from './58-update-unread';
|
||||
import updateToSchemaVersion59 from './59-unprocessed-received-at-counter-index';
|
||||
import updateToSchemaVersion60 from './60-update-expiring-index';
|
||||
import updateToSchemaVersion61 from './61-distribution-list-storage';
|
||||
|
||||
function updateToSchemaVersion1(
|
||||
currentVersion: number,
|
||||
|
@ -1935,6 +1936,7 @@ export const SCHEMA_VERSIONS = [
|
|||
updateToSchemaVersion58,
|
||||
updateToSchemaVersion59,
|
||||
updateToSchemaVersion60,
|
||||
updateToSchemaVersion61,
|
||||
];
|
||||
|
||||
export function updateSchema(db: Database, logger: LoggerType): void {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue