117 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 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 });
 | |
| 	}
 | |
| }
 |