/** * Serves the figures of the client's `/community` page, which * `community-service.ts` recomputes on a timer, and the community feed. * * - `GET /api/community?period=weekly|monthly` — signed in. Answers the * site's figures over the last week (by default) or month. * - `GET /api/community/feed` — signed in. Answers the latest things done in * public and the week's figures. * * @module */ import { Router } from "@oak/oak"; import type { APIResponse } from "../../shared/api.ts"; import type { CommunityFeed, CommunityStats } from "../../shared/community.ts"; import { isDigestCadence } from "../../shared/settings.ts"; import { APIException } from "../lib/api-exception.ts"; import { type AuthContext, authMiddleware } from "../middleware/auth.ts"; import { getCommunityFeed, getCommunityStats, } from "../services/community-service.ts"; /** * Mounted by `api/main.ts` behind the error, Open Graph and CORS middleware. * An `APIException` thrown by a handler becomes the JSON error response there. */ const router: Router = new Router({ prefix: "/api/community" }); router.get("/", authMiddleware, (ctx: AuthContext) => { const period = ctx.request.url.searchParams.get("period") ?? "weekly"; if (!isDigestCadence(period)) { throw new APIException(400, "period must be weekly or monthly"); } const responseBody: APIResponse = { success: true, data: getCommunityStats(period), }; ctx.response.body = responseBody; }); router.get("/feed", authMiddleware, (ctx: AuthContext) => { const responseBody: APIResponse = { success: true, data: getCommunityFeed(), }; ctx.response.body = responseBody; }); export default router;