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')
85 parentComments = parentComments.concat(data)
88 return resolveThread(commentFromDatabase.Video.url, parentComments)
92 // Maybe it's a reply to a video?
93 const { video } = await getOrCreateAccountAndVideoAndChannel(url)
95 if (comments.length !== 0) {
96 const firstReply = comments[ comments.length - 1 ]
97 firstReply.inReplyToCommentId = null
98 firstReply.originCommentId = null
99 firstReply.videoId = video.id
100 comments[comments.length - 1] = await firstReply.save()
102 for (let i = comments.length - 2; i >= 0; i--) {
103 const comment = comments[ i ]
104 comment.originCommentId = firstReply.id
105 comment.inReplyToCommentId = comments[ i + 1 ].id
106 comment.videoId = video.id
108 comments[i] = await comment.save()
112 return { video, parents: comments }
114 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, err)
116 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
117 throw new Error('Recursion limit reached when resolving a thread')
120 const { body } = await doRequest({
126 if (isVideoCommentObjectValid(body) === false) {
127 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
130 const actorUrl = body.attributedTo
131 if (!actorUrl) throw new Error('Miss attributed to in comment')
133 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
134 const comment = new VideoCommentModel({
138 accountId: actor.Account.id,
139 inReplyToCommentId: null,
140 originCommentId: null,
141 createdAt: new Date(body.published),
142 updatedAt: new Date(body.updated)
145 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
151 videoCommentActivityObjectToDBAttributes,