]>
Commit | Line | Data |
---|---|---|
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 { getOrCreateActorAndServerAndModel } from './actor' | |
10 | import { getOrCreateAPVideo } from './videos' | |
11 | ||
12 | type ResolveThreadParams = { | |
13 | url: string | |
14 | comments?: MCommentOwner[] | |
15 | isVideo?: boolean | |
16 | commentCreated?: boolean | |
17 | } | |
18 | type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> | |
19 | ||
20 | async function addVideoComments (commentUrls: string[]) { | |
21 | return Bluebird.map(commentUrls, async commentUrl => { | |
22 | try { | |
23 | await resolveThread({ url: commentUrl, isVideo: false }) | |
24 | } catch (err) { | |
25 | logger.warn('Cannot resolve thread %s.', commentUrl, { err }) | |
26 | } | |
27 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
28 | } | |
29 | ||
30 | async function resolveThread (params: ResolveThreadParams): ResolveThreadResult { | |
31 | const { url, isVideo } = params | |
32 | ||
33 | if (params.commentCreated === undefined) params.commentCreated = false | |
34 | if (params.comments === undefined) params.comments = [] | |
35 | ||
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 | |
40 | } | |
41 | ||
42 | try { | |
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) | |
47 | } | |
48 | } catch (err) { | |
49 | logger.debug('Cannot resolve thread from video %s, maybe because it was not a video', url, { err }) | |
50 | } | |
51 | ||
52 | return resolveRemoteParentComment(params) | |
53 | } | |
54 | ||
55 | export { | |
56 | addVideoComments, | |
57 | resolveThread | |
58 | } | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | async function resolveCommentFromDB (params: ResolveThreadParams) { | |
63 | const { url, comments, commentCreated } = params | |
64 | ||
65 | const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url) | |
66 | if (!commentFromDatabase) return undefined | |
67 | ||
68 | let parentComments = comments.concat([ commentFromDatabase ]) | |
69 | ||
70 | // Speed up things and resolve directly the thread | |
71 | if (commentFromDatabase.InReplyToVideoComment) { | |
72 | const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC') | |
73 | ||
74 | parentComments = parentComments.concat(data) | |
75 | } | |
76 | ||
77 | return resolveThread({ | |
78 | url: commentFromDatabase.Video.url, | |
79 | comments: parentComments, | |
80 | isVideo: true, | |
81 | commentCreated | |
82 | }) | |
83 | } | |
84 | ||
85 | async function tryToResolveThreadFromVideo (params: ResolveThreadParams) { | |
86 | const { url, comments, commentCreated } = params | |
87 | ||
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 }) | |
92 | ||
93 | if (video.isOwned() && !video.hasPrivacyForFederation()) { | |
94 | throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation') | |
95 | } | |
96 | ||
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 | |
105 | ||
106 | comments[comments.length - 1] = await firstReply.save() | |
107 | ||
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 | |
115 | ||
116 | comments[i] = await comment.save() | |
117 | } | |
118 | ||
119 | resultComment = comments[0] as MCommentOwnerVideo | |
120 | } | |
121 | ||
122 | return { video, comment: resultComment, commentCreated } | |
123 | } | |
124 | ||
125 | async function resolveRemoteParentComment (params: ResolveThreadParams) { | |
126 | const { url, comments } = params | |
127 | ||
128 | if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { | |
129 | throw new Error('Recursion limit reached when resolving a thread') | |
130 | } | |
131 | ||
132 | const { body } = await doJSONRequest<any>(url, { activityPub: true }) | |
133 | ||
134 | if (sanitizeAndCheckVideoCommentObject(body) === false) { | |
135 | throw new Error(`Remote video comment JSON ${url} is not valid:` + JSON.stringify(body)) | |
136 | } | |
137 | ||
138 | const actorUrl = body.attributedTo | |
139 | if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment') | |
140 | ||
141 | if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) { | |
142 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`) | |
143 | } | |
144 | ||
145 | if (checkUrlsSameHost(body.id, url) !== true) { | |
146 | throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`) | |
147 | } | |
148 | ||
149 | const actor = actorUrl | |
150 | ? await getOrCreateActorAndServerAndModel(actorUrl, 'all') | |
151 | : null | |
152 | ||
153 | const comment = new VideoCommentModel({ | |
154 | url: body.id, | |
155 | text: body.content ? body.content : '', | |
156 | videoId: null, | |
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 | |
163 | }) as MCommentOwner | |
164 | comment.Account = actor ? actor.Account : null | |
165 | ||
166 | return resolveThread({ | |
167 | url: body.inReplyTo, | |
168 | comments: comments.concat([ comment ]), | |
169 | commentCreated: true | |
170 | }) | |
171 | } |