feat: bunch of work done here, got comments and edit and delete

This commit is contained in:
2026-02-04 13:00:16 -08:00
parent b6d66d34cb
commit 318f3bc500
19 changed files with 1868 additions and 117 deletions
+4 -4
View File
@@ -22,7 +22,7 @@ export class BookService {
return books.map((book) => ({
...book,
status: book.status.toLowerCase() as BookStatus,
status: book.status as unknown as BookStatus,
dateAdded: book.dateAdded,
dateFinished: book.dateFinished || undefined,
createdAt: book.createdAt,
@@ -42,7 +42,7 @@ export class BookService {
return {
...book,
status: book.status.toLowerCase() as BookStatus,
status: book.status as unknown as BookStatus,
dateAdded: book.dateAdded,
dateFinished: book.dateFinished || undefined,
createdAt: book.createdAt,
@@ -63,7 +63,7 @@ export class BookService {
return {
...book,
status: book.status.toLowerCase() as BookStatus,
status: book.status as unknown as BookStatus,
dateAdded: book.dateAdded,
dateFinished: book.dateFinished || undefined,
createdAt: book.createdAt,
@@ -87,7 +87,7 @@ export class BookService {
return {
...book,
status: book.status.toLowerCase() as BookStatus,
status: book.status as unknown as BookStatus,
dateAdded: book.dateAdded,
dateFinished: book.dateFinished || undefined,
createdAt: book.createdAt,
+154
View File
@@ -0,0 +1,154 @@
/**
* @copyright 2026 NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { Comment, CreateCommentDto } from "@library/shared-types";
import { prisma } from "../lib/prisma";
import createDOMPurify from "dompurify";
import { JSDOM } from "jsdom";
import { marked } from "marked";
const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);
// Add hook to sanitise links - prevent javascript: URLs and add security attributes
DOMPurify.addHook("afterSanitizeAttributes", (node) => {
if (node.tagName === "A") {
const href = node.getAttribute("href") || "";
// Block javascript:, data:, and vbscript: URLs
if (/^(javascript|data|vbscript):/i.test(href)) {
node.removeAttribute("href");
} else {
// Add security attributes to external links
node.setAttribute("target", "_blank");
node.setAttribute("rel", "noopener noreferrer nofollow");
}
}
});
export class CommentService {
private prisma = prisma;
constructor() {}
private sanitizeMarkdown(content: string): string {
const html = marked.parse(content, { async: false }) as string;
return DOMPurify.sanitize(html, {
ALLOWED_TAGS: [
"p", "br", "strong", "em", "b", "i", "u", "s", "strike",
"h1", "h2", "h3", "h4", "h5", "h6",
"ul", "ol", "li",
"blockquote", "code", "pre",
"a", "hr",
],
ALLOWED_ATTR: ["href", "target", "rel"],
ALLOW_DATA_ATTR: false,
ADD_ATTR: ["target", "rel"],
FORCE_BODY: true,
});
}
private mapComment(comment: any): Comment {
return {
id: comment.id,
content: comment.content,
userId: comment.userId,
user: {
id: comment.user.id,
username: comment.user.username,
avatar: comment.user.avatar || undefined,
},
gameId: comment.gameId || undefined,
bookId: comment.bookId || undefined,
musicId: comment.musicId || undefined,
createdAt: comment.createdAt,
updatedAt: comment.updatedAt,
};
}
async getCommentsForGame(gameId: string): Promise<Comment[]> {
const comments = await this.prisma.comment.findMany({
where: { gameId },
include: { user: true },
orderBy: { createdAt: "desc" },
});
return comments.map((c) => this.mapComment(c));
}
async getCommentsForBook(bookId: string): Promise<Comment[]> {
const comments = await this.prisma.comment.findMany({
where: { bookId },
include: { user: true },
orderBy: { createdAt: "desc" },
});
return comments.map((c) => this.mapComment(c));
}
async getCommentsForMusic(musicId: string): Promise<Comment[]> {
const comments = await this.prisma.comment.findMany({
where: { musicId },
include: { user: true },
orderBy: { createdAt: "desc" },
});
return comments.map((c) => this.mapComment(c));
}
async createCommentForGame(
gameId: string,
userId: string,
data: CreateCommentDto
): Promise<Comment> {
const sanitizedContent = this.sanitizeMarkdown(data.content);
const comment = await this.prisma.comment.create({
data: {
content: sanitizedContent,
userId,
gameId,
},
include: { user: true },
});
return this.mapComment(comment);
}
async createCommentForBook(
bookId: string,
userId: string,
data: CreateCommentDto
): Promise<Comment> {
const sanitizedContent = this.sanitizeMarkdown(data.content);
const comment = await this.prisma.comment.create({
data: {
content: sanitizedContent,
userId,
bookId,
},
include: { user: true },
});
return this.mapComment(comment);
}
async createCommentForMusic(
musicId: string,
userId: string,
data: CreateCommentDto
): Promise<Comment> {
const sanitizedContent = this.sanitizeMarkdown(data.content);
const comment = await this.prisma.comment.create({
data: {
content: sanitizedContent,
userId,
musicId,
},
include: { user: true },
});
return this.mapComment(comment);
}
async deleteComment(commentId: string): Promise<void> {
await this.prisma.comment.delete({
where: { id: commentId },
});
}
}
+4 -4
View File
@@ -22,7 +22,7 @@ export class GameService {
return games.map((game) => ({
...game,
status: game.status.toLowerCase() as GameStatus,
status: game.status as unknown as GameStatus,
dateAdded: game.dateAdded,
dateCompleted: game.dateCompleted || undefined,
createdAt: game.createdAt,
@@ -42,7 +42,7 @@ export class GameService {
return {
...game,
status: game.status.toLowerCase() as GameStatus,
status: game.status as unknown as GameStatus,
dateAdded: game.dateAdded,
dateCompleted: game.dateCompleted || undefined,
createdAt: game.createdAt,
@@ -63,7 +63,7 @@ export class GameService {
return {
...game,
status: game.status.toLowerCase() as GameStatus,
status: game.status as unknown as GameStatus,
dateAdded: game.dateAdded,
dateCompleted: game.dateCompleted || undefined,
createdAt: game.createdAt,
@@ -87,7 +87,7 @@ export class GameService {
return {
...game,
status: game.status.toLowerCase() as GameStatus,
status: game.status as unknown as GameStatus,
dateAdded: game.dateAdded,
dateCompleted: game.dateCompleted || undefined,
createdAt: game.createdAt,
+8 -8
View File
@@ -22,8 +22,8 @@ export class MusicService {
return musicItems.map((music) => ({
...music,
type: music.type.toLowerCase() as MusicType,
status: music.status.toLowerCase() as MusicStatus,
type: music.type as unknown as MusicType,
status: music.status as unknown as MusicStatus,
dateAdded: music.dateAdded,
dateCompleted: music.dateCompleted || undefined,
createdAt: music.createdAt,
@@ -43,8 +43,8 @@ export class MusicService {
return {
...music,
type: music.type.toLowerCase() as MusicType,
status: music.status.toLowerCase() as MusicStatus,
type: music.type as unknown as MusicType,
status: music.status as unknown as MusicStatus,
dateAdded: music.dateAdded,
dateCompleted: music.dateCompleted || undefined,
createdAt: music.createdAt,
@@ -66,8 +66,8 @@ export class MusicService {
return {
...music,
type: music.type.toLowerCase() as MusicType,
status: music.status.toLowerCase() as MusicStatus,
type: music.type as unknown as MusicType,
status: music.status as unknown as MusicStatus,
dateAdded: music.dateAdded,
dateCompleted: music.dateCompleted || undefined,
createdAt: music.createdAt,
@@ -94,8 +94,8 @@ export class MusicService {
return {
...music,
type: music.type.toLowerCase() as MusicType,
status: music.status.toLowerCase() as MusicStatus,
type: music.type as unknown as MusicType,
status: music.status as unknown as MusicStatus,
dateAdded: music.dateAdded,
dateCompleted: music.dateCompleted || undefined,
createdAt: music.createdAt,