Save storage for defunct and pending call links

This commit is contained in:
ayumi-signal 2024-10-22 11:20:35 -07:00 committed by GitHub
parent a7be33b201
commit c6902ec26a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 474 additions and 31 deletions

View file

@ -12,11 +12,14 @@ import type {
CallLinkRecord,
CallLinkRestrictions,
CallLinkType,
DefunctCallLinkRecord,
DefunctCallLinkType,
} from '../types/CallLink';
import {
type CallLinkStateType,
CallLinkNameMaxByteLength,
callLinkRecordSchema,
defunctCallLinkRecordSchema,
toCallLinkRestrictions,
} from '../types/CallLink';
import { unicodeSlice } from './unicodeSlice';
@ -64,6 +67,11 @@ export function getRoomIdFromRootKey(rootKey: CallLinkRootKey): string {
return rootKey.deriveRoomId().toString('hex');
}
export function getRoomIdFromRootKeyString(rootKeyString: string): string {
const callLinkRootKey = CallLinkRootKey.parse(rootKeyString);
return getRoomIdFromRootKey(callLinkRootKey);
}
export function getCallLinkRootKeyFromUrlKey(key: string): Uint8Array {
// Returns `Buffer` which inherits from `Uint8Array`
return CallLinkRootKey.parse(key).bytes;
@ -167,3 +175,45 @@ export function callLinkToRecord(callLink: CallLinkType): CallLinkRecord {
storageNeedsSync: callLink.storageNeedsSync ? 1 : 0,
});
}
export function defunctCallLinkFromRecord(
record: DefunctCallLinkRecord
): DefunctCallLinkType {
if (record.rootKey == null) {
throw new Error('CallLink.defunctCallLinkFromRecord: rootKey is null');
}
const rootKey = fromRootKeyBytes(record.rootKey);
const adminKey = record.adminKey ? fromAdminKeyBytes(record.adminKey) : null;
return {
roomId: record.roomId,
rootKey,
adminKey,
storageID: record.storageID || undefined,
storageVersion: record.storageVersion || undefined,
storageUnknownFields: record.storageUnknownFields || undefined,
storageNeedsSync: record.storageNeedsSync === 1,
};
}
export function defunctCallLinkToRecord(
defunctCallLink: DefunctCallLinkType
): DefunctCallLinkRecord {
if (defunctCallLink.rootKey == null) {
throw new Error('CallLink.defunctCallLinkToRecord: rootKey is null');
}
const rootKey = toRootKeyBytes(defunctCallLink.rootKey);
const adminKey = defunctCallLink.adminKey
? toAdminKeyBytes(defunctCallLink.adminKey)
: null;
return parseStrict(defunctCallLinkRecordSchema, {
roomId: defunctCallLink.roomId,
rootKey,
adminKey,
storageID: defunctCallLink.storageID || null,
storageVersion: defunctCallLink.storageVersion || null,
storageUnknownFields: defunctCallLink.storageUnknownFields || null,
storageNeedsSync: defunctCallLink.storageNeedsSync ? 1 : 0,
});
}