1 import * as Sequelize from 'sequelize'
2 import { ResultList } from '../../shared/models'
3 import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
4 import { AccountModel } from '../models/account/account'
5 import { VideoModel } from '../models/video/video'
6 import { VideoCommentModel } from '../models/video/video-comment'
7 import { getVideoCommentActivityPubUrl } from './activitypub'
8 import { sendCreateVideoComment } from './activitypub/send'
10 async function createVideoComment (obj: {
12 inReplyToComment: VideoCommentModel | null,
15 }, t: Sequelize.Transaction) {
16 let originCommentId: number | null = null
17 let inReplyToCommentId: number | null = null
19 if (obj.inReplyToComment && obj.inReplyToComment !== null) {
20 originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
21 inReplyToCommentId = obj.inReplyToComment.id
24 const comment = await VideoCommentModel.create({
28 videoId: obj.video.id,
29 accountId: obj.account.id,
31 }, { transaction: t, validate: false })
33 comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
35 const savedComment = await comment.save({ transaction: t })
36 savedComment.InReplyToVideoComment = obj.inReplyToComment
37 savedComment.Video = obj.video
38 savedComment.Account = obj.account
40 await sendCreateVideoComment(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