autonomous.contact/public/api/events/index.ts

253 lines
6.6 KiB
TypeScript
Raw Normal View History

import lurid from '@andyburke/lurid';
import { get_session, get_user, PRECHECK_TABLE, require_user, user_has_write_permission_for_event } from '../../../utils/prechecks.ts';
import * as CANNED_RESPONSES from '../../../utils/canned_responses.ts';
import { EVENT, EVENTS, VALIDATE_EVENT } from '../../../models/event.ts';
import parse_body from '../../../utils/bodyparser.ts';
import { FSDB_SEARCH_OPTIONS, WALK_ENTRY } from '@andyburke/fsdb';
import { WATCH, WATCHES } from '../../../models/watch.ts';
import { flatten } from '../../../utils/object_helpers.ts';
import { CHANNEL, CHANNELS } from '../../../models/channel.ts';
export const PRECHECKS: PRECHECK_TABLE = {};
// GET /api/events - get events
// query parameters:
// partial_id: the partial id subset you would like to match (remember, lurids are lexigraphically sorted)
PRECHECKS.GET = [get_session, get_user, require_user];
2025-07-02 21:28:07 -07:00
export async function GET(request: Request, meta: Record<string, any>): Promise<Response> {
const sorts = EVENTS.sorts;
2025-07-02 21:28:07 -07:00
const sort_name: string = meta.query.sort ?? 'newest';
const key = sort_name as keyof typeof sorts;
const sort: any = sorts[key];
if (!sort) {
return Response.json({
error: {
2025-07-02 21:28:07 -07:00
message: 'You must specify a sort: newest, oldest, latest, stalest',
cause: 'invalid_sort'
}
}, {
status: 400
});
}
2025-07-02 21:28:07 -07:00
const options: FSDB_SEARCH_OPTIONS<EVENT> = {
...(meta.query ?? {}),
limit: Math.min(parseInt(meta.query?.limit ?? '100', 10), 1_000),
2025-10-15 17:50:48 -07:00
offset: Math.max(parseInt(meta.query?.offset ?? '0', 10), 0),
2025-07-02 21:28:07 -07:00
sort,
filter: (entry: WALK_ENTRY<EVENT>) => {
const {
event_type,
event_id
} = /^.*\/events\/(?<event_type>.*?)\/.*\/(?<event_id>[A-Za-z-]+)\.json$/.exec(entry.path)?.groups ?? {};
2025-07-02 21:28:07 -07:00
2025-10-15 17:50:48 -07:00
if (meta.query.after_id && event_id <= meta.query.after_id) {
2025-07-02 21:28:07 -07:00
return false;
}
2025-10-15 17:50:48 -07:00
if (meta.query.before_id && event_id >= meta.query.before_id) {
2025-07-02 21:28:07 -07:00
return false;
}
2025-10-14 22:20:54 -07:00
if (meta.query.type && !meta.query.type.split(',').includes(event_type)) {
2025-07-04 12:15:06 -07:00
return false;
}
2025-07-02 21:28:07 -07:00
return true;
}
};
const headers = {
'Cache-Control': 'no-cache, must-revalidate'
};
const results = (await EVENTS.all(options))
.map((entry: WALK_ENTRY<EVENT>) => entry.load())
.filter((event) => typeof event.channel === 'undefined') // channel events must be queried via the channel's api
.sort((lhs_item: EVENT, rhs_item: EVENT) => rhs_item.timestamps.created.localeCompare(lhs_item.timestamps.created));
2025-07-02 21:28:07 -07:00
// long-polling support
if (results.length === 0 && meta.query.wait) {
2025-07-04 12:15:06 -07:00
return new Promise((resolve, reject) => {
2025-07-02 21:28:07 -07:00
function on_create(create_event: any) {
2025-10-15 17:50:48 -07:00
if (meta.query.type && !meta.query.type.split(',').includes(create_event.item.type)) {
return;
}
2025-07-02 21:28:07 -07:00
results.push(create_event.item);
clearTimeout(timeout);
EVENTS.off('create', on_create);
2025-07-02 21:28:07 -07:00
return resolve(Response.json(results, {
status: 200,
headers
}));
}
const timeout = setTimeout(() => {
EVENTS.off('create', on_create);
2025-07-02 21:28:07 -07:00
return resolve(Response.json(results, {
status: 200,
headers
}));
}, 60_000); // 60 seconds
EVENTS.on('create', on_create);
2025-07-02 21:28:07 -07:00
request.signal.addEventListener('abort', () => {
EVENTS.off('create', on_create);
clearTimeout(timeout);
reject(new Error('request aborted'));
2025-07-02 21:28:07 -07:00
});
2025-07-04 12:15:06 -07:00
Deno.addSignalListener('SIGINT', () => {
EVENTS.off('create', on_create);
clearTimeout(timeout);
2025-07-04 12:15:06 -07:00
return resolve(Response.json(results, {
status: 200,
headers
}));
});
2025-07-02 21:28:07 -07:00
});
}
return Response.json(results, {
status: 200,
headers
});
}
async function update_watches(event: EVENT) {
const limit = 100;
let more_to_process;
let offset = 0;
do {
const watches: WATCH[] = (await WATCHES.all({
limit,
offset
})).map((entry) => entry.load());
for (const watch of watches) {
if (typeof watch.type === 'string' && event.type !== watch.type) {
continue;
}
if (typeof watch.parent_id === 'string' && event.parent_id !== watch.parent_id) {
continue;
}
if (typeof watch.channel === 'string' && event.channel !== watch.channel) {
continue;
}
if (typeof watch.topic === 'string' && event.topic !== watch.topic) {
continue;
}
if (typeof watch.tags !== 'undefined' && !watch.tags.every((tag) => event.tags?.includes(tag))) {
continue;
}
if (typeof watch.data !== 'undefined') {
const event_data = flatten(event.data ?? {});
for (const [key, value] of Object.entries(flatten(watch.data))) {
const matcher = new RegExp(value);
if (!matcher.test(event_data[key])) {
continue;
}
}
}
if (event.id < watch.last_id_seen) {
continue;
}
watch.last_id_seen = event.id;
// TODO: send a notification
console.dir({
notification: {
watch,
event
}
});
}
offset += watches.length;
more_to_process = watches.length === limit;
} while (more_to_process);
}
// POST /api/events - Create an event
PRECHECKS.POST = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
const user_can_create_events = meta.user.permissions.some((permission: string) => permission.indexOf('events.create') === 0);
if (!user_can_create_events) {
return CANNED_RESPONSES.permission_denied();
}
}];
export async function POST(req: Request, meta: Record<string, any>): Promise<Response> {
try {
const now = new Date().toISOString();
2025-07-02 21:28:07 -07:00
const body = await parse_body(req);
2025-07-02 21:28:07 -07:00
const event: EVENT = {
type: 'unknown',
...body,
creator_id: meta.user.id,
timestamps: {
created: now,
updated: now
}
};
event.id = `${event.type}:${lurid()}`;
2025-07-04 12:15:06 -07:00
const errors = VALIDATE_EVENT(event);
if (errors) {
return Response.json({
errors
}, {
status: 400
});
}
if (!user_has_write_permission_for_event(meta.user, event)) {
return CANNED_RESPONSES.permission_denied();
}
if (event.channel) {
const channel: CHANNEL | null = await CHANNELS.get(event.channel);
if (!channel) {
return Response.json({
errors: [{
cause: 'missing_channel',
message: 'No such channel exists.'
}]
}, {
status: 400
});
}
const user_can_write_events_to_channel = channel.permissions.events.write.length === 0 ? true : channel.permissions.events.write.includes(meta.user.id);
if (!user_can_write_events_to_channel) {
return CANNED_RESPONSES.permission_denied();
}
}
await EVENTS.create(event);
update_watches(event);
2025-07-02 21:28:07 -07:00
return Response.json(event, {
status: 201
});
} catch (error) {
return Response.json({
error: {
message: (error as Error).message ?? 'Unknown Error!',
cause: (error as Error).cause ?? 'unknown'
}
}, { status: 500 });
}
}