1 import { cloneDeep } from 'lodash'
2 import * as Sequelize from 'sequelize'
3 import express from 'express'
4 import { logger } from '@server/helpers/logger'
5 import { sequelizeTypescript } from '@server/initializers/database'
6 import { ResultList } from '../../shared/models'
7 import { VideoCommentThreadTree } from '../../shared/models/videos/comment/video-comment.model'
8 import { VideoCommentModel } from '../models/video/video-comment'
9 import { MAccountDefault, MComment, MCommentOwnerVideo, MCommentOwnerVideoReply, MVideoFullLight } from '../types/models'
10 import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/send'
11 import { getLocalVideoCommentActivityPubUrl } from './activitypub/url'
12 import { Hooks } from './plugins/hooks'
14 async function removeComment (videoCommentInstance: MCommentOwnerVideo, req: express.Request, res: express.Response) {
15 const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
17 await sequelizeTypescript.transaction(async t => {
18 if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
19 await sendDeleteVideoComment(videoCommentInstance, t)
22 videoCommentInstance.markAsDeleted()
24 await videoCommentInstance.save({ transaction: t })
27 logger.info('Video comment %d deleted.', videoCommentInstance.id)
29 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore, req, res })
32 async function createVideoComment (obj: {
34 inReplyToComment: MComment | null
35 video: MVideoFullLight
36 account: MAccountDefault
37 }, t: Sequelize.Transaction) {
38 let originCommentId: number | null = null
39 let inReplyToCommentId: number | null = null
41 if (obj.inReplyToComment && obj.inReplyToComment !== null) {
42 originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
43 inReplyToCommentId = obj.inReplyToComment.id
46 const comment = await VideoCommentModel.create({
50 videoId: obj.video.id,
51 accountId: obj.account.id,
52 url: new Date().toISOString()
53 }, { transaction: t, validate: false })
55 comment.url = getLocalVideoCommentActivityPubUrl(obj.video, comment)
57 const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction: t })
58 savedComment.InReplyToVideoComment = obj.inReplyToComment
59 savedComment.Video = obj.video
60 savedComment.Account = obj.account
62 await sendCreateVideoComment(savedComment, t)
67 function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
68 // Comments are sorted by id ASC
69 const comments = resultList.data
71 const comment = comments.shift()
72 const thread: VideoCommentThreadTree = {
73 comment: comment.toFormattedJSON(),
80 while (comments.length !== 0) {
81 const childComment = comments.shift()
83 const childCommentThread: VideoCommentThreadTree = {
84 comment: childComment.toFormattedJSON(),
88 const parentCommentThread = idx[childComment.inReplyToCommentId]
89 // Maybe the parent comment was blocked by the admin/user
90 if (!parentCommentThread) continue
92 parentCommentThread.children.push(childCommentThread)
93 idx[childComment.id] = childCommentThread
99 // ---------------------------------------------------------------------------
104 buildFormattedCommentTree