forked from andyburke/autonomous.contact
refactor: zones => topics
This commit is contained in:
parent
20a5d1bc88
commit
fac8f409f4
26 changed files with 470 additions and 469 deletions
23
public/api/topics/:topic_id/README.md
Normal file
23
public/api/topics/:topic_id/README.md
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
# /api/topics/:topic_id
|
||||
|
||||
Interact with a specific topic.
|
||||
|
||||
## GET /api/topics/:topic_id
|
||||
|
||||
Get the topic specified by `:topic_id`.
|
||||
|
||||
## PUT /api/topics/:topic_id
|
||||
|
||||
Update the topics specified by `:topic_id`.
|
||||
|
||||
Eg:
|
||||
|
||||
```
|
||||
{
|
||||
name?: string;
|
||||
}
|
||||
```
|
||||
|
||||
## DELETE /api/topics/:topic_id
|
||||
|
||||
Delete the topic specified by `:topic_id`.
|
||||
0
public/api/topics/:topic_id/events/:event_id/README.md
Normal file
0
public/api/topics/:topic_id/events/:event_id/README.md
Normal file
166
public/api/topics/:topic_id/events/:event_id/index.ts
Normal file
166
public/api/topics/:topic_id/events/:event_id/index.ts
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
import { FSDB_COLLECTION } from '@andyburke/fsdb';
|
||||
import { EVENT, get_events_collection_for_topic } from '../../../../../../models/event.ts';
|
||||
import { TOPIC, TOPICS } from '../../../../../../models/topic.ts';
|
||||
import parse_body from '../../../../../../utils/bodyparser.ts';
|
||||
import * as CANNED_RESPONSES from '../../../../../../utils/canned_responses.ts';
|
||||
import { get_session, get_user, PRECHECK_TABLE, require_user } from '../../../../../../utils/prechecks.ts';
|
||||
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/topics/:topic_id/events/:id - Get an event
|
||||
PRECHECKS.GET = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public = topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || topic.permissions.read.includes(meta.user.id);
|
||||
const topic_has_public_events = user_has_read_for_topic && (topic.permissions.read_events.length === 0);
|
||||
const user_has_read_events_for_topic = user_has_read_for_topic &&
|
||||
(topic_has_public_events || topic.permissions.read_events.includes(meta.user.id));
|
||||
|
||||
if (!user_has_read_events_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function GET(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const events: FSDB_COLLECTION<EVENT> = get_events_collection_for_topic(meta.topic.id);
|
||||
const event: EVENT | null = await events.get(meta.params.event_id);
|
||||
|
||||
if (!event) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
return Response.json(event, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
|
||||
// PUT /api/topics/:topic_id/events/:event_id - Update event
|
||||
PRECHECKS.PUT = [
|
||||
get_session,
|
||||
get_user,
|
||||
require_user,
|
||||
(_req: Request, _meta: Record<string, any>): Response | undefined => {
|
||||
if (Deno.env.get('APPEND_ONLY_EVENTS')) {
|
||||
return CANNED_RESPONSES.append_only_events();
|
||||
}
|
||||
},
|
||||
async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public: boolean = meta.topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || meta.topic.permissions.read.includes(meta.user.id);
|
||||
const topic_events_are_publicly_writable = meta.topic.permissions.write_events.length === 0;
|
||||
const user_has_write_events_for_topic = user_has_read_for_topic &&
|
||||
(topic_events_are_publicly_writable || meta.topic.permissions.write_events.includes(meta.user.id));
|
||||
|
||||
if (!user_has_write_events_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}
|
||||
];
|
||||
export async function PUT(req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
try {
|
||||
const events: FSDB_COLLECTION<EVENT> = get_events_collection_for_topic(meta.topic.id);
|
||||
const event: EVENT | null = await events.get(meta.params.event_id);
|
||||
|
||||
if (!event) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
if (event.creator_id !== meta.user.id) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
|
||||
const body = await parse_body(req);
|
||||
const updated = {
|
||||
...event,
|
||||
...body,
|
||||
id: event.id,
|
||||
creator_id: event.creator_id,
|
||||
timestamps: {
|
||||
created: event.timestamps.created,
|
||||
updated: now
|
||||
}
|
||||
};
|
||||
|
||||
await events.update(updated);
|
||||
return Response.json(updated, {
|
||||
status: 200
|
||||
});
|
||||
} catch (err) {
|
||||
return Response.json({
|
||||
error: {
|
||||
message: (err as Error)?.message ?? 'Unknown error due to invalid data.',
|
||||
cause: (err as Error)?.cause ?? 'invalid_data'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/topics/:topic_id/events/:event_id - Delete event
|
||||
PRECHECKS.DELETE = [
|
||||
get_session,
|
||||
get_user,
|
||||
require_user,
|
||||
(_req: Request, _meta: Record<string, any>): Response | undefined => {
|
||||
if (Deno.env.get('APPEND_ONLY_EVENTS')) {
|
||||
return CANNED_RESPONSES.append_only_events();
|
||||
}
|
||||
},
|
||||
async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public: boolean = meta.topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || meta.topic.permissions.read.includes(meta.user.id);
|
||||
const topic_events_are_publicly_writable = meta.topic.permissions.write_events.length === 0;
|
||||
const user_has_write_events_for_topic = user_has_read_for_topic &&
|
||||
(topic_events_are_publicly_writable || meta.topic.permissions.write_events.includes(meta.user.id));
|
||||
|
||||
if (!user_has_write_events_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}
|
||||
];
|
||||
export async function DELETE(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const events: FSDB_COLLECTION<EVENT> = get_events_collection_for_topic(meta.topic.id);
|
||||
const event: EVENT | null = await events.get(meta.params.event_id);
|
||||
if (!event) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
await events.delete(event);
|
||||
|
||||
return Response.json({
|
||||
deleted: true
|
||||
}, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
15
public/api/topics/:topic_id/events/README.md
Normal file
15
public/api/topics/:topic_id/events/README.md
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# /api/topics/:topic_id/events
|
||||
|
||||
Interact with a events for a topic.
|
||||
|
||||
## GET /api/topics/:topic_id/events
|
||||
|
||||
Get events for the given topic.
|
||||
|
||||
## PUT /api/topics/:topic_id/events/:event_id
|
||||
|
||||
Update an event.
|
||||
|
||||
## DELETE /api/topics/:topic_id/events/:event_id
|
||||
|
||||
Delete an event.
|
||||
183
public/api/topics/:topic_id/events/index.ts
Normal file
183
public/api/topics/:topic_id/events/index.ts
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
import lurid from '@andyburke/lurid';
|
||||
import { get_session, get_user, PRECHECK_TABLE, require_user } from '../../../../../utils/prechecks.ts';
|
||||
import { TOPIC, TOPICS } from '../../../../../models/topic.ts';
|
||||
import * as CANNED_RESPONSES from '../../../../../utils/canned_responses.ts';
|
||||
import { EVENT, get_events_collection_for_topic } from '../../../../../models/event.ts';
|
||||
import parse_body from '../../../../../utils/bodyparser.ts';
|
||||
import { FSDB_COLLECTION, FSDB_SEARCH_OPTIONS, WALK_ENTRY } from '@andyburke/fsdb';
|
||||
import * as path from '@std/path';
|
||||
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/topics/:topic_id/events - get topic 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, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public: boolean = meta.topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || meta.topic.permissions.read.includes(meta.user.id);
|
||||
const topic_events_are_public = meta.topic.permissions.read_events.length === 0;
|
||||
const user_has_read_events_for_topic = user_has_read_for_topic &&
|
||||
(topic_events_are_public || meta.topic.permissions.read_events.includes(meta.user.id));
|
||||
|
||||
if (!user_has_read_events_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function GET(request: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const events: FSDB_COLLECTION<EVENT> = get_events_collection_for_topic(meta.topic.id);
|
||||
|
||||
const sorts = events.sorts;
|
||||
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: {
|
||||
message: 'You must specify a sort: newest, oldest, latest, stalest',
|
||||
cause: 'invalid_sort'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
const options: FSDB_SEARCH_OPTIONS<EVENT> = {
|
||||
...(meta.query ?? {}),
|
||||
limit: Math.min(parseInt(meta.query?.limit ?? '10'), 1_000),
|
||||
sort,
|
||||
filter: (entry: WALK_ENTRY<EVENT>) => {
|
||||
const [event_id, event_type] = path.basename(entry.path).replace(/\.json$/i, '').split(':');
|
||||
|
||||
if (meta.query.after_id && event_id <= meta.query.after_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (meta.query.before_id && event_id >= meta.query.before_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (meta.query.type && event_type !== meta.query.type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
const headers = {
|
||||
'Cache-Control': 'no-cache, must-revalidate'
|
||||
};
|
||||
|
||||
const results = (await events.all(options))
|
||||
.map((entry: WALK_ENTRY<EVENT>) => entry.load())
|
||||
.sort((lhs_item: EVENT, rhs_item: EVENT) => rhs_item.timestamps.created.localeCompare(lhs_item.timestamps.created));
|
||||
|
||||
// long-polling support
|
||||
if (results.length === 0 && meta.query.wait) {
|
||||
return new Promise((resolve, reject) => {
|
||||
function on_create(create_event: any) {
|
||||
results.push(create_event.item);
|
||||
clearTimeout(timeout);
|
||||
events.off('create', on_create);
|
||||
|
||||
return resolve(Response.json(results, {
|
||||
status: 200,
|
||||
headers
|
||||
}));
|
||||
}
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
events.off('create', on_create);
|
||||
return resolve(Response.json(results, {
|
||||
status: 200,
|
||||
headers
|
||||
}));
|
||||
}, 60_000); // 60 seconds
|
||||
events.on('create', on_create);
|
||||
request.signal.addEventListener('abort', () => {
|
||||
events.off('create', on_create);
|
||||
clearTimeout(timeout);
|
||||
reject(new Error('request aborted'));
|
||||
});
|
||||
Deno.addSignalListener('SIGINT', () => {
|
||||
events.off('create', on_create);
|
||||
clearTimeout(timeout);
|
||||
return resolve(Response.json(results, {
|
||||
status: 200,
|
||||
headers
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return Response.json(results, {
|
||||
status: 200,
|
||||
headers
|
||||
});
|
||||
}
|
||||
|
||||
// POST /api/topics/:topic_id/events - Create an event
|
||||
PRECHECKS.POST = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public: boolean = meta.topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || meta.topic.permissions.read.includes(meta.user.id);
|
||||
const topic_events_are_publicly_writable = meta.topic.permissions.write_events.length === 0;
|
||||
const user_has_write_events_for_topic = user_has_read_for_topic &&
|
||||
(topic_events_are_publicly_writable || meta.topic.permissions.write_events.includes(meta.user.id));
|
||||
|
||||
if (!user_has_write_events_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function POST(req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
try {
|
||||
const events: FSDB_COLLECTION<EVENT> = get_events_collection_for_topic(meta.topic.id);
|
||||
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const body = await parse_body(req);
|
||||
const event: EVENT = {
|
||||
type: 'unknown',
|
||||
...body,
|
||||
creator_id: meta.user.id,
|
||||
timestamps: {
|
||||
created: now,
|
||||
updated: now
|
||||
}
|
||||
};
|
||||
|
||||
event.id = `${lurid()}:${event.type}`;
|
||||
|
||||
await events.create(event);
|
||||
|
||||
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 });
|
||||
}
|
||||
}
|
||||
113
public/api/topics/:topic_id/index.ts
Normal file
113
public/api/topics/:topic_id/index.ts
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
import { get_session, get_user, PRECHECK_TABLE, require_user } from '../../../../utils/prechecks.ts';
|
||||
import parse_body from '../../../../utils/bodyparser.ts';
|
||||
import * as CANNED_RESPONSES from '../../../../utils/canned_responses.ts';
|
||||
import { TOPIC, TOPICS } from '../../../../models/topic.ts';
|
||||
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/topics/:id - Get a topic
|
||||
PRECHECKS.GET = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const topic_is_public = topic.permissions.read.length === 0;
|
||||
const user_has_read_for_topic = topic_is_public || topic.permissions.read.includes(meta.user.id);
|
||||
|
||||
if (!user_has_read_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export function GET(_req: Request, meta: Record<string, any>): Response {
|
||||
return Response.json(meta.topic, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
|
||||
// PUT /api/topics/:id - Update topic
|
||||
PRECHECKS.PUT = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const user_has_write_for_topic = topic.permissions.write.includes(meta.user.id);
|
||||
|
||||
if (!user_has_write_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function PUT(req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
try {
|
||||
const body = await parse_body(req);
|
||||
const updated = {
|
||||
...meta.topic,
|
||||
...body,
|
||||
id: meta.topic.id,
|
||||
timestamps: {
|
||||
created: meta.topic.timestamps.created,
|
||||
updated: now
|
||||
}
|
||||
};
|
||||
|
||||
await TOPICS.update(updated);
|
||||
return Response.json(updated, {
|
||||
status: 200
|
||||
});
|
||||
} catch (err) {
|
||||
return Response.json({
|
||||
error: {
|
||||
message: (err as Error)?.message ?? 'Unknown error due to invalid data.',
|
||||
cause: (err as Error)?.cause ?? 'invalid_data'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/topics/:id - Delete topic
|
||||
PRECHECKS.DELETE = [
|
||||
get_session,
|
||||
get_user,
|
||||
require_user,
|
||||
async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const topic_id: string = meta.params?.topic_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const topic: TOPIC | null = topic_id.length === 49 ? await TOPICS.get(topic_id) : null;
|
||||
|
||||
if (!topic) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.topic = topic;
|
||||
const user_has_write_for_topic = topic.permissions.write.includes(meta.user.id);
|
||||
|
||||
if (!user_has_write_for_topic) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}
|
||||
];
|
||||
export async function DELETE(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
await TOPICS.delete(meta.topic);
|
||||
|
||||
return Response.json({
|
||||
deleted: true
|
||||
}, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
28
public/api/topics/README.md
Normal file
28
public/api/topics/README.md
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# /api/topics
|
||||
|
||||
Interact with topics.
|
||||
|
||||
## POST /api/topics
|
||||
|
||||
Create a new topic.
|
||||
|
||||
```
|
||||
export type TOPIC = {
|
||||
id: string; // unique id for this topic
|
||||
name: string; // the name of the topic (max 128 characters)
|
||||
icon_url?: string; // optional url for a topic icon
|
||||
topic?: string; // optional topic topic
|
||||
tags: string[]; // a list of tags for the topic
|
||||
meta: Record<string, any>;
|
||||
limits: {
|
||||
users: number;
|
||||
user_messages_per_minute: number;
|
||||
};
|
||||
creator_id: string; // user_id of the topic creator
|
||||
emojis: Record<string, string>; // either: string: emoji eg: { 'rofl: 🤣, ... } or { 'rofl': 🤣, 'blap': 'https://somewhere.someplace/image.jpg' }
|
||||
};
|
||||
```
|
||||
|
||||
## GET /api/topics
|
||||
|
||||
Get topics.
|
||||
117
public/api/topics/index.ts
Normal file
117
public/api/topics/index.ts
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import lurid from '@andyburke/lurid';
|
||||
import parse_body from '../../../utils/bodyparser.ts';
|
||||
import { get_session, get_user, require_user } from '../../../utils/prechecks.ts';
|
||||
import * as CANNED_RESPONSES from '../../../utils/canned_responses.ts';
|
||||
import { PRECHECK_TABLE } from '../../../utils/prechecks.ts';
|
||||
import { TOPIC, TOPICS } from '../../../models/topic.ts';
|
||||
import { WALK_ENTRY } from '@andyburke/fsdb';
|
||||
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/topics - get topics
|
||||
PRECHECKS.GET = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
|
||||
const can_read_topics = meta.user.permissions.includes('topics.read');
|
||||
|
||||
if (!can_read_topics) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function GET(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
const limit = Math.min(parseInt(meta.query.limit ?? '100'), 100);
|
||||
const topics = (await TOPICS.all({
|
||||
limit,
|
||||
filter: (entry: WALK_ENTRY<TOPIC>) => {
|
||||
// we push our event collections into the topics, and fsdb
|
||||
// doesn't yet filter that out in its all() logic
|
||||
return entry.path.indexOf('/events/') === -1;
|
||||
}
|
||||
})).map((topic_entry) => topic_entry.load());
|
||||
|
||||
return Response.json(topics, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
|
||||
// POST /api/topics - Create a topic
|
||||
PRECHECKS.POST = [get_session, get_user, require_user, (_req: Request, meta: Record<string, any>): Response | undefined => {
|
||||
const can_create_topics = meta.user.permissions.includes('topics.create');
|
||||
|
||||
if (!can_create_topics) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export async function POST(req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
try {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const body = await parse_body(req);
|
||||
|
||||
if (typeof body.name !== 'string' || body.name.length === 0) {
|
||||
return Response.json({
|
||||
error: {
|
||||
cause: 'missing_topic_name',
|
||||
message: 'You must specify a unique name for a topic.'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
if (body.name.length > 64) {
|
||||
return Response.json({
|
||||
error: {
|
||||
cause: 'invalid_topic_name',
|
||||
message: 'topic names must be 64 characters or fewer.'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
const normalized_name = body.name.toLowerCase();
|
||||
|
||||
const existing_topic = (await TOPICS.find({
|
||||
name: normalized_name
|
||||
})).shift();
|
||||
if (existing_topic) {
|
||||
return Response.json({
|
||||
error: {
|
||||
cause: 'topic_name_conflict',
|
||||
message: 'There is already a topic with this name.'
|
||||
}
|
||||
}, {
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
const topic: TOPIC = {
|
||||
...body,
|
||||
id: lurid(),
|
||||
creator_id: meta.user.id,
|
||||
permissions: {
|
||||
read: (body.permissions?.read ?? []),
|
||||
write: (body.permissions?.write ?? [meta.user.id]),
|
||||
read_events: (body.permissions?.read_events ?? []),
|
||||
write_events: (body.permissions?.write_events ?? [])
|
||||
},
|
||||
timestamps: {
|
||||
created: now,
|
||||
updated: now,
|
||||
archived: undefined
|
||||
}
|
||||
};
|
||||
|
||||
await TOPICS.create(topic);
|
||||
|
||||
return Response.json(topic, {
|
||||
status: 201
|
||||
});
|
||||
} catch (error) {
|
||||
return Response.json({
|
||||
error: {
|
||||
message: (error as Error).message ?? 'Unknown Error!',
|
||||
cause: (error as Error).cause ?? 'unknown'
|
||||
}
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue