( data: InputTypeCreateComment, )
| 131 | return segments; |
| 132 | }; |
| 133 | const createCommentHandler = async ( |
| 134 | data: InputTypeCreateComment, |
| 135 | ): Promise<ReturnTypeCreateComment> => { |
| 136 | const session = await getServerSession(authOptions); |
| 137 | |
| 138 | if (!session || !session.user) { |
| 139 | return { error: 'Unauthorized or insufficient permissions' }; |
| 140 | } |
| 141 | |
| 142 | const { content, contentId, parentId } = data; |
| 143 | const userId = session.user.id; |
| 144 | |
| 145 | if (!rateLimit(userId)) { |
| 146 | return { error: 'Rate limit exceeded. Please try again later.' }; |
| 147 | } |
| 148 | |
| 149 | try { |
| 150 | // Check if the parent comment exists and is a top-level comment |
| 151 | // Only top-level comments can have replies like youtube comments otherwise it would be a thread |
| 152 | let parentComment; |
| 153 | if (parentId) { |
| 154 | parentComment = await prisma.comment.findUnique({ |
| 155 | where: { id: parentId }, |
| 156 | }); |
| 157 | |
| 158 | if (!parentComment) { |
| 159 | return { error: 'Parent comment not found.' }; |
| 160 | } |
| 161 | |
| 162 | if (parentComment.parentId) { |
| 163 | return { error: 'Cannot reply to a nested comment.' }; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Check if the related content exists and is not a folder |
| 168 | // We only allow comments on videos and Notion pages |
| 169 | const relatedContent = await prisma.content.findUnique({ |
| 170 | where: { id: contentId }, |
| 171 | }); |
| 172 | |
| 173 | if (!relatedContent || relatedContent.type === 'folder') { |
| 174 | return { error: 'Invalid content for commenting.' }; |
| 175 | } |
| 176 | let comment; |
| 177 | if (parentComment) { |
| 178 | await prisma.$transaction(async (prisma) => { |
| 179 | comment = await prisma.comment.create({ |
| 180 | data: { |
| 181 | content, |
| 182 | contentId, |
| 183 | parentId, // undefined if its a comment without parent (top level) |
| 184 | userId, |
| 185 | }, |
| 186 | }); |
| 187 | await prisma.comment.update({ |
| 188 | where: { id: parentId }, |
| 189 | data: { |
| 190 | repliesCount: { increment: 1 }, |
nothing calls this directly
no test coverage detected