mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-03 23:32:22 -08:00
229 lines
7.1 KiB
TypeScript
229 lines
7.1 KiB
TypeScript
import type { MatrixClient, MSC3575RoomData, SlidingSync } from '$types/matrix-sdk';
|
|
import { EventType, MatrixEvent, SlidingSyncEvent } from '$types/matrix-sdk';
|
|
import { CustomAccountDataEvent } from '$types/matrix/accountData';
|
|
import { CustomStateEvent } from '$types/matrix/room';
|
|
|
|
const CACHE_VERSION = 1;
|
|
const CACHE_KEY_PREFIX = 'sable.slidingSyncSidebarCache.';
|
|
const CACHE_WRITE_DELAY_MS = 500;
|
|
const MAX_CACHED_ROOMS = 2000;
|
|
const HYDRATION_BATCH_SIZE = 50;
|
|
|
|
type StateEvent = MSC3575RoomData['required_state'][number];
|
|
|
|
type SidebarCacheData = {
|
|
version: number;
|
|
rooms: Record<string, MSC3575RoomData>;
|
|
accountData: Record<string, Record<string, unknown>>;
|
|
};
|
|
|
|
const CACHED_ACCOUNT_DATA_TYPES = new Set<string>([
|
|
EventType.Direct,
|
|
CustomAccountDataEvent.CinnySpaces,
|
|
]);
|
|
|
|
const CACHED_STATE_TYPES = new Set<string>([
|
|
EventType.RoomCreate,
|
|
EventType.RoomName,
|
|
EventType.RoomTopic,
|
|
EventType.RoomAvatar,
|
|
EventType.RoomCanonicalAlias,
|
|
EventType.RoomJoinRules,
|
|
EventType.RoomHistoryVisibility,
|
|
EventType.RoomGuestAccess,
|
|
EventType.RoomEncryption,
|
|
EventType.RoomTombstone,
|
|
EventType.RoomPowerLevels,
|
|
EventType.RoomPinnedEvents,
|
|
EventType.RoomServerAcl,
|
|
EventType.SpaceChild,
|
|
EventType.SpaceParent,
|
|
CustomStateEvent.PowerLevelTags,
|
|
CustomStateEvent.RoomWidget,
|
|
CustomStateEvent.RoomAbbreviations,
|
|
CustomStateEvent.RoomBanner,
|
|
]);
|
|
|
|
const emptyCache = (): SidebarCacheData => ({
|
|
version: CACHE_VERSION,
|
|
rooms: {},
|
|
accountData: {},
|
|
});
|
|
|
|
const stateEventKey = (event: StateEvent): string => `${event.type}\u0000${event.state_key}`;
|
|
|
|
const mergeStateEvents = (
|
|
previous: StateEvent[] | undefined,
|
|
incoming: StateEvent[] | undefined,
|
|
userId: string
|
|
): StateEvent[] => {
|
|
const merged = new Map<string, StateEvent>();
|
|
previous?.forEach((event) => merged.set(stateEventKey(event), event));
|
|
incoming?.forEach((event) => {
|
|
const cacheMember = event.type === 'm.room.member' && event.state_key === userId;
|
|
if (cacheMember || CACHED_STATE_TYPES.has(event.type)) {
|
|
merged.set(stateEventKey(event), event);
|
|
}
|
|
});
|
|
return [...merged.values()];
|
|
};
|
|
|
|
const mergeRoomData = (
|
|
previous: MSC3575RoomData | undefined,
|
|
incoming: MSC3575RoomData,
|
|
userId: string
|
|
): MSC3575RoomData => ({
|
|
...previous,
|
|
...incoming,
|
|
name: incoming.name ?? previous?.name ?? '',
|
|
heroes: incoming.heroes ?? previous?.heroes,
|
|
notification_count: incoming.notification_count ?? previous?.notification_count,
|
|
highlight_count: incoming.highlight_count ?? previous?.highlight_count,
|
|
joined_count: incoming.joined_count ?? previous?.joined_count,
|
|
invited_count: incoming.invited_count ?? previous?.invited_count,
|
|
is_dm: incoming.is_dm ?? previous?.is_dm,
|
|
bump_stamp: incoming.bump_stamp ?? previous?.bump_stamp,
|
|
required_state: mergeStateEvents(previous?.required_state, incoming.required_state, userId),
|
|
invite_state: incoming.invite_state
|
|
? mergeStateEvents(previous?.invite_state, incoming.invite_state, userId)
|
|
: previous?.invite_state,
|
|
timeline: [],
|
|
initial: true,
|
|
limited: false,
|
|
num_live: 0,
|
|
prev_batch: undefined,
|
|
});
|
|
|
|
const parseCache = (value: string | null): SidebarCacheData => {
|
|
if (!value) return emptyCache();
|
|
try {
|
|
const parsed = JSON.parse(value) as Partial<SidebarCacheData>;
|
|
if (parsed.version !== CACHE_VERSION || !parsed.rooms || !parsed.accountData) {
|
|
return emptyCache();
|
|
}
|
|
return parsed as SidebarCacheData;
|
|
} catch {
|
|
return emptyCache();
|
|
}
|
|
};
|
|
|
|
const hydrateRoomBatch = async (
|
|
slidingSync: SlidingSync,
|
|
rooms: [string, MSC3575RoomData][],
|
|
startIndex = 0
|
|
): Promise<void> => {
|
|
const batch = rooms.slice(startIndex, startIndex + HYDRATION_BATCH_SIZE);
|
|
await Promise.all(
|
|
batch.map(([roomId, roomData]) =>
|
|
slidingSync.emitPromised(SlidingSyncEvent.RoomData, roomId, roomData)
|
|
)
|
|
);
|
|
if (startIndex + HYDRATION_BATCH_SIZE < rooms.length) {
|
|
await hydrateRoomBatch(slidingSync, rooms, startIndex + HYDRATION_BATCH_SIZE);
|
|
}
|
|
};
|
|
|
|
export class SlidingSyncSidebarCache {
|
|
public static clear(userId: string): void {
|
|
try {
|
|
globalThis.localStorage?.removeItem(`${CACHE_KEY_PREFIX}${encodeURIComponent(userId)}`);
|
|
} catch {
|
|
// Storage can be disabled for this origin.
|
|
}
|
|
}
|
|
|
|
private readonly storageKey: string;
|
|
|
|
private data: SidebarCacheData;
|
|
|
|
private writeTimer: ReturnType<typeof globalThis.setTimeout> | undefined;
|
|
|
|
public constructor(private readonly userId: string) {
|
|
this.storageKey = `${CACHE_KEY_PREFIX}${encodeURIComponent(userId)}`;
|
|
let stored: string | null = null;
|
|
try {
|
|
stored = globalThis.localStorage?.getItem(this.storageKey) ?? null;
|
|
} catch {
|
|
// Storage can be disabled for this origin.
|
|
}
|
|
this.data = parseCache(stored);
|
|
}
|
|
|
|
public cacheRoom(roomId: string, roomData: MSC3575RoomData): void {
|
|
this.data.rooms[roomId] = mergeRoomData(this.data.rooms[roomId], roomData, this.userId);
|
|
this.scheduleWrite();
|
|
}
|
|
|
|
public removeRoom(roomId: string): void {
|
|
if (!this.data.rooms[roomId]) return;
|
|
delete this.data.rooms[roomId];
|
|
this.scheduleWrite();
|
|
}
|
|
|
|
public reconcileRooms(validRoomIds: ReadonlySet<string>): string[] {
|
|
const removedRoomIds = Object.keys(this.data.rooms).filter(
|
|
(roomId) => !validRoomIds.has(roomId)
|
|
);
|
|
if (removedRoomIds.length === 0) return removedRoomIds;
|
|
|
|
removedRoomIds.forEach((roomId) => delete this.data.rooms[roomId]);
|
|
this.scheduleWrite();
|
|
return removedRoomIds;
|
|
}
|
|
|
|
public cacheAccountData(event: MatrixEvent): void {
|
|
if (!CACHED_ACCOUNT_DATA_TYPES.has(event.getType())) return;
|
|
this.data.accountData[event.getType()] = event.getContent<Record<string, unknown>>();
|
|
this.scheduleWrite();
|
|
}
|
|
|
|
public async hydrate(mx: MatrixClient, slidingSync: SlidingSync): Promise<boolean> {
|
|
const rooms = Object.entries(this.data.rooms);
|
|
const accountData = Object.entries(this.data.accountData);
|
|
if (rooms.length === 0 && accountData.length === 0) return false;
|
|
|
|
if (accountData.length > 0) {
|
|
mx.store.storeAccountDataEvents(
|
|
accountData.map(([type, content]) => new MatrixEvent({ type, content }))
|
|
);
|
|
}
|
|
|
|
await hydrateRoomBatch(slidingSync, rooms);
|
|
return rooms.length > 0;
|
|
}
|
|
|
|
public dispose(): void {
|
|
if (this.writeTimer !== undefined) {
|
|
globalThis.clearTimeout(this.writeTimer);
|
|
this.writeTimer = undefined;
|
|
this.write();
|
|
}
|
|
}
|
|
|
|
private scheduleWrite(): void {
|
|
if (this.writeTimer !== undefined) return;
|
|
this.writeTimer = globalThis.setTimeout(() => {
|
|
this.writeTimer = undefined;
|
|
this.write();
|
|
}, CACHE_WRITE_DELAY_MS);
|
|
}
|
|
|
|
private write(): void {
|
|
let rooms = Object.entries(this.data.rooms)
|
|
.toSorted(([, a], [, b]) => (b.bump_stamp ?? 0) - (a.bump_stamp ?? 0))
|
|
.slice(0, MAX_CACHED_ROOMS);
|
|
|
|
while (true) {
|
|
const nextData = { ...this.data, rooms: Object.fromEntries(rooms) };
|
|
try {
|
|
globalThis.localStorage?.setItem(this.storageKey, JSON.stringify(nextData));
|
|
this.data = nextData;
|
|
return;
|
|
} catch {
|
|
if (rooms.length <= 1) return;
|
|
rooms = rooms.slice(0, Math.floor(rooms.length / 2));
|
|
}
|
|
}
|
|
}
|
|
}
|