v3: added multiple stylesheets, improved user profiles
This commit is contained in:
@@ -15,6 +15,7 @@ import { parseOptionalAuth } from "../lib/auth.ts";
|
||||
import { parsePagination } from "../lib/pagination.ts";
|
||||
import {
|
||||
createUser,
|
||||
getInviteTree,
|
||||
getUserById,
|
||||
getUserByUsername,
|
||||
searchUsers,
|
||||
@@ -30,7 +31,10 @@ import {
|
||||
getVotedDumpsByUser,
|
||||
} from "../services/dump-service.ts";
|
||||
import { listPlaylistsByUser } from "../services/playlist-service.ts";
|
||||
import { getFollowedPlaylistsByUser } from "../services/follow-service.ts";
|
||||
import {
|
||||
getFollowedPlaylistsByUser,
|
||||
getFollowedUsersByUser,
|
||||
} from "../services/follow-service.ts";
|
||||
|
||||
// Users router
|
||||
const router = new Router({ prefix: "/api/users" });
|
||||
@@ -189,6 +193,21 @@ router.get("/by-id/:userId", (ctx) => {
|
||||
ctx.response.body = { success: true, data: publicUser };
|
||||
});
|
||||
|
||||
// Followed users for a user (public)
|
||||
router.get("/:username/followed-users", (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
const { page, limit } = parsePagination(ctx.request.url.searchParams);
|
||||
const { items, total } = getFollowedUsersByUser(user.id, page, limit);
|
||||
ctx.response.body = {
|
||||
success: true,
|
||||
data: {
|
||||
items: items.map(({ passwordHash: _, email: _e, ...pub }) => pub),
|
||||
total,
|
||||
hasMore: page * limit < total,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
// Followed playlists for a user (public only)
|
||||
router.get("/:username/followed-playlists", (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
@@ -225,6 +244,13 @@ router.get("/:username/playlists", async (ctx) => {
|
||||
};
|
||||
});
|
||||
|
||||
// Invite tree for a user (public)
|
||||
router.get("/:username/invitees", (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
const tree = getInviteTree(user.id);
|
||||
ctx.response.body = { success: true, data: tree };
|
||||
});
|
||||
|
||||
// Public user profile by username (no passwordHash)
|
||||
router.get("/:username", (ctx) => {
|
||||
const user = getUserByUsername(ctx.params.username);
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
type Dump,
|
||||
type FollowStatus,
|
||||
type Playlist,
|
||||
type User,
|
||||
} from "../model/interfaces.ts";
|
||||
import {
|
||||
notifyPlaylistOwnerNewFollower,
|
||||
@@ -15,7 +16,9 @@ import {
|
||||
isDumpRow,
|
||||
isFollowRow,
|
||||
isPlaylistRow,
|
||||
isUserRow,
|
||||
playlistRowToApi,
|
||||
userRowToApi,
|
||||
} from "../model/db.ts";
|
||||
|
||||
// Mirrors dump-service SELECT_COLS_ALIASED — kept local to avoid circular imports
|
||||
@@ -256,3 +259,39 @@ export function getFollowedPlaylistsByUser(
|
||||
}
|
||||
return { items: rawRows.map(playlistRowToApi), total: totalRow?.count ?? 0 };
|
||||
}
|
||||
|
||||
// ── Followed users (as user objects) ─────────────────────────────────────────
|
||||
|
||||
export function getFollowedUsersByUser(
|
||||
userId: string,
|
||||
page: number,
|
||||
limit: number,
|
||||
): { items: User[]; total: number } {
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const totalRow = db.prepare(
|
||||
`SELECT COUNT(*) as count FROM follows WHERE follower_id = ? AND followed_user_id IS NOT NULL;`,
|
||||
).get(userId) as { count: number } | undefined;
|
||||
|
||||
const rawRows = db.prepare(
|
||||
`SELECT u.id, u.username, u.password_hash, u.is_admin, u.created_at, u.updated_at,
|
||||
u.avatar_mime, u.description, u.invited_by, u.email,
|
||||
i.username as invited_by_username
|
||||
FROM users u
|
||||
LEFT JOIN users i ON i.id = u.invited_by
|
||||
INNER JOIN follows f ON f.followed_user_id = u.id
|
||||
WHERE f.follower_id = ?
|
||||
ORDER BY f.created_at DESC
|
||||
LIMIT ? OFFSET ?;`,
|
||||
).all(userId, limit, offset);
|
||||
|
||||
if (!rawRows.every(isUserRow)) {
|
||||
throw new APIException(
|
||||
APIErrorCode.SERVER_ERROR,
|
||||
500,
|
||||
"Malformed user data",
|
||||
);
|
||||
}
|
||||
|
||||
return { items: rawRows.map(userRowToApi), total: totalRow?.count ?? 0 };
|
||||
}
|
||||
|
||||
@@ -178,6 +178,44 @@ export function updateUserAvatar(userId: string, mime: string): void {
|
||||
}
|
||||
}
|
||||
|
||||
export function getInviteTree(
|
||||
userId: string,
|
||||
): {
|
||||
id: string;
|
||||
username: string;
|
||||
avatarMime?: string;
|
||||
invitedById: string;
|
||||
createdAt: Date;
|
||||
}[] {
|
||||
const rows = db.prepare(`
|
||||
WITH RECURSIVE tree AS (
|
||||
SELECT id, username, avatar_mime, invited_by, created_at, 0 AS depth
|
||||
FROM users
|
||||
WHERE invited_by = ?
|
||||
UNION ALL
|
||||
SELECT u.id, u.username, u.avatar_mime, u.invited_by, u.created_at, t.depth + 1
|
||||
FROM users u
|
||||
INNER JOIN tree t ON u.invited_by = t.id
|
||||
WHERE t.depth < 10
|
||||
)
|
||||
SELECT * FROM tree ORDER BY created_at;
|
||||
`).all(userId) as {
|
||||
id: string;
|
||||
username: string;
|
||||
avatar_mime: string | null;
|
||||
invited_by: string;
|
||||
created_at: string;
|
||||
}[];
|
||||
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
username: r.username,
|
||||
avatarMime: r.avatar_mime ?? undefined,
|
||||
invitedById: r.invited_by,
|
||||
createdAt: new Date(r.created_at),
|
||||
}));
|
||||
}
|
||||
|
||||
export function deleteUser(userId: string): void {
|
||||
disconnectUser(userId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user