v3: follows, notifications, invite-only registration, unread markers
This commit is contained in:
@@ -15,52 +15,48 @@ import {
|
||||
getUserById,
|
||||
getUserByUsername,
|
||||
} from "../services/user-service.ts";
|
||||
import { redeemInvite, validateInvite } from "../services/invite-service.ts";
|
||||
import {
|
||||
getDumpsByUser,
|
||||
getVotedDumpsByUser,
|
||||
} from "../services/dump-service.ts";
|
||||
import { listPlaylistsByUser } from "../services/playlist-service.ts";
|
||||
import { getFollowedPlaylistsByUser } from "../services/follow-service.ts";
|
||||
|
||||
// Users router
|
||||
const router = new Router({ prefix: "/api/users" });
|
||||
|
||||
// Register a new user
|
||||
// Register a new user (requires a valid invite token)
|
||||
router.post("/register", async (ctx) => {
|
||||
try {
|
||||
const body = await ctx.request.body.json();
|
||||
|
||||
if (!isRegisterUserRequest(body)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"Invalid request",
|
||||
);
|
||||
}
|
||||
|
||||
const user = await createUser(body);
|
||||
const token = await createJWT({
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
isAdmin: user.isAdmin,
|
||||
});
|
||||
|
||||
ctx.response.status = 201;
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: {
|
||||
token,
|
||||
user,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
const body = await ctx.request.body.json();
|
||||
|
||||
if (!isRegisterUserRequest(body)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.SERVER_ERROR,
|
||||
500,
|
||||
"Failed to register user",
|
||||
APIErrorCode.VALIDATION_ERROR,
|
||||
400,
|
||||
"Invalid request",
|
||||
);
|
||||
}
|
||||
|
||||
// Validate invite — throws 404/409 if bad
|
||||
const inviterId = await validateInvite(body.inviteToken);
|
||||
|
||||
const user = await createUser(body, inviterId);
|
||||
|
||||
// Mark invite as used only after the user row is committed
|
||||
redeemInvite(body.inviteToken);
|
||||
|
||||
const authToken = await createJWT({
|
||||
userId: user.id,
|
||||
username: user.username,
|
||||
isAdmin: user.isAdmin,
|
||||
});
|
||||
|
||||
ctx.response.status = 201;
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: { token: authToken, user },
|
||||
};
|
||||
});
|
||||
|
||||
// Login
|
||||
@@ -142,6 +138,31 @@ router.get("/by-id/:userId", (ctx) => {
|
||||
ctx.response.body = { success: true, data: publicUser };
|
||||
});
|
||||
|
||||
// Followed playlists for a user (public only)
|
||||
router.get("/:username/followed-playlists", (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
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 } = getFollowedPlaylistsByUser(user.id, page, limit);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: {
|
||||
items,
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
} satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
// Playlists by user (optional auth: include private only if requester === owner)
|
||||
router.get("/:username/playlists", async (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
@@ -151,12 +172,30 @@ router.get("/:username/playlists", async (ctx) => {
|
||||
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 } = listPlaylistsByUser(user.id, requestingUserId, page, limit);
|
||||
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]>,
|
||||
data: {
|
||||
items,
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
} satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -176,13 +215,26 @@ router.get("/:username/dumps", async (ctx) => {
|
||||
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 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]>,
|
||||
data: {
|
||||
items,
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
} satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -195,12 +247,30 @@ router.get("/:username/votes", async (ctx) => {
|
||||
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);
|
||||
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]>,
|
||||
data: {
|
||||
items,
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
} satisfies PaginatedData<typeof items[number]>,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user