autonomous.contact/models/event.ts

56 lines
1.6 KiB
TypeScript

import { by_character, by_lurid } from '@andyburke/fsdb/organizers';
import { FSDB_COLLECTION } from 'jsr:@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 - room_id(lurid):event_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
* @property {string[]} [tags] - optional event tags
* @property {Record<string,any>} [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;
tags?: string[];
data?: Record<string, any>;
timestamps: {
created: string;
updated: string;
};
};
export const EVENTS = new FSDB_COLLECTION<EVENT>({
name: 'events',
id_field: 'id',
organize: (combined_id) => {
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',
organize: by_lurid
}),
tags: new FSDB_INDEXER_SYMLINKS<EVENT>({
name: 'tags',
get_values_to_index: (event): string[] => {
return (event.tags ?? []).map((tag) => tag.toLowerCase());
},
organize: by_character
})
}
});