| 28 | import { ROLES } from '../types'; |
| 29 | |
| 30 | export const getComments = async ( |
| 31 | q: Prisma.CommentFindManyArgs, |
| 32 | parentId: number | null | undefined, |
| 33 | ) => { |
| 34 | let parentComment = null; |
| 35 | if (parentId) { |
| 36 | parentComment = await prisma.comment.findUnique({ |
| 37 | where: { id: parseInt(parentId.toString(), 10) }, |
| 38 | include: { |
| 39 | user: true, |
| 40 | }, |
| 41 | }); |
| 42 | } |
| 43 | if (!parentComment) { |
| 44 | delete q.where?.parentId; |
| 45 | } |
| 46 | const pinnedComment = await prisma.comment.findFirst({ |
| 47 | where: { |
| 48 | contentId: q.where?.contentId, |
| 49 | isPinned: true, |
| 50 | ...(parentId ? { parentId: parseInt(parentId.toString(), 10) } : {}), |
| 51 | }, |
| 52 | include: q.include, |
| 53 | }); |
| 54 | if (pinnedComment) { |
| 55 | q.where = { |
| 56 | ...q.where, |
| 57 | NOT: { |
| 58 | id: pinnedComment.id, |
| 59 | }, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | const comments = await prisma.comment.findMany(q); |
| 64 | const combinedComments = pinnedComment |
| 65 | ? [pinnedComment, ...comments] |
| 66 | : comments; |
| 67 | |
| 68 | return { |
| 69 | comments: combinedComments, |
| 70 | parentComment, |
| 71 | }; |
| 72 | }; |
| 73 | const parseIntroComment = (comment: string) => { |
| 74 | const introPattern = /^intro:\s*([\s\S]*)$/i; |
| 75 | const match = comment.match(introPattern); |