1 import * as Bluebird from 'bluebird'
2 import { checkUrlsSameHost } from '../../helpers/activitypub'
3 import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
4 import { logger } from '../../helpers/logger'
5 import { doJSONRequest } from '../../helpers/requests'
6 import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
7 import { VideoCommentModel } from '../../models/video/video-comment'
8 import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video'
9 import { getOrCreateAPActor } from './actors'
10 import { getOrCreateAPVideo } from './videos'
12 type ResolveThreadParams = {
14 comments?: MCommentOwner[]
16 commentCreated?: boolean
18 type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }>
20 async function addVideoComments (commentUrls: string[]) {
21 return Bluebird.map(commentUrls, async commentUrl => {
23 await resolveThread({ url: commentUrl, isVideo: false })
25 logger.warn('Cannot resolve thread %s.', commentUrl, { err })
27 }, { concurrency: CRAWL_REQUEST_CONCURRENCY })
30 async function resolveThread (params: ResolveThreadParams): ResolveThreadResult {
31 const { url, isVideo } = params
33 if (params.commentCreated === undefined) params.commentCreated = false
34 if (params.comments === undefined) params.comments = []
36 // If it is not a video, or if we don't know if it's a video, try to get the thread from DB
37 if (isVideo === false || isVideo === undefined) {
38 const result = await resolveCommentFromDB(params)
39 if (result) return result
43 // If it is a video, or if we don't know if it's a video
44 if (isVideo === true || isVideo === undefined) {
45 // Keep await so we catch the exception
46 return await tryToResolveThreadFromVideo(params)
49 logger.debug('Cannot resolve thread from video %s, maybe because it was not a video', url, { err })
52 return resolveRemoteParentComment(params)
60 // ---------------------------------------------------------------------------
62 async function resolveCommentFromDB (params: ResolveThreadParams) {
63 const { url, comments, commentCreated } = params
65 const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url)
66 if (!commentFromDatabase) return undefined
68 let parentComments = comments.concat([ commentFromDatabase ])
70 // Speed up things and resolve directly the thread
71 if (commentFromDatabase.InReplyToVideoComment) {
72 const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
74 parentComments = parentComments.concat(data)
77 return resolveThread({
78 url: commentFromDatabase.Video.url,
79 comments: parentComments,
85 async function tryToResolveThreadFromVideo (params: ResolveThreadParams) {
86 const { url, comments, commentCreated } = params
88 // Maybe it's a reply to a video?
89 // If yes, it's done: we resolved all the thread
90 const syncParam = { likes: true, dislikes: true, shares: true, comments: false, thumbnail: true, refreshVideo: false }
91 const { video } = await getOrCreateAPVideo({ videoObject: url, syncParam })
93 if (video.isOwned() && !video.hasPrivacyForFederation()) {
94 throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation')
97 let resultComment: MCommentOwnerVideo
98 if (comments.length !== 0) {
99 const firstReply = comments[comments.length - 1] as MCommentOwnerVideo
100 firstReply.inReplyToCommentId = null
101 firstReply.originCommentId = null
102 firstReply.videoId = video.id
103 firstReply.changed('updatedAt', true)
104 firstReply.Video = video
106 comments[comments.length - 1] = await firstReply.save()
108 for (let i = comments.length - 2; i >= 0; i--) {
109 const comment = comments[i] as MCommentOwnerVideo
110 comment.originCommentId = firstReply.id
111 comment.inReplyToCommentId = comments[i + 1].id
112 comment.videoId = video.id
113 comment.changed('updatedAt', true)
114 comment.Video = video
116 comments[i] = await comment.save()
119 resultComment = comments[0] as MCommentOwnerVideo
122 return { video, comment: resultComment, commentCreated }
125 async function resolveRemoteParentComment (params: ResolveThreadParams) {
126 const { url, comments } = params
128 if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
129 throw new Error('Recursion limit reached when resolving a thread')
132 const { body } = await doJSONRequest<any>(url, { activityPub: true })
134 if (sanitizeAndCheckVideoCommentObject(body) === false) {
135 throw new Error(`Remote video comment JSON ${url} is not valid:` + JSON.stringify(body))
138 const actorUrl = body.attributedTo
139 if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment')
141 if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) {
142 throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
145 if (checkUrlsSameHost(body.id, url) !== true) {
146 throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
149 const actor = actorUrl
150 ? await getOrCreateAPActor(actorUrl, 'all')
153 const comment = new VideoCommentModel({
155 text: body.content ? body.content : '',
157 accountId: actor ? actor.Account.id : null,
158 inReplyToCommentId: null,
159 originCommentId: null,
160 createdAt: new Date(body.published),
161 updatedAt: new Date(body.updated),
162 deletedAt: body.deleted ? new Date(body.deleted) : null
164 comment.Account = actor ? actor.Account : null
166 return resolveThread({
168 comments: comments.concat([ comment ]),