refactor: zones => topics

This commit is contained in:
Andy Burke 2025-09-10 12:51:27 -07:00
parent 20a5d1bc88
commit fac8f409f4
26 changed files with 470 additions and 469 deletions

View file

@ -32,16 +32,16 @@ export type EVENT = {
};
};
type ZONE_EVENT_CACHE_ENTRY = {
type TOPIC_EVENT_CACHE_ENTRY = {
collection: FSDB_COLLECTION<EVENT>;
eviction_timeout: number;
};
const ZONE_EVENTS: Record<string, ZONE_EVENT_CACHE_ENTRY> = {};
export function get_events_collection_for_zone(zone_id: string): FSDB_COLLECTION<EVENT> {
ZONE_EVENTS[zone_id] = ZONE_EVENTS[zone_id] ?? {
const TOPIC_EVENTS: Record<string, TOPIC_EVENT_CACHE_ENTRY> = {};
export function get_events_collection_for_topic(topic_id: string): FSDB_COLLECTION<EVENT> {
TOPIC_EVENTS[topic_id] = TOPIC_EVENTS[topic_id] ?? {
collection: new FSDB_COLLECTION<EVENT>({
name: `zones/${zone_id.slice(0, 14)}/${zone_id.slice(0, 34)}/${zone_id}/events`,
name: `topics/${topic_id.slice(0, 14)}/${topic_id.slice(0, 34)}/${topic_id}/events`,
id_field: 'id',
organize: by_lurid,
indexers: {
@ -65,22 +65,22 @@ export function get_events_collection_for_zone(zone_id: string): FSDB_COLLECTION
eviction_timeout: 0
};
if (ZONE_EVENTS[zone_id].eviction_timeout) {
clearTimeout(ZONE_EVENTS[zone_id].eviction_timeout);
if (TOPIC_EVENTS[topic_id].eviction_timeout) {
clearTimeout(TOPIC_EVENTS[topic_id].eviction_timeout);
}
ZONE_EVENTS[zone_id].eviction_timeout = setTimeout(() => {
delete ZONE_EVENTS[zone_id];
TOPIC_EVENTS[topic_id].eviction_timeout = setTimeout(() => {
delete TOPIC_EVENTS[topic_id];
}, 60_000 * 5);
return ZONE_EVENTS[zone_id].collection;
return TOPIC_EVENTS[topic_id].collection;
}
export function clear_zone_events_cache() {
for (const [zone_id, cached] of Object.entries(ZONE_EVENTS)) {
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 ZONE_EVENTS[zone_id];
delete TOPIC_EVENTS[topic_id];
}
}

View file

@ -3,29 +3,29 @@ import { FSDB_COLLECTION } from '@andyburke/fsdb';
import { FSDB_INDEXER_SYMLINKS } from '@andyburke/fsdb/indexers';
/**
* @typedef {object} ZONE_PERMISSIONS
* @property {string[]} read a list of user_ids with read permission for the zone
* @property {string[]} write a list of user_ids with write permission for the zone
* @property {string[]} read_events a list of user_ids with read_events permission for this zone
* @property {string[]} write_events a list of user_ids with write_events permission for this zone
* @typedef {object} TOPIC_PERMISSIONS
* @property {string[]} read a list of user_ids with read permission for the topic
* @property {string[]} write a list of user_ids with write permission for the topic
* @property {string[]} read_events a list of user_ids with read_events permission for this topic
* @property {string[]} write_events a list of user_ids with write_events permission for this topic
*/
/**
* ZONE
* TOPIC
*
* @property {string} id - lurid (stable)
* @property {string} name - channel name (max 64 characters, unique, unstable)
* @property {string} creator_id - user id of the zone creator
* @property {ZONE_PERMISSIONS} permissions - permissions setup for the zone
* @property {string} [icon_url] - optional url for zone icon
* @property {string} [topic] - optional topic for the zone
* @property {string} [rules] - optional zone rules (Markdown/text)
* @property {string[]} [tags] - optional tags for the zone
* @property {Record<string,any>} [meta] - optional metadata about the zone
* @property {string} creator_id - user id of the topic creator
* @property {TOPIC_PERMISSIONS} permissions - permissions setup for the topic
* @property {string} [icon_url] - optional url for topic icon
* @property {string} [topic] - optional topic for the topic
* @property {string} [rules] - optional topic rules (Markdown/text)
* @property {string[]} [tags] - optional tags for the topic
* @property {Record<string,any>} [meta] - optional metadata about the topic
* @property {Record<string,string>} [emojis] - optional emojis table, eg: { 'rofl': 🤣, 'blap': 'https://somewhere.someplace/image.jpg' }
*/
export type ZONE = {
export type TOPIC = {
id: string;
name: string;
creator_id: string;
@ -48,37 +48,37 @@ export type ZONE = {
};
};
export const ZONES = new FSDB_COLLECTION<ZONE>({
name: 'zones',
export const TOPICS = new FSDB_COLLECTION<TOPIC>({
name: 'topics',
id_field: 'id',
organize: by_lurid,
indexers: {
creator_id: new FSDB_INDEXER_SYMLINKS<ZONE>({
creator_id: new FSDB_INDEXER_SYMLINKS<TOPIC>({
name: 'creator_id',
field: 'creator_id',
to_many: true,
organize: by_lurid
}),
name: new FSDB_INDEXER_SYMLINKS<ZONE>({
name: new FSDB_INDEXER_SYMLINKS<TOPIC>({
name: 'name',
get_values_to_index: (zone) => [zone.name.toLowerCase()],
get_values_to_index: (topic) => [topic.name.toLowerCase()],
organize: by_character
}),
tags: new FSDB_INDEXER_SYMLINKS<ZONE>({
tags: new FSDB_INDEXER_SYMLINKS<TOPIC>({
name: 'tags',
get_values_to_index: (zone): string[] => {
return (zone.tags ?? []).map((tag) => tag.toLowerCase());
get_values_to_index: (topic): string[] => {
return (topic.tags ?? []).map((tag) => tag.toLowerCase());
},
to_many: true,
organize: by_character
}),
topic: new FSDB_INDEXER_SYMLINKS<ZONE>({
topic: new FSDB_INDEXER_SYMLINKS<TOPIC>({
name: 'topic',
get_values_to_index: (zone): string[] => {
return (zone.topic ?? '').split(/\W/);
get_values_to_index: (topic): string[] => {
return (topic.topic ?? '').split(/\W/);
},
to_many: true,
organize: by_character