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'
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),
38 updatedAt: new Date(comment.updated)
42 async function addVideoComments (commentUrls: string[], instance: VideoModel) {
43 return Bluebird.map(commentUrls, commentUrl => {
44 return addVideoComment(instance, commentUrl)
45 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
48 async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
49 logger.info('Fetching remote video comment %s.', commentUrl)
51 const { body } = await doRequest({
57 if (sanitizeAndCheckVideoCommentObject(body) === false) {
58 logger.debug('Remote video comment JSON is not valid.', { body })
59 return { created: false }
62 const actorUrl = body.attributedTo
63 if (!actorUrl) return { created: false }
65 if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
66 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
69 if (checkUrlsSameHost(body.id, commentUrl) !== true) {
70 throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
73 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
74 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
75 if (!entry) return { created: false }
77 const [ comment, created ] = await VideoCommentModel.findOrCreate({
84 return { comment, created }
87 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
88 // Already have this comment?
89 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
90 if (commentFromDatabase) {
91 let parentComments = comments.concat([ commentFromDatabase ])
93 // Speed up things and resolve directly the thread
94 if (commentFromDatabase.InReplyToVideoComment) {
95 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
97 parentComments = parentComments.concat(data)
100 return resolveThread(commentFromDatabase.Video.url, parentComments)
104 // Maybe it's a reply to a video?
105 // If yes, it's done: we resolved all the thread
106 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
108 if (comments.length !== 0) {
109 const firstReply = comments[ comments.length - 1 ]
110 firstReply.inReplyToCommentId = null
111 firstReply.originCommentId = null
112 firstReply.videoId = video.id
113 comments[comments.length - 1] = await firstReply.save()
115 for (let i = comments.length - 2; i >= 0; i--) {
116 const comment = comments[ i ]
117 comment.originCommentId = firstReply.id
118 comment.inReplyToCommentId = comments[ i + 1 ].id
119 comment.videoId = video.id
121 comments[i] = await comment.save()
125 return { video, parents: comments }
127 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
129 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
130 throw new Error('Recursion limit reached when resolving a thread')
133 const { body } = await doRequest({
139 if (sanitizeAndCheckVideoCommentObject(body) === false) {
140 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
143 const actorUrl = body.attributedTo
144 if (!actorUrl) throw new Error('Miss attributed to in comment')
146 if (checkUrlsSameHost(url, actorUrl) !== true) {
147 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
150 if (checkUrlsSameHost(body.id, url) !== true) {
151 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
154 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
155 const comment = new VideoCommentModel({
159 accountId: actor.Account.id,
160 inReplyToCommentId: null,
161 originCommentId: null,
162 createdAt: new Date(body.published),
163 updatedAt: new Date(body.updated)
166 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
172 videoCommentActivityObjectToDBAttributes,