signal-desktop/ts/util/retryPlaceholders.ts

198 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-05-28 19:11:19 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { z } from 'zod';
import { groupBy } from 'lodash';
import * as log from '../logging/log';
2021-05-28 19:11:19 +00:00
const retryItemSchema = z
.object({
conversationId: z.string(),
sentAt: z.number(),
receivedAt: z.number(),
receivedAtCounter: z.number(),
senderUuid: z.string(),
2021-06-08 21:51:58 +00:00
wasOpened: z.boolean().optional(),
2021-05-28 19:11:19 +00:00
})
.passthrough();
export type RetryItemType = z.infer<typeof retryItemSchema>;
const retryItemListSchema = z.array(retryItemSchema);
export type RetryItemListType = z.infer<typeof retryItemListSchema>;
export type ByConversationLookupType = {
[key: string]: Array<RetryItemType>;
};
export type ByMessageLookupType = Map<string, RetryItemType>;
export function getItemId(conversationId: string, sentAt: number): string {
return `${conversationId}--${sentAt}`;
}
const HOUR = 60 * 60 * 1000;
export const STORAGE_KEY = 'retryPlaceholders';
2021-06-08 21:51:58 +00:00
export function getDeltaIntoPast(delta?: number): number {
return Date.now() - (delta || HOUR);
2021-05-28 19:11:19 +00:00
}
export class RetryPlaceholders {
private items: Array<RetryItemType>;
private byConversation: ByConversationLookupType;
private byMessage: ByMessageLookupType;
2021-06-08 21:51:58 +00:00
private retryReceiptLifespan: number;
constructor(options: { retryReceiptLifespan?: number } = {}) {
2021-05-28 19:11:19 +00:00
if (!window.storage) {
throw new Error(
'RetryPlaceholders.constructor: window.storage not available!'
);
}
const parsed = retryItemListSchema.safeParse(
window.storage.get(STORAGE_KEY, new Array<RetryItemType>())
2021-05-28 19:11:19 +00:00
);
if (!parsed.success) {
log.warn(
2021-05-28 19:11:19 +00:00
`RetryPlaceholders.constructor: Data fetched from storage did not match schema: ${JSON.stringify(
parsed.error.flatten()
)}`
);
}
this.items = parsed.success ? parsed.data : [];
this.sortByExpiresAtAsc();
this.byConversation = this.makeByConversationLookup();
this.byMessage = this.makeByMessageLookup();
2021-06-08 21:51:58 +00:00
this.retryReceiptLifespan = options.retryReceiptLifespan || HOUR;
log.info(
`RetryPlaceholders.constructor: Started with ${this.items.length} items, lifespan of ${this.retryReceiptLifespan}`
);
2021-05-28 19:11:19 +00:00
}
// Arranging local data for efficiency
sortByExpiresAtAsc(): void {
this.items.sort(
(left: RetryItemType, right: RetryItemType) =>
left.receivedAt - right.receivedAt
);
}
makeByConversationLookup(): ByConversationLookupType {
return groupBy(this.items, item => item.conversationId);
}
makeByMessageLookup(): ByMessageLookupType {
const lookup = new Map<string, RetryItemType>();
this.items.forEach(item => {
lookup.set(getItemId(item.conversationId, item.sentAt), item);
});
return lookup;
}
makeLookups(): void {
this.byConversation = this.makeByConversationLookup();
this.byMessage = this.makeByMessageLookup();
}
// Basic data management
async add(item: RetryItemType): Promise<void> {
const parsed = retryItemSchema.safeParse(item);
if (!parsed.success) {
throw new Error(
`RetryPlaceholders.add: Item did not match schema ${JSON.stringify(
parsed.error.flatten()
)}`
);
}
this.items.push(item);
this.sortByExpiresAtAsc();
this.makeLookups();
await this.save();
}
async save(): Promise<void> {
await window.storage.put(STORAGE_KEY, this.items);
}
// Finding items in different ways
getCount(): number {
return this.items.length;
}
getNextToExpire(): RetryItemType | undefined {
return this.items[0];
}
async getExpiredAndRemove(): Promise<Array<RetryItemType>> {
2021-06-08 21:51:58 +00:00
const expiration = getDeltaIntoPast(this.retryReceiptLifespan);
2021-05-28 19:11:19 +00:00
const max = this.items.length;
const result: Array<RetryItemType> = [];
for (let i = 0; i < max; i += 1) {
const item = this.items[i];
if (item.receivedAt <= expiration) {
result.push(item);
} else {
break;
}
}
log.info(
2021-05-28 19:11:19 +00:00
`RetryPlaceholders.getExpiredAndRemove: Found ${result.length} expired items`
);
this.items.splice(0, result.length);
this.makeLookups();
await this.save();
return result;
}
2021-06-08 21:51:58 +00:00
async findByConversationAndMarkOpened(conversationId: string): Promise<void> {
let changed = 0;
const items = this.byConversation[conversationId];
(items || []).forEach(item => {
if (!item.wasOpened) {
changed += 1;
// eslint-disable-next-line no-param-reassign
item.wasOpened = true;
}
});
2021-05-28 19:11:19 +00:00
2021-06-08 21:51:58 +00:00
if (changed > 0) {
log.info(
2021-06-08 21:51:58 +00:00
`RetryPlaceholders.findByConversationAndMarkOpened: Updated ${changed} items for conversation ${conversationId}`
);
2021-05-28 19:11:19 +00:00
2021-06-08 21:51:58 +00:00
await this.save();
}
2021-05-28 19:11:19 +00:00
}
async findByMessageAndRemove(
conversationId: string,
sentAt: number
): Promise<RetryItemType | undefined> {
const result = this.byMessage.get(getItemId(conversationId, sentAt));
if (!result) {
return undefined;
}
const index = this.items.findIndex(item => item === result);
this.items.splice(index, 1);
this.makeLookups();
await this.save();
return result;
}
}