1 import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
2 import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
3 import { logger } from '../../helpers/logger'
4 import { doRequest } from '../../helpers/requests'
5 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } 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 { getOrCreateVideoAndAccountAndChannel } from './videos'
11 import * as Bluebird from 'bluebird'
13 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
14 let originCommentId: number = null
15 let inReplyToCommentId: number = null
17 // If this is not a reply to the video (thread), create or get the parent comment
18 if (video.url !== comment.inReplyTo) {
19 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
21 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
25 originCommentId = parent.originCommentId || parent.id
26 inReplyToCommentId = parent.id
31 text: comment.content,
33 accountId: actor.Account.id,
36 createdAt: new Date(comment.published),
37 updatedAt: new Date(comment.updated)
41 async function addVideoComments (commentUrls: string[], instance: VideoModel) {
42 return Bluebird.map(commentUrls, commentUrl => {
43 return addVideoComment(instance, commentUrl)
44 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
47 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
48 logger.info('Fetching remote video comment %s.', commentUrl)
50 const { body } = await doRequest({
56 if (sanitizeAndCheckVideoCommentObject(body) === false) {
57 logger.debug('Remote video comment JSON is not valid.', { body })
58 return { created: false }
61 const actorUrl = body.attributedTo
62 if (!actorUrl) return { created: false }
64 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
65 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
66 if (!entry) return { created: false }
68 const [ comment, created ] = await VideoCommentModel.findOrCreate({
75 return { comment, created }
78 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
79 // Already have this comment?
80 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
81 if (commentFromDatabase) {
82 let parentComments = comments.concat([ commentFromDatabase ])
84 // Speed up things and resolve directly the thread
85 if (commentFromDatabase.InReplyToVideoComment) {
86 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
88 parentComments = parentComments.concat(data)
91 return resolveThread(commentFromDatabase.Video.url, parentComments)
95 // Maybe it's a reply to a video?
96 // If yes, it's done: we resolved all the thread
97 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
99 if (comments.length !== 0) {
100 const firstReply = comments[ comments.length - 1 ]
101 firstReply.inReplyToCommentId = null
102 firstReply.originCommentId = null
103 firstReply.videoId = video.id
104 comments[comments.length - 1] = await firstReply.save()
106 for (let i = comments.length - 2; i >= 0; i--) {
107 const comment = comments[ i ]
108 comment.originCommentId = firstReply.id
109 comment.inReplyToCommentId = comments[ i + 1 ].id
110 comment.videoId = video.id
112 comments[i] = await comment.save()
116 return { video, parents: comments }
118 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
120 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
121 throw new Error('Recursion limit reached when resolving a thread')
124 const { body } = await doRequest({
130 if (sanitizeAndCheckVideoCommentObject(body) === false) {
131 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
134 const actorUrl = body.attributedTo
135 if (!actorUrl) throw new Error('Miss attributed to in comment')
137 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
138 const comment = new VideoCommentModel({
142 accountId: actor.Account.id,
143 inReplyToCommentId: null,
144 originCommentId: null,
145 createdAt: new Date(body.published),
146 updatedAt: new Date(body.updated)
149 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
155 videoCommentActivityObjectToDBAttributes,