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 } from './activitypub'
8 async function createVideoComment (obj: {
10 inReplyToCommentId: number,
13 }, t: Sequelize.Transaction) {
14 let originCommentId: number = null
16 if (obj.inReplyToCommentId) {
17 const repliedComment = await VideoCommentModel.loadById(obj.inReplyToCommentId)
18 if (!repliedComment) throw new Error('Unknown replied comment.')
20 originCommentId = repliedComment.originCommentId || repliedComment.id
23 const comment = await VideoCommentModel.create({
26 inReplyToCommentId: obj.inReplyToCommentId,
27 videoId: obj.video.id,
28 accountId: obj.accountId,
30 }, { transaction: t, validate: false })
32 comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
34 return comment.save({ transaction: t })
37 function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
38 // Comments are sorted by id ASC
39 const comments = resultList.data
41 const comment = comments.shift()
42 const thread: VideoCommentThreadTree = {
43 comment: comment.toFormattedJSON(),
50 while (comments.length !== 0) {
51 const childComment = comments.shift()
53 const childCommentThread: VideoCommentThreadTree = {
54 comment: childComment.toFormattedJSON(),
58 const parentCommentThread = idx[childComment.inReplyToCommentId]
59 if (!parentCommentThread) {
60 const msg = `Cannot format video thread tree, parent ${childComment.inReplyToCommentId} not found for child ${childComment.id}`
64 parentCommentThread.children.push(childCommentThread)
65 idx[childComment.id] = childCommentThread
71 // ---------------------------------------------------------------------------
75 buildFormattedCommentTree