]>
Commit | Line | Data |
---|---|---|
1 | import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' | |
2 | import { logger } from '../../helpers/logger' | |
3 | import { doRequest } from '../../helpers/requests' | |
4 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | |
5 | import { VideoCommentModel } from '../../models/video/video-comment' | |
6 | import { getOrCreateActorAndServerAndModel } from './actor' | |
7 | import { getOrCreateVideoAndAccountAndChannel } from './videos' | |
8 | import * as Bluebird from 'bluebird' | |
9 | import { checkUrlsSameHost } from '../../helpers/activitypub' | |
10 | import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' | |
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, commentUrl => { | |
22 | return resolveThread({ url: commentUrl, isVideo: false }) | |
23 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
24 | } | |
25 | ||
26 | async function resolveThread (params: ResolveThreadParams): ResolveThreadResult { | |
27 | const { url, isVideo } = params | |
28 | if (params.commentCreated === undefined) params.commentCreated = false | |
29 | if (params.comments === undefined) params.comments = [] | |
30 | ||
31 | // Already have this comment? | |
32 | if (isVideo === false) { | |
33 | const result = await resolveCommentFromDB(params) | |
34 | if (result) return result | |
35 | } | |
36 | ||
37 | try { | |
38 | if (isVideo === true) return tryResolveThreadFromVideo(params) | |
39 | } catch (err) { | |
40 | logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err }) | |
41 | } | |
42 | ||
43 | return resolveParentComment(params) | |
44 | } | |
45 | ||
46 | export { | |
47 | addVideoComments, | |
48 | resolveThread | |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | async function resolveCommentFromDB (params: ResolveThreadParams) { | |
54 | const { url, comments, commentCreated } = params | |
55 | ||
56 | const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url) | |
57 | if (commentFromDatabase) { | |
58 | let parentComments = comments.concat([ commentFromDatabase ]) | |
59 | ||
60 | // Speed up things and resolve directly the thread | |
61 | if (commentFromDatabase.InReplyToVideoComment) { | |
62 | const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC') | |
63 | ||
64 | parentComments = parentComments.concat(data) | |
65 | } | |
66 | ||
67 | return resolveThread({ | |
68 | url: commentFromDatabase.Video.url, | |
69 | comments: parentComments, | |
70 | isVideo: true, | |
71 | commentCreated | |
72 | }) | |
73 | } | |
74 | ||
75 | return undefined | |
76 | } | |
77 | ||
78 | async function tryResolveThreadFromVideo (params: ResolveThreadParams) { | |
79 | const { url, comments, commentCreated } = params | |
80 | ||
81 | // Maybe it's a reply to a video? | |
82 | // If yes, it's done: we resolved all the thread | |
83 | const syncParam = { likes: true, dislikes: true, shares: true, comments: false, thumbnail: true, refreshVideo: false } | |
84 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam }) | |
85 | ||
86 | if (video.isOwned() && !video.hasPrivacyForFederation()) { | |
87 | throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation') | |
88 | } | |
89 | ||
90 | let resultComment: MCommentOwnerVideo | |
91 | if (comments.length !== 0) { | |
92 | const firstReply = comments[comments.length - 1] as MCommentOwnerVideo | |
93 | firstReply.inReplyToCommentId = null | |
94 | firstReply.originCommentId = null | |
95 | firstReply.videoId = video.id | |
96 | firstReply.changed('updatedAt', true) | |
97 | firstReply.Video = video | |
98 | ||
99 | comments[comments.length - 1] = await firstReply.save() | |
100 | ||
101 | for (let i = comments.length - 2; i >= 0; i--) { | |
102 | const comment = comments[i] as MCommentOwnerVideo | |
103 | comment.originCommentId = firstReply.id | |
104 | comment.inReplyToCommentId = comments[i + 1].id | |
105 | comment.videoId = video.id | |
106 | comment.changed('updatedAt', true) | |
107 | comment.Video = video | |
108 | ||
109 | comments[i] = await comment.save() | |
110 | } | |
111 | ||
112 | resultComment = comments[0] as MCommentOwnerVideo | |
113 | } | |
114 | ||
115 | return { video, comment: resultComment, commentCreated } | |
116 | } | |
117 | ||
118 | async function resolveParentComment (params: ResolveThreadParams) { | |
119 | const { url, comments } = params | |
120 | ||
121 | if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { | |
122 | throw new Error('Recursion limit reached when resolving a thread') | |
123 | } | |
124 | ||
125 | const { body } = await doRequest<any>({ | |
126 | uri: url, | |
127 | json: true, | |
128 | activityPub: true | |
129 | }) | |
130 | ||
131 | if (sanitizeAndCheckVideoCommentObject(body) === false) { | |
132 | throw new Error('Remote video comment JSON is not valid:' + JSON.stringify(body)) | |
133 | } | |
134 | ||
135 | const actorUrl = body.attributedTo | |
136 | if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment') | |
137 | ||
138 | if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) { | |
139 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`) | |
140 | } | |
141 | ||
142 | if (checkUrlsSameHost(body.id, url) !== true) { | |
143 | throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`) | |
144 | } | |
145 | ||
146 | const actor = actorUrl | |
147 | ? await getOrCreateActorAndServerAndModel(actorUrl, 'all') | |
148 | : null | |
149 | ||
150 | const comment = new VideoCommentModel({ | |
151 | url: body.id, | |
152 | text: body.content ? body.content : '', | |
153 | videoId: null, | |
154 | accountId: actor ? actor.Account.id : null, | |
155 | inReplyToCommentId: null, | |
156 | originCommentId: null, | |
157 | createdAt: new Date(body.published), | |
158 | updatedAt: new Date(body.updated), | |
159 | deletedAt: body.deleted ? new Date(body.deleted) : null | |
160 | }) as MCommentOwner | |
161 | comment.Account = actor ? actor.Account : null | |
162 | ||
163 | return resolveThread({ | |
164 | url: body.inReplyTo, | |
165 | comments: comments.concat([ comment ]), | |
166 | commentCreated: true | |
167 | }) | |
168 | } |