113 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| 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
 | |
| 	});
 | |
| }
 |