feature: events polling

This commit is contained in:
Andy Burke 2025-07-02 21:28:07 -07:00
parent 61a51017a3
commit b700251278
19 changed files with 353 additions and 77 deletions

View file

@ -11,7 +11,7 @@ import { FSDB_INDEXER_SYMLINKS } from 'jsr:@andyburke/fsdb/indexers';
/**
* Event
*
* @property {string} id - room_id(lurid):event_id(lurid)
* @property {string} id - lurid
* @property {string} creator_id - id of the source user
* @property {string} room_id - id of the target room
* @property {string} type - event type
@ -31,28 +31,55 @@ export type EVENT = {
};
};
export const EVENTS = new FSDB_COLLECTION<EVENT>({
name: 'events',
id_field: 'id',
organize: (combined_id: string) => {
const [room_id, event_id] = combined_id.split(':', 2);
return ['rooms', room_id, event_id.substring(0, 14), `${event_id}.json`];
},
indexers: {
creator_id: new FSDB_INDEXER_SYMLINKS<EVENT>({
name: 'creator_id',
field: 'creator_id',
to_many: true,
organize: by_lurid
}),
type ROOM_EVENT_CACHE_ENTRY = {
collection: FSDB_COLLECTION<EVENT>;
eviction_timeout: number;
};
tags: new FSDB_INDEXER_SYMLINKS<EVENT>({
name: 'tags',
get_values_to_index: (event: EVENT): string[] => {
return (event.tags ?? []).map((tag: string) => tag.toLowerCase());
},
to_many: true,
organize: by_character
})
const ROOM_EVENTS: Record<string, ROOM_EVENT_CACHE_ENTRY> = {};
export function get_events_collection_for_room(room_id: string): FSDB_COLLECTION<EVENT> {
ROOM_EVENTS[room_id] = ROOM_EVENTS[room_id] ?? {
collection: new FSDB_COLLECTION<EVENT>({
name: `rooms/${room_id.substring(0, 14)}/${room_id.substring(0, 36)}/${room_id}/events`,
id_field: 'id',
organize: by_lurid,
indexers: {
creator_id: new FSDB_INDEXER_SYMLINKS<EVENT>({
name: 'creator_id',
field: 'creator_id',
to_many: true,
organize: by_lurid
}),
tags: new FSDB_INDEXER_SYMLINKS<EVENT>({
name: 'tags',
get_values_to_index: (event: EVENT): string[] => {
return (event.tags ?? []).map((tag: string) => tag.toLowerCase());
},
to_many: true,
organize: by_character
})
}
}),
eviction_timeout: 0
};
if (ROOM_EVENTS[room_id].eviction_timeout) {
clearTimeout(ROOM_EVENTS[room_id].eviction_timeout);
}
});
ROOM_EVENTS[room_id].eviction_timeout = setTimeout(() => {
delete ROOM_EVENTS[room_id];
}, 60_000 * 5);
return ROOM_EVENTS[room_id].collection;
}
export function clear_room_events_cache() {
for (const [room_id, cached] of Object.entries(ROOM_EVENTS)) {
if (cached.eviction_timeout) {
clearTimeout(cached.eviction_timeout);
}
delete ROOM_EVENTS[room_id];
}
}