1 import * as Sequelize from 'sequelize'
2 import { ResultList } from '../../shared/models'
3 import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
4 import { VideoModel } from '../models/video/video'
5 import { VideoCommentModel } from '../models/video/video-comment'
6 import { getVideoCommentActivityPubUrl, sendVideoRateChangeToFollowers } from './activitypub'
7 import { sendCreateVideoCommentToOrigin, sendCreateVideoCommentToVideoFollowers } from './activitypub/send'
9 async function createVideoComment (obj: {
11 inReplyToComment: VideoCommentModel,
14 }, t: Sequelize.Transaction) {
15 let originCommentId: number = null
17 if (obj.inReplyToComment) {
18 originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
21 const comment = await VideoCommentModel.create({
24 inReplyToCommentId: obj.inReplyToComment.id,
25 videoId: obj.video.id,
26 accountId: obj.accountId,
28 }, { transaction: t, validate: false })
30 comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
32 const savedComment = await comment.save({ transaction: t })
33 savedComment.InReplyToVideoComment = obj.inReplyToComment
34 savedComment.Video = obj.video
36 if (savedComment.Video.isOwned()) {
37 await sendCreateVideoCommentToVideoFollowers(savedComment, t)
39 await sendCreateVideoCommentToOrigin(savedComment, t)
45 function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
46 // Comments are sorted by id ASC
47 const comments = resultList.data
49 const comment = comments.shift()
50 const thread: VideoCommentThreadTree = {
51 comment: comment.toFormattedJSON(),
58 while (comments.length !== 0) {
59 const childComment = comments.shift()
61 const childCommentThread: VideoCommentThreadTree = {
62 comment: childComment.toFormattedJSON(),
66 const parentCommentThread = idx[childComment.inReplyToCommentId]
67 if (!parentCommentThread) {
68 const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}`
72 parentCommentThread.children.push(childCommentThread)
73 idx[childComment.id] = childCommentThread
79 // ---------------------------------------------------------------------------
83 buildFormattedCommentTree