import { by_character, by_lurid } from '@andyburke/fsdb/organizers'; import { FSDB_COLLECTION } from '@andyburke/fsdb'; import { FSDB_INDEXER_SYMLINKS } from '@andyburke/fsdb/indexers'; /** * @typedef {object} TIMESTAMPS * @property {string} created when the event was created * @property {string} updated when the event was last updated */ /** * Event * * @property {string} id - lurid * @property {string} creator_id - id of the source user * @property {string} type - event type * @property {string} [parent_id] - optional parent event id * @property {string[]} [tags] - optional event tags * @property {Record} [data] - optional data payload of the event * @property {TIMESTAMPS} timestamps - timestamps that will be set by the server */ export type EVENT = { id: string; creator_id: string; type: string; parent_id?: string; tags?: string[]; data?: Record; timestamps: { created: string; updated: string; }; }; type TOPIC_EVENT_CACHE_ENTRY = { collection: FSDB_COLLECTION; eviction_timeout: number; }; const TOPIC_EVENT_ID_MATCHER = /^(?.*):(?.*)$/; const TOPIC_EVENTS: Record = {}; export function get_events_collection_for_topic(topic_id: string): FSDB_COLLECTION { TOPIC_EVENTS[topic_id] = TOPIC_EVENTS[topic_id] ?? { collection: new FSDB_COLLECTION({ name: `topics/${topic_id.slice(0, 14)}/${topic_id.slice(0, 34)}/${topic_id}/events`, id_field: 'id', organize: (id) => { TOPIC_EVENT_ID_MATCHER.lastIndex = 0; const { groups: { event_type, event_id } } = TOPIC_EVENT_ID_MATCHER.exec(id ?? '') ?? { groups: {} }; return [ event_type, event_id.slice(0, 14), event_id.slice(0, 34), event_id, `${event_id}.json` ]; }, indexers: { creator_id: new FSDB_INDEXER_SYMLINKS({ name: 'creator_id', field: 'creator_id', to_many: true, organize: by_lurid }), tags: new FSDB_INDEXER_SYMLINKS({ 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 (TOPIC_EVENTS[topic_id].eviction_timeout) { clearTimeout(TOPIC_EVENTS[topic_id].eviction_timeout); } TOPIC_EVENTS[topic_id].eviction_timeout = setTimeout(() => { delete TOPIC_EVENTS[topic_id]; }, 60_000 * 5); return TOPIC_EVENTS[topic_id].collection; } export function clear_topic_events_cache() { for (const [topic_id, cached] of Object.entries(TOPIC_EVENTS)) { if (cached.eviction_timeout) { clearTimeout(cached.eviction_timeout); } delete TOPIC_EVENTS[topic_id]; } }