v2: global player, infinite scroll, image picker, threaded comments
This commit is contained in:
77
api/routes/comments.ts
Normal file
77
api/routes/comments.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Router } from "@oak/oak";
|
||||
import {
|
||||
APIErrorCode,
|
||||
APIException,
|
||||
type APIResponse,
|
||||
type Comment,
|
||||
isCreateCommentRequest,
|
||||
} from "../model/interfaces.ts";
|
||||
import { authMiddleware } from "../middleware/auth.ts";
|
||||
import { verifyJWT } from "../lib/jwt.ts";
|
||||
import {
|
||||
createComment,
|
||||
deleteComment,
|
||||
getComments,
|
||||
} from "../services/comment-service.ts";
|
||||
import { getDump } from "../services/dump-service.ts";
|
||||
import {
|
||||
broadcastCommentCreated,
|
||||
broadcastCommentDeleted,
|
||||
} from "../services/ws-service.ts";
|
||||
|
||||
const router = new Router({ prefix: "/api" });
|
||||
|
||||
// GET /api/dumps/:dumpId/comments — optional auth (to access private dump comments)
|
||||
router.get("/dumps/:dumpId/comments", async (ctx) => {
|
||||
let requestingUserId: string | undefined;
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (authHeader?.startsWith("Bearer ")) {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
const dump = getDump(ctx.params.dumpId, requestingUserId);
|
||||
const comments = getComments(dump.id);
|
||||
const responseBody: APIResponse<Comment[]> = { success: true, data: comments };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
// POST /api/dumps/:dumpId/comments — auth required
|
||||
router.post("/dumps/:dumpId/comments", authMiddleware, async (ctx) => {
|
||||
const userId = ctx.state.user.userId as string;
|
||||
const isAdmin = (ctx.state.user.isAdmin ?? false) as boolean;
|
||||
const dump = getDump(ctx.params.dumpId, userId);
|
||||
const body = await ctx.request.body.json();
|
||||
if (!isCreateCommentRequest(body)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"Invalid comment data",
|
||||
);
|
||||
}
|
||||
const comment = createComment(
|
||||
dump.id,
|
||||
userId,
|
||||
body.body,
|
||||
body.parentId ?? undefined,
|
||||
);
|
||||
if (!dump.isPrivate) broadcastCommentCreated(comment);
|
||||
const responseBody: APIResponse<Comment> = { success: true, data: comment };
|
||||
ctx.response.status = 201;
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
// DELETE /api/comments/:commentId — auth required
|
||||
router.delete("/comments/:commentId", authMiddleware, (ctx) => {
|
||||
const userId = ctx.state.user.userId as string;
|
||||
const isAdmin = (ctx.state.user.isAdmin ?? false) as boolean;
|
||||
const { dumpId, isPrivate } = deleteComment(
|
||||
ctx.params.commentId,
|
||||
userId,
|
||||
isAdmin,
|
||||
);
|
||||
if (!isPrivate) broadcastCommentDeleted(ctx.params.commentId, dumpId);
|
||||
const responseBody: APIResponse<null> = { success: true, data: null };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -7,15 +7,18 @@ import {
|
||||
type Dump,
|
||||
isCreateUrlDumpRequest,
|
||||
isUpdateDumpRequest,
|
||||
type PaginatedData,
|
||||
} from "../model/interfaces.ts";
|
||||
|
||||
import { authMiddleware } from "../middleware/auth.ts";
|
||||
import { verifyJWT } from "../lib/jwt.ts";
|
||||
import {
|
||||
createFileDump,
|
||||
createUrlDump,
|
||||
deleteDump,
|
||||
getDump,
|
||||
listDumps,
|
||||
refreshDumpMetadata,
|
||||
replaceFileDump,
|
||||
updateDump,
|
||||
} from "../services/dump-service.ts";
|
||||
@@ -35,6 +38,7 @@ router.post(
|
||||
const formData = await ctx.request.body.formData();
|
||||
const file = formData.get("file");
|
||||
const comment = formData.get("comment");
|
||||
const isPrivate = formData.get("isPrivate") === "true";
|
||||
|
||||
if (!(file instanceof File)) {
|
||||
throw new APIException(
|
||||
@@ -48,6 +52,7 @@ router.post(
|
||||
file,
|
||||
typeof comment === "string" && comment ? comment : undefined,
|
||||
userId,
|
||||
isPrivate,
|
||||
);
|
||||
} else {
|
||||
const body = await ctx.request.body.json();
|
||||
@@ -69,15 +74,32 @@ router.post(
|
||||
},
|
||||
);
|
||||
|
||||
router.get("/:dumpId", (ctx) => {
|
||||
const dump = getDump(ctx.params.dumpId);
|
||||
router.get("/:dumpId", async (ctx) => {
|
||||
let requestingUserId: string | undefined;
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (authHeader?.startsWith("Bearer ")) {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
const dump = getDump(ctx.params.dumpId, requestingUserId);
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: dump };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.get("/", (ctx) => {
|
||||
const dumps = listDumps();
|
||||
const responseBody: APIResponse<Dump[]> = { success: true, data: dumps };
|
||||
router.get("/", async (ctx) => {
|
||||
let requestingUserId: string | undefined;
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (authHeader?.startsWith("Bearer ")) {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
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 } = listDumps(page, limit, requestingUserId);
|
||||
const responseBody: APIResponse<PaginatedData<Dump>> = {
|
||||
success: true,
|
||||
data: { items, total, hasMore: page * limit < total },
|
||||
};
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
@@ -85,7 +107,7 @@ router.put("/:dumpId/file", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
|
||||
const dump = getDump(dumpId);
|
||||
const dump = getDump(dumpId, userId);
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
APIErrorCode.UNAUTHORIZED,
|
||||
@@ -128,7 +150,7 @@ router.put("/:dumpId", authMiddleware, async (ctx) => {
|
||||
);
|
||||
}
|
||||
|
||||
const dump = getDump(dumpId);
|
||||
const dump = getDump(dumpId, userId);
|
||||
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
@@ -143,10 +165,28 @@ router.put("/:dumpId", authMiddleware, async (ctx) => {
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.post("/:dumpId/refresh-metadata", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
const dump = getDump(dumpId, userId);
|
||||
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
APIErrorCode.UNAUTHORIZED,
|
||||
401,
|
||||
"Not authorized to update dump",
|
||||
);
|
||||
}
|
||||
|
||||
const updatedDump = await refreshDumpMetadata(dumpId);
|
||||
const responseBody: APIResponse<Dump> = { success: true, data: updatedDump };
|
||||
ctx.response.body = responseBody;
|
||||
});
|
||||
|
||||
router.delete("/:dumpId", authMiddleware, async (ctx) => {
|
||||
const dumpId = ctx.params.dumpId;
|
||||
const userId = ctx.state.user?.userId;
|
||||
const dump = getDump(dumpId);
|
||||
const dump = getDump(dumpId, userId);
|
||||
|
||||
if (userId !== dump.userId) {
|
||||
throw new APIException(
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
APIException,
|
||||
isLoginUserRequest,
|
||||
isRegisterUserRequest,
|
||||
type PaginatedData,
|
||||
} from "../model/interfaces.ts";
|
||||
|
||||
import { createJWT, verifyJWT, verifyPassword } from "../lib/jwt.ts";
|
||||
@@ -150,8 +151,13 @@ router.get("/:username/playlists", async (ctx) => {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
const playlists = listPlaylistsByUser(user.id, requestingUserId);
|
||||
ctx.response.body = { success: true, data: playlists };
|
||||
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 } = listPlaylistsByUser(user.id, requestingUserId, page, limit);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: { items, total, hasMore: page * limit < total } satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
// Public user profile by username (no passwordHash)
|
||||
@@ -161,18 +167,41 @@ router.get("/:username", (ctx) => {
|
||||
ctx.response.body = { success: true, data: publicUser };
|
||||
});
|
||||
|
||||
// Dumps posted by user
|
||||
router.get("/:username/dumps", (ctx) => {
|
||||
// Dumps posted by user (optional auth: owner sees their private dumps)
|
||||
router.get("/:username/dumps", async (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
const dumps = getDumpsByUser(user.id);
|
||||
ctx.response.body = { success: true, data: dumps };
|
||||
let requestingUserId: string | null = null;
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (authHeader?.startsWith("Bearer ")) {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
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 includePrivate = requestingUserId === user.id;
|
||||
const { items, total } = getDumpsByUser(user.id, page, limit, includePrivate);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: { items, total, hasMore: page * limit < total } satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
// Dumps upvoted by user
|
||||
router.get("/:username/votes", (ctx) => {
|
||||
// Dumps upvoted by user (optional auth: hide private dump entries for non-owners)
|
||||
router.get("/:username/votes", async (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
const dumps = getVotedDumpsByUser(user.id);
|
||||
ctx.response.body = { success: true, data: dumps };
|
||||
let requestingUserId: string | null = null;
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (authHeader?.startsWith("Bearer ")) {
|
||||
const payload = await verifyJWT(authHeader.substring(7));
|
||||
if (payload) requestingUserId = payload.userId;
|
||||
}
|
||||
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 } = getVotedDumpsByUser(user.id, page, limit, requestingUserId);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: { items, total, hasMore: page * limit < total } satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user