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, 'all')
74 const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
75 if (!entry) return { created: false }
77 const [ comment, created ] = await VideoCommentModel.findOrCreate({
83 comment.Account = actor.Account
84 comment.Video = videoInstance
86 return { comment, created }
89 async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
90 // Already have this comment?
91 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
92 if (commentFromDatabase) {
93 let parentComments = comments.concat([ commentFromDatabase ])
95 // Speed up things and resolve directly the thread
96 if (commentFromDatabase.InReplyToVideoComment) {
97 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
99 parentComments = parentComments.concat(data)
102 return resolveThread(commentFromDatabase.Video.url, parentComments)
106 // Maybe it's a reply to a video?
107 // If yes, it's done: we resolved all the thread
108 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
110 if (comments.length !== 0) {
111 const firstReply = comments[ comments.length - 1 ]
112 firstReply.inReplyToCommentId = null
113 firstReply.originCommentId = null
114 firstReply.videoId = video.id
115 comments[comments.length - 1] = await firstReply.save()
117 for (let i = comments.length - 2; i >= 0; i--) {
118 const comment = comments[ i ]
119 comment.originCommentId = firstReply.id
120 comment.inReplyToCommentId = comments[ i + 1 ].id
121 comment.videoId = video.id
123 comments[i] = await comment.save()
127 return { video, parents: comments }
129 logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
131 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
132 throw new Error('Recursion limit reached when resolving a thread')
135 const { body } = await doRequest({
141 if (sanitizeAndCheckVideoCommentObject(body) === false) {
142 throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body))
145 const actorUrl = body.attributedTo
146 if (!actorUrl) throw new Error('Miss attributed to in comment')
148 if (checkUrlsSameHost(url, actorUrl) !== true) {
149 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
152 if (checkUrlsSameHost(body.id, url) !== true) {
153 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
156 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
157 const comment = new VideoCommentModel({
161 accountId: actor.Account.id,
162 inReplyToCommentId: null,
163 originCommentId: null,
164 createdAt: new Date(body.published),
165 updatedAt: new Date(body.updated)
168 return resolveThread(body.inReplyTo, comments.concat([ comment ]))
174 videoCommentActivityObjectToDBAttributes,