1 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2 import { isVideoCommentObjectValid } from '../../helpers/custom-validators/activitypub/video-comments'
3 import { logger } from '../../helpers/logger'
4 import { doRequest } from '../../helpers/requests'
5 import { ACTIVITY_PUB } from '../../initializers'
6 import { ActorModel } from '../../models/activitypub/actor'
7 import { VideoModel } from '../../models/video/video'
8 import { VideoCommentModel } from '../../models/video/video-comment'
9 import { getOrCreateActorAndServerAndModel } from './actor'
10 import { getOrCreateAccountAndVideoAndChannel } from './videos'
12 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
13 let originCommentId: number = null
14 let inReplyToCommentId: number = null
16 // If this is not a reply to the video (thread), create or get the parent comment
17 if (video.url !== comment.inReplyTo) {
18 const [ parent ] = await addVideoComment(video, comment.inReplyTo)
20 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
24 originCommentId = parent.originCommentId || parent.id
25 inReplyToCommentId = parent.id
30 text: comment.content,
32 accountId: actor.Account.id,
35 createdAt: new Date(comment.published),
36 updatedAt: new Date(comment.updated)
40 async function addVideoComments (instance: VideoModel, commentUrls: string[]) {
41 for (const commentUrl of commentUrls) {
42 await addVideoComment(instance, commentUrl)
46 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
47 logger.info('Fetching remote video comment %s.', commentUrl)
49 const { body } = await doRequest({
55 if (isVideoCommentObjectValid(body) === false) {
56 logger.debug('Remote video comment JSON is not valid.', { body })
60 const actorUrl = body.attributedTo
61 if (!actorUrl) return []
63 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
64 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
67 return VideoCommentModel.findOrCreate({
75 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
76 // Already have this comment?
77 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
78 if (commentFromDatabase) {
79 let parentComments = comments.concat([ commentFromDatabase ])
81 // Speed up things and resolve directly the thread
82 if (commentFromDatabase.InReplyToVideoComment) {
83 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
86 parentComments = parentComments.concat(data)
89 return resolveThread(commentFromDatabase.Video.url, parentComments)
93 // Maybe it's a reply to a video?
94 const { video } = await getOrCreateAccountAndVideoAndChannel(url)
96 if (comments.length !== 0) {
97 const firstReply = comments[ comments.length - 1 ]
98 firstReply.inReplyToCommentId = null
99 firstReply.originCommentId = null
100 firstReply.videoId = video.id
101 comments[comments.length - 1] = await firstReply.save()
103 for (let i = comments.length - 2; i >= 0; i--) {
104 const comment = comments[ i ]
105 comment.originCommentId = firstReply.id
106 comment.inReplyToCommentId = comments[ i + 1 ].id
107 comment.videoId = video.id
109 comments[i] = await comment.save()
113 return { video, parents: comments }
115 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, err)
117 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
118 throw new Error('Recursion limit reached when resolving a thread')
121 const { body } = await doRequest({
127 if (isVideoCommentObjectValid(body) === false) {
128 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
131 const actorUrl = body.attributedTo
132 if (!actorUrl) throw new Error('Miss attributed to in comment')
134 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
135 const comment = new VideoCommentModel({
139 accountId: actor.Account.id,
140 inReplyToCommentId: null,
141 originCommentId: null,
142 createdAt: new Date(body.published),
143 updatedAt: new Date(body.updated)
146 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
152 videoCommentActivityObjectToDBAttributes,