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/constants'
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'
12 import { checkUrlsSameHost } from '../../helpers/activitypub'
14 async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
15 let originCommentId: number = null
16 let inReplyToCommentId: number = null
18 // If this is not a reply to the video (thread), create or get the parent comment
19 if (video.url !== comment.inReplyTo) {
20 const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
22 logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
26 originCommentId = parent.originCommentId || parent.id
27 inReplyToCommentId = parent.id
32 text: comment.content,
34 accountId: actor.Account.id,
37 createdAt: new Date(comment.published)
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 if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
65 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
68 if (checkUrlsSameHost(body.id, commentUrl) !== true) {
69 throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
72 const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
73 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
74 if (!entry) return { created: false }
76 const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true })
77 comment.Account = actor.Account
78 comment.Video = videoInstance
80 return { comment, created }
83 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
84 // Already have this comment?
85 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
86 if (commentFromDatabase) {
87 let parentComments = comments.concat([ commentFromDatabase ])
89 // Speed up things and resolve directly the thread
90 if (commentFromDatabase.InReplyToVideoComment) {
91 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
93 parentComments = parentComments.concat(data)
96 return resolveThread(commentFromDatabase.Video.url, parentComments)
100 // Maybe it's a reply to a video?
101 // If yes, it's done: we resolved all the thread
102 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
104 if (comments.length !== 0) {
105 const firstReply = comments[ comments.length - 1 ]
106 firstReply.inReplyToCommentId = null
107 firstReply.originCommentId = null
108 firstReply.videoId = video.id
109 comments[comments.length - 1] = await firstReply.save()
111 for (let i = comments.length - 2; i >= 0; i--) {
112 const comment = comments[ i ]
113 comment.originCommentId = firstReply.id
114 comment.inReplyToCommentId = comments[ i + 1 ].id
115 comment.videoId = video.id
117 comments[i] = await comment.save()
121 return { video, parents: comments }
123 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
125 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
126 throw new Error('Recursion limit reached when resolving a thread')
129 const { body } = await doRequest({
135 if (sanitizeAndCheckVideoCommentObject(body) === false) {
136 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
139 const actorUrl = body.attributedTo
140 if (!actorUrl) throw new Error('Miss attributed to in comment')
142 if (checkUrlsSameHost(url, actorUrl) !== true) {
143 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
146 if (checkUrlsSameHost(body.id, url) !== true) {
147 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
150 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
151 const comment = new VideoCommentModel({
155 accountId: actor.Account.id,
156 inReplyToCommentId: null,
157 originCommentId: null,
158 createdAt: new Date(body.published),
159 updatedAt: new Date(body.updated)
162 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
168 videoCommentActivityObjectToDBAttributes,