v3: follows, notifications, invite-only registration, unread markers
This commit is contained in:
67
api/routes/notifications.ts
Normal file
67
api/routes/notifications.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { Router } from "@oak/oak";
|
||||
import {
|
||||
APIErrorCode,
|
||||
APIException,
|
||||
type AuthPayload,
|
||||
type PaginatedData,
|
||||
} from "../model/interfaces.ts";
|
||||
import { type AuthContext, authMiddleware } from "../middleware/auth.ts";
|
||||
import {
|
||||
getNotificationsForUser,
|
||||
markAllRead,
|
||||
markOneRead,
|
||||
} from "../services/notification-service.ts";
|
||||
|
||||
const router = new Router({ prefix: "/api/notifications" });
|
||||
|
||||
// GET /api/notifications?page=N&limit=N
|
||||
router.get("/", authMiddleware, (ctx: AuthContext) => {
|
||||
if (!ctx.state.user) {
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
||||
}
|
||||
const page = Math.max(
|
||||
1,
|
||||
parseInt(ctx.request.url.searchParams.get("page") ?? "1") || 1,
|
||||
);
|
||||
const limit = Math.min(
|
||||
Math.max(
|
||||
1,
|
||||
parseInt(ctx.request.url.searchParams.get("limit") ?? "20") || 20,
|
||||
),
|
||||
100,
|
||||
);
|
||||
const { items, total } = getNotificationsForUser(
|
||||
ctx.state.user.userId,
|
||||
page,
|
||||
limit,
|
||||
);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: {
|
||||
items,
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
} satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
// POST /api/notifications/read-all
|
||||
router.post("/read-all", authMiddleware, (ctx: AuthContext) => {
|
||||
if (!ctx.state.user) {
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
||||
}
|
||||
markAllRead(ctx.state.user.userId);
|
||||
ctx.response.status = 204;
|
||||
});
|
||||
|
||||
// PATCH /api/notifications/:id/read
|
||||
router.patch("/:id/read", authMiddleware, (ctx) => {
|
||||
const user = ctx.state.user as AuthPayload;
|
||||
if (!user) {
|
||||
throw new APIException(APIErrorCode.UNAUTHORIZED, 401, "Not authenticated");
|
||||
}
|
||||
markOneRead(ctx.params.id, user.userId);
|
||||
ctx.response.status = 204;
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user