49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
/**
|
|
* Serves the caller's own notifications to the client's `/notifications`
|
|
* page, which marks them all read when it opens. New ones arrive over the
|
|
* WebSocket instead, as `notification_created`.
|
|
*
|
|
* - `GET /api/me/notifications?page=&limit=` — signed in. Answers a page of
|
|
* them, newest first.
|
|
* - `POST /api/me/notifications/read` — signed in. Marks them all read.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Router } from "@oak/oak";
|
|
import { type APIResponse, type PaginatedData } from "../../shared/api.ts";
|
|
import type { Notification } from "../../shared/notification.ts";
|
|
import { parsePagination } from "../lib/pagination.ts";
|
|
import { type AuthContext, authMiddleware } from "../middleware/auth.ts";
|
|
import {
|
|
getNotificationsForUser,
|
|
markAllRead,
|
|
} from "../services/notification-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/me/notifications" });
|
|
|
|
router.get("/", authMiddleware, (ctx: AuthContext) => {
|
|
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
|
const { items, hasMore } = getNotificationsForUser(
|
|
ctx.state.user.userId,
|
|
page,
|
|
limit,
|
|
);
|
|
const responseBody: APIResponse<PaginatedData<Notification>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.post("/read", authMiddleware, (ctx: AuthContext) => {
|
|
markAllRead(ctx.state.user.userId);
|
|
ctx.response.status = 204;
|
|
});
|
|
|
|
export default router;
|