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