/playlists lays its cards out as tiles, the cover across the top, four to a row and two on a phone, on the wide column: the same PlaylistCard, laid out by its list's class (.playlist-grid), so each theme's card skin carries over. It drops the "public" badge there, every playlist listed being public. On every card, a playlist without a cover shows a tile of its own colour with its initial (ThumbnailPlaceholder) in place of the 📋, and the date is "updated …" of its last dump, or "created …". A middle, ctrl, cmd or shift click anywhere on a playlist card opens it in a new tab — only its title was a link, the rest navigating by a click handler. useCardLink (hooks/shared) is useDumpCardLink's handlers for any card, which the dump cards now get through it. A profile tab's query stays with the tab — going from the playlists to the dumps kept the whole query, so a playlist category, and now ?sort=, landed on the dumps' URL. Only the dump lists pass their category to each other; any other change of tab drops the query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
344 lines
12 KiB
TypeScript
344 lines
12 KiB
TypeScript
/**
|
|
* Serves sign-up and user profiles, with what each user posted, upvoted, made,
|
|
* follows and invited, and the dumps and playlists under them. A user's `:key`
|
|
* is their id or `@name`, a current or former name in any case
|
|
* (`resolveUserKey`). A profile sent to anyone goes through `toPublicUser`,
|
|
* which drops the email. The caller's own account is `routes/me.ts`'s.
|
|
*
|
|
* - `POST /api/users` — public, with a valid invite. Creates an account, sends
|
|
* the welcome email, and answers the user and their tokens.
|
|
* - `GET /api/users?q=` — public. Answers up to 8 users whose name starts
|
|
* with `q`, for @mention suggestions.
|
|
* - `GET /api/users/:key` — public. Answers a user's profile.
|
|
* - `GET /api/users/:key/dumps?page=&limit=&category=` — optional sign-in.
|
|
* Answers a page of their dumps, the private ones only to themselves,
|
|
* optionally in one category.
|
|
* - `GET /api/users/:key/dumps/:slug` — optional sign-in. Answers their dump
|
|
* whose slug is or was `slug`, with its current slug; a private one 404s
|
|
* but for its poster.
|
|
* - `GET /api/users/:key/upvoted?page=&limit=&category=` — optional sign-in.
|
|
* Answers a page of the dumps they upvoted, private ones only to
|
|
* themselves, optionally in one category.
|
|
* - `GET /api/users/:key/playlists?page=&limit=&category=&sort=` — optional
|
|
* sign-in. Answers a page of the playlists they made, the private ones only
|
|
* to themselves, optionally in one playlist category, in the order `sort`
|
|
* names, as `GET /api/playlists` does.
|
|
* - `GET /api/users/:key/playlists/:slug` — optional sign-in. Answers their
|
|
* playlist whose slug is or was `slug`, with its current slug and the dumps
|
|
* the caller may see; a private one 404s but for its owner.
|
|
* - `GET /api/users/:key/follow/users?page=&limit=` — public. Answers a page of
|
|
* the users they follow.
|
|
* - `GET /api/users/:key/follow/playlists?page=&limit=` — public. Answers a
|
|
* page of the public playlists they follow.
|
|
* - `GET /api/users/:key/invitees` — public. Answers the tree of users they
|
|
* invited.
|
|
* - `GET /api/users/:key/home?path=` — public. Redirects to a page of the
|
|
* instance a user from another instance comes from, their profile there
|
|
* unless `path` names another; a local user 404s.
|
|
* - `PATCH /api/users/:key/role` — permission `user:manage`. Sets another
|
|
* user's role.
|
|
* - `PATCH /api/users/:key/username` — permission `user:manage`. Renames a
|
|
* user, the caller included. It is the only way a local name changes; a
|
|
* user from another instance is named there (400). The instances the user
|
|
* signed in to are told.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Router } from "@oak/oak";
|
|
|
|
import { type APIResponse, type PaginatedData } from "../../shared/api.ts";
|
|
import { APIException } from "../lib/api-exception.ts";
|
|
import {
|
|
type AuthResponse,
|
|
type Invitee,
|
|
isRole,
|
|
type PublicUser,
|
|
type UserSuggestion,
|
|
} from "../../shared/user.ts";
|
|
import type { Dump } from "../../shared/dump.ts";
|
|
import {
|
|
parsePlaylistSort,
|
|
type Playlist,
|
|
type PlaylistWithDumps,
|
|
} from "../../shared/playlist.ts";
|
|
import {
|
|
fieldOf,
|
|
isRegisterUserRequest,
|
|
validateRegisterUserRequest,
|
|
} from "../validators/user.ts";
|
|
import { toPublicUser, toSelfUser } from "../model/user.ts";
|
|
import { issueTokens } from "../lib/jwt.ts";
|
|
import {
|
|
type AuthContext,
|
|
authMiddleware,
|
|
requirePermission,
|
|
} from "../middleware/auth.ts";
|
|
import { parseOptionalAuth } from "../lib/auth.ts";
|
|
import { parsePagination } from "../lib/pagination.ts";
|
|
import {
|
|
assertLocalUser,
|
|
createUser,
|
|
getInviteTree,
|
|
renameUser,
|
|
resolveUserKey,
|
|
searchUsers,
|
|
sendWelcomeEmail,
|
|
updateUser,
|
|
} from "../services/user-service.ts";
|
|
import { claimInvite, releaseInvite } from "../services/invite-service.ts";
|
|
import { notifyClients } from "../services/identity-provider-service.ts";
|
|
import {
|
|
broadcastUserUpdated,
|
|
updateClientUsername,
|
|
} from "../services/ws-service.ts";
|
|
import {
|
|
getDumpByPath,
|
|
getDumpsByUser,
|
|
getVotedDumpsByUser,
|
|
} from "../services/dump-service.ts";
|
|
import {
|
|
getPlaylist,
|
|
getPlaylistByPath,
|
|
listPlaylistsByUser,
|
|
} from "../services/playlist-service.ts";
|
|
import {
|
|
getFollowedPlaylistsByUser,
|
|
getFollowedUsersByUser,
|
|
} from "../services/follow-service.ts";
|
|
import { resolveCategoryFilter } from "../services/category-service.ts";
|
|
import { homePageUrl } from "../services/federation-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/users" });
|
|
|
|
router.post("/", async (ctx) => {
|
|
const body: unknown = await ctx.request.body.json();
|
|
|
|
if (!isRegisterUserRequest(body)) {
|
|
throw new APIException(
|
|
400,
|
|
validateRegisterUserRequest(body) ?? "Invalid request",
|
|
);
|
|
}
|
|
|
|
// Claimed before the async account creation, so that one invite can't open
|
|
// two accounts, and handed back if the creation fails.
|
|
const inviterId = claimInvite(body.inviteToken);
|
|
|
|
let user;
|
|
try {
|
|
user = await createUser(body, inviterId);
|
|
} catch (err) {
|
|
releaseInvite(body.inviteToken);
|
|
throw err;
|
|
}
|
|
|
|
sendWelcomeEmail(user);
|
|
|
|
const responseBody: APIResponse<AuthResponse> = {
|
|
success: true,
|
|
data: { ...await issueTokens(user), user: toSelfUser(user) },
|
|
};
|
|
ctx.response.status = 201;
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/", (ctx) => {
|
|
const q = (ctx.request.url.searchParams.get("q") ?? "").trim();
|
|
const responseBody: APIResponse<UserSuggestion[]> = {
|
|
success: true,
|
|
data: searchUsers(q, 8),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key", (ctx) => {
|
|
const responseBody: APIResponse<PublicUser> = {
|
|
success: true,
|
|
data: toPublicUser(resolveUserKey(ctx.params.key)),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/dumps", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx);
|
|
const params = ctx.request.url.searchParams;
|
|
const { page, limit } = parsePagination(params);
|
|
const categoryId = resolveCategoryFilter("dump", params);
|
|
const includePrivate = requestingUserId === user.id;
|
|
const { items, hasMore } = categoryId === "missing"
|
|
? { items: [], hasMore: false }
|
|
: getDumpsByUser(user.id, page, limit, includePrivate, categoryId);
|
|
const responseBody: APIResponse<PaginatedData<Dump>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/dumps/:slug", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx) ?? undefined;
|
|
const responseBody: APIResponse<Dump> = {
|
|
success: true,
|
|
data: getDumpByPath(user.id, ctx.params.slug, requestingUserId),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/upvoted", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx);
|
|
const params = ctx.request.url.searchParams;
|
|
const { page, limit } = parsePagination(params);
|
|
const categoryId = resolveCategoryFilter("dump", params);
|
|
const { items, hasMore } = categoryId === "missing"
|
|
? { items: [], hasMore: false }
|
|
: getVotedDumpsByUser(user.id, page, limit, requestingUserId, categoryId);
|
|
const responseBody: APIResponse<PaginatedData<Dump>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/playlists", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx);
|
|
const params = ctx.request.url.searchParams;
|
|
const { page, limit } = parsePagination(params);
|
|
const categoryId = resolveCategoryFilter("playlist", params);
|
|
const { items, hasMore } = categoryId === "missing"
|
|
? { items: [], hasMore: false }
|
|
: listPlaylistsByUser(
|
|
user.id,
|
|
requestingUserId,
|
|
page,
|
|
limit,
|
|
parsePlaylistSort(params.get("sort")),
|
|
categoryId,
|
|
);
|
|
const responseBody: APIResponse<PaginatedData<Playlist>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/playlists/:slug", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx);
|
|
const responseBody: APIResponse<PlaylistWithDumps> = {
|
|
success: true,
|
|
data: getPlaylist(
|
|
getPlaylistByPath(user.id, ctx.params.slug, requestingUserId),
|
|
requestingUserId,
|
|
),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/follow/users", (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
|
const { items, hasMore } = getFollowedUsersByUser(user.id, page, limit);
|
|
const responseBody: APIResponse<PaginatedData<PublicUser>> = {
|
|
success: true,
|
|
data: {
|
|
items: items.map(toPublicUser),
|
|
hasMore,
|
|
},
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/follow/playlists", async (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const requestingUserId = await parseOptionalAuth(ctx);
|
|
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
|
const { items, hasMore } = getFollowedPlaylistsByUser(
|
|
user.id,
|
|
requestingUserId,
|
|
page,
|
|
limit,
|
|
);
|
|
const responseBody: APIResponse<PaginatedData<Playlist>> = {
|
|
success: true,
|
|
data: { items, hasMore },
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/invitees", (ctx) => {
|
|
const user = resolveUserKey(ctx.params.key);
|
|
const responseBody: APIResponse<Invitee[]> = {
|
|
success: true,
|
|
data: getInviteTree(user.id),
|
|
};
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
router.get("/:key/home", async (ctx) => {
|
|
const path = ctx.request.url.searchParams.get("path") ?? undefined;
|
|
ctx.response.redirect(
|
|
await homePageUrl(resolveUserKey(ctx.params.key), path),
|
|
);
|
|
});
|
|
|
|
router.patch(
|
|
"/:key/role",
|
|
authMiddleware,
|
|
requirePermission("user:manage"),
|
|
async (ctx: AuthContext<"/:key/role">) => {
|
|
const body: unknown = await ctx.request.body.json();
|
|
const role = fieldOf(body, "role");
|
|
if (!isRole(role)) {
|
|
throw new APIException(400, "Invalid role");
|
|
}
|
|
const user = resolveUserKey(ctx.params.key);
|
|
// So that an admin can't demote themselves by mistake and leave no admin.
|
|
if (user.id === ctx.state.user.userId) {
|
|
throw new APIException(400, "Cannot change your own role");
|
|
}
|
|
const updated = await updateUser(user.id, { role });
|
|
const publicUser = toPublicUser(updated);
|
|
broadcastUserUpdated(publicUser);
|
|
const responseBody: APIResponse<PublicUser> = {
|
|
success: true,
|
|
data: publicUser,
|
|
};
|
|
ctx.response.body = responseBody;
|
|
},
|
|
);
|
|
|
|
router.patch(
|
|
"/:key/username",
|
|
authMiddleware,
|
|
requirePermission("user:manage"),
|
|
async (ctx: AuthContext<"/:key/username">) => {
|
|
const body: unknown = await ctx.request.body.json();
|
|
const username = fieldOf(body, "username");
|
|
if (typeof username !== "string") {
|
|
throw new APIException(400, "Invalid request");
|
|
}
|
|
const user = resolveUserKey(ctx.params.key);
|
|
assertLocalUser(user);
|
|
const publicUser = toPublicUser(renameUser(user.id, username));
|
|
updateClientUsername(publicUser.id, publicUser.username);
|
|
broadcastUserUpdated(publicUser);
|
|
await notifyClients(publicUser.id, { type: "update" });
|
|
const responseBody: APIResponse<PublicUser> = {
|
|
success: true,
|
|
data: publicUser,
|
|
};
|
|
ctx.response.body = responseBody;
|
|
},
|
|
);
|
|
|
|
export default router;
|