refactor: zones => topics
This commit is contained in:
parent
20a5d1bc88
commit
fac8f409f4
26 changed files with 470 additions and 469 deletions
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
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue