refactor: zones => topics
This commit is contained in:
parent
20a5d1bc88
commit
fac8f409f4
26 changed files with 470 additions and 469 deletions
|
|
@ -1,113 +0,0 @@
|
|||
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 { ZONE, ZONES } from '../../../../models/zone.ts';
|
||||
|
||||
export const PRECHECKS: PRECHECK_TABLE = {};
|
||||
|
||||
// GET /api/zones/:id - Get a zone
|
||||
PRECHECKS.GET = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const zone_id: string = meta.params?.zone_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const zone: ZONE | null = zone_id.length === 49 ? await ZONES.get(zone_id) : null;
|
||||
|
||||
if (!zone) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.zone = zone;
|
||||
const zone_is_public = zone.permissions.read.length === 0;
|
||||
const user_has_read_for_zone = zone_is_public || zone.permissions.read.includes(meta.user.id);
|
||||
|
||||
if (!user_has_read_for_zone) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}];
|
||||
export function GET(_req: Request, meta: Record<string, any>): Response {
|
||||
return Response.json(meta.zone, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
|
||||
// PUT /api/zones/:id - Update zone
|
||||
PRECHECKS.PUT = [get_session, get_user, require_user, async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const zone_id: string = meta.params?.zone_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const zone: ZONE | null = zone_id.length === 49 ? await ZONES.get(zone_id) : null;
|
||||
|
||||
if (!zone) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.zone = zone;
|
||||
const user_has_write_for_zone = zone.permissions.write.includes(meta.user.id);
|
||||
|
||||
if (!user_has_write_for_zone) {
|
||||
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.zone,
|
||||
...body,
|
||||
id: meta.zone.id,
|
||||
timestamps: {
|
||||
created: meta.zone.timestamps.created,
|
||||
updated: now
|
||||
}
|
||||
};
|
||||
|
||||
await ZONES.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/zones/:id - Delete zone
|
||||
PRECHECKS.DELETE = [
|
||||
get_session,
|
||||
get_user,
|
||||
require_user,
|
||||
async (_req: Request, meta: Record<string, any>): Promise<Response | undefined> => {
|
||||
const zone_id: string = meta.params?.zone_id?.toLowerCase().trim() ?? '';
|
||||
|
||||
// lurid is 49 chars as we use them, eg: "also-play-flow-want-form-wide-thus-work-burn-same"
|
||||
const zone: ZONE | null = zone_id.length === 49 ? await ZONES.get(zone_id) : null;
|
||||
|
||||
if (!zone) {
|
||||
return CANNED_RESPONSES.not_found();
|
||||
}
|
||||
|
||||
meta.zone = zone;
|
||||
const user_has_write_for_zone = zone.permissions.write.includes(meta.user.id);
|
||||
|
||||
if (!user_has_write_for_zone) {
|
||||
return CANNED_RESPONSES.permission_denied();
|
||||
}
|
||||
}
|
||||
];
|
||||
export async function DELETE(_req: Request, meta: Record<string, any>): Promise<Response> {
|
||||
await ZONES.delete(meta.zone);
|
||||
|
||||
return Response.json({
|
||||
deleted: true
|
||||
}, {
|
||||
status: 200
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue