]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import { map } from 'bluebird' |
5cf13500 | 2 | import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' |
2ccaeeb3 | 3 | import { logger } from '../../helpers/logger' |
db4b15f2 | 4 | import { doJSONRequest } from '../../helpers/requests' |
74dc3bca | 5 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
2ccaeeb3 | 6 | import { VideoCommentModel } from '../../models/video/video-comment' |
b2a70e3c C |
7 | import { MComment, MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' |
8 | import { isRemoteVideoCommentAccepted } from '../moderation' | |
9 | import { Hooks } from '../plugins/hooks' | |
136d7efd | 10 | import { getOrCreateAPActor } from './actors' |
7e98a7df | 11 | import { checkUrlsSameHost } from './url' |
304a84d5 | 12 | import { getOrCreateAPVideo } from './videos' |
2ccaeeb3 | 13 | |
6b9c966f | 14 | type ResolveThreadParams = { |
a1587156 C |
15 | url: string |
16 | comments?: MCommentOwner[] | |
17 | isVideo?: boolean | |
6b9c966f | 18 | commentCreated?: boolean |
2ccaeeb3 | 19 | } |
0283eaac | 20 | type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> |
2ccaeeb3 | 21 | |
6b9c966f | 22 | async function addVideoComments (commentUrls: string[]) { |
41fb13c3 | 23 | return map(commentUrls, async commentUrl => { |
266131e0 C |
24 | try { |
25 | await resolveThread({ url: commentUrl, isVideo: false }) | |
26 | } catch (err) { | |
27 | logger.warn('Cannot resolve thread %s.', commentUrl, { err }) | |
28 | } | |
f6eebcb3 | 29 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) |
2ccaeeb3 C |
30 | } |
31 | ||
6b9c966f C |
32 | async function resolveThread (params: ResolveThreadParams): ResolveThreadResult { |
33 | const { url, isVideo } = params | |
49af5ac8 | 34 | |
6b9c966f C |
35 | if (params.commentCreated === undefined) params.commentCreated = false |
36 | if (params.comments === undefined) params.comments = [] | |
2ccaeeb3 | 37 | |
49af5ac8 | 38 | // If it is not a video, or if we don't know if it's a video, try to get the thread from DB |
10c8b0b7 | 39 | if (isVideo === false || isVideo === undefined) { |
6b9c966f C |
40 | const result = await resolveCommentFromDB(params) |
41 | if (result) return result | |
2ccaeeb3 C |
42 | } |
43 | ||
6b9c966f | 44 | try { |
10c8b0b7 C |
45 | // If it is a video, or if we don't know if it's a video |
46 | if (isVideo === true || isVideo === undefined) { | |
47 | // Keep await so we catch the exception | |
49af5ac8 | 48 | return await tryToResolveThreadFromVideo(params) |
10c8b0b7 | 49 | } |
6b9c966f | 50 | } catch (err) { |
74d249bc | 51 | logger.debug('Cannot resolve thread from video %s, maybe because it was not a video', url, { err }) |
5c6d985f | 52 | } |
403c69c5 | 53 | |
74d249bc | 54 | return resolveRemoteParentComment(params) |
6b9c966f | 55 | } |
5c6d985f | 56 | |
6b9c966f C |
57 | export { |
58 | addVideoComments, | |
59 | resolveThread | |
60 | } | |
2ccaeeb3 | 61 | |
6b9c966f | 62 | // --------------------------------------------------------------------------- |
83e6519b | 63 | |
6b9c966f C |
64 | async function resolveCommentFromDB (params: ResolveThreadParams) { |
65 | const { url, comments, commentCreated } = params | |
2ccaeeb3 | 66 | |
6b9c966f | 67 | const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url) |
49af5ac8 | 68 | if (!commentFromDatabase) return undefined |
2ccaeeb3 | 69 | |
49af5ac8 | 70 | let parentComments = comments.concat([ commentFromDatabase ]) |
2ccaeeb3 | 71 | |
49af5ac8 C |
72 | // Speed up things and resolve directly the thread |
73 | if (commentFromDatabase.InReplyToVideoComment) { | |
74 | const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC') | |
2ccaeeb3 | 75 | |
49af5ac8 | 76 | parentComments = parentComments.concat(data) |
2ccaeeb3 C |
77 | } |
78 | ||
49af5ac8 C |
79 | return resolveThread({ |
80 | url: commentFromDatabase.Video.url, | |
81 | comments: parentComments, | |
82 | isVideo: true, | |
83 | commentCreated | |
84 | }) | |
6b9c966f C |
85 | } |
86 | ||
49af5ac8 | 87 | async function tryToResolveThreadFromVideo (params: ResolveThreadParams) { |
6b9c966f C |
88 | const { url, comments, commentCreated } = params |
89 | ||
90 | // Maybe it's a reply to a video? | |
91 | // If yes, it's done: we resolved all the thread | |
57e4e1c1 | 92 | const syncParam = { rates: true, shares: true, comments: false, thumbnail: true, refreshVideo: false } |
304a84d5 | 93 | const { video } = await getOrCreateAPVideo({ videoObject: url, syncParam }) |
6b9c966f | 94 | |
403c69c5 C |
95 | if (video.isOwned() && !video.hasPrivacyForFederation()) { |
96 | throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation') | |
97 | } | |
98 | ||
453e83ea | 99 | let resultComment: MCommentOwnerVideo |
6b9c966f | 100 | if (comments.length !== 0) { |
a1587156 | 101 | const firstReply = comments[comments.length - 1] as MCommentOwnerVideo |
6b9c966f C |
102 | firstReply.inReplyToCommentId = null |
103 | firstReply.originCommentId = null | |
104 | firstReply.videoId = video.id | |
105 | firstReply.changed('updatedAt', true) | |
106 | firstReply.Video = video | |
107 | ||
b2a70e3c C |
108 | if (await isRemoteCommentAccepted(firstReply) !== true) { |
109 | return undefined | |
110 | } | |
111 | ||
6b9c966f C |
112 | comments[comments.length - 1] = await firstReply.save() |
113 | ||
114 | for (let i = comments.length - 2; i >= 0; i--) { | |
a1587156 | 115 | const comment = comments[i] as MCommentOwnerVideo |
6b9c966f | 116 | comment.originCommentId = firstReply.id |
a1587156 | 117 | comment.inReplyToCommentId = comments[i + 1].id |
6b9c966f C |
118 | comment.videoId = video.id |
119 | comment.changed('updatedAt', true) | |
120 | comment.Video = video | |
121 | ||
b2a70e3c C |
122 | if (await isRemoteCommentAccepted(comment) !== true) { |
123 | return undefined | |
124 | } | |
125 | ||
6b9c966f | 126 | comments[i] = await comment.save() |
2ccaeeb3 C |
127 | } |
128 | ||
453e83ea | 129 | resultComment = comments[0] as MCommentOwnerVideo |
6b9c966f | 130 | } |
2ccaeeb3 | 131 | |
6b9c966f C |
132 | return { video, comment: resultComment, commentCreated } |
133 | } | |
2ccaeeb3 | 134 | |
74d249bc | 135 | async function resolveRemoteParentComment (params: ResolveThreadParams) { |
6b9c966f | 136 | const { url, comments } = params |
2ccaeeb3 | 137 | |
6b9c966f C |
138 | if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { |
139 | throw new Error('Recursion limit reached when resolving a thread') | |
140 | } | |
2ccaeeb3 | 141 | |
db4b15f2 | 142 | const { body } = await doJSONRequest<any>(url, { activityPub: true }) |
2ccaeeb3 | 143 | |
6b9c966f | 144 | if (sanitizeAndCheckVideoCommentObject(body) === false) { |
74d249bc | 145 | throw new Error(`Remote video comment JSON ${url} is not valid:` + JSON.stringify(body)) |
6b9c966f | 146 | } |
5c6d985f | 147 | |
6b9c966f | 148 | const actorUrl = body.attributedTo |
b5206dfc | 149 | if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment') |
5c6d985f | 150 | |
b5206dfc | 151 | if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) { |
6b9c966f C |
152 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`) |
153 | } | |
2ccaeeb3 | 154 | |
6b9c966f C |
155 | if (checkUrlsSameHost(body.id, url) !== true) { |
156 | throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`) | |
2ccaeeb3 | 157 | } |
2ccaeeb3 | 158 | |
c883db6d | 159 | const actor = actorUrl |
136d7efd | 160 | ? await getOrCreateAPActor(actorUrl, 'all') |
c883db6d C |
161 | : null |
162 | ||
6b9c966f C |
163 | const comment = new VideoCommentModel({ |
164 | url: body.id, | |
b5206dfc | 165 | text: body.content ? body.content : '', |
6b9c966f | 166 | videoId: null, |
b5206dfc | 167 | accountId: actor ? actor.Account.id : null, |
6b9c966f C |
168 | inReplyToCommentId: null, |
169 | originCommentId: null, | |
170 | createdAt: new Date(body.published), | |
b5206dfc JM |
171 | updatedAt: new Date(body.updated), |
172 | deletedAt: body.deleted ? new Date(body.deleted) : null | |
453e83ea | 173 | }) as MCommentOwner |
b5206dfc | 174 | comment.Account = actor ? actor.Account : null |
6b9c966f C |
175 | |
176 | return resolveThread({ | |
177 | url: body.inReplyTo, | |
178 | comments: comments.concat([ comment ]), | |
179 | commentCreated: true | |
180 | }) | |
2ccaeeb3 | 181 | } |
b2a70e3c C |
182 | |
183 | async function isRemoteCommentAccepted (comment: MComment) { | |
184 | // Already created | |
185 | if (comment.id) return true | |
186 | ||
187 | const acceptParameters = { | |
188 | comment | |
189 | } | |
190 | ||
191 | const acceptedResult = await Hooks.wrapFun( | |
192 | isRemoteVideoCommentAccepted, | |
193 | acceptParameters, | |
194 | 'filter:activity-pub.remote-video-comment.create.accept.result' | |
195 | ) | |
196 | ||
197 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
198 | logger.info('Refused to create a remote comment.', { acceptedResult, acceptParameters }) | |
199 | ||
200 | return false | |
201 | } | |
202 | ||
203 | return true | |
204 | } |