]>
Commit | Line | Data |
---|---|---|
5cf13500 | 1 | import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' |
2ccaeeb3 C |
2 | import { logger } from '../../helpers/logger' |
3 | import { doRequest } from '../../helpers/requests' | |
74dc3bca | 4 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' |
2ccaeeb3 C |
5 | import { VideoCommentModel } from '../../models/video/video-comment' |
6 | import { getOrCreateActorAndServerAndModel } from './actor' | |
1297eb5d | 7 | import { getOrCreateVideoAndAccountAndChannel } from './videos' |
f6eebcb3 | 8 | import * as Bluebird from 'bluebird' |
5c6d985f | 9 | import { checkUrlsSameHost } from '../../helpers/activitypub' |
26d6bf65 | 10 | import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' |
2ccaeeb3 | 11 | |
6b9c966f | 12 | type ResolveThreadParams = { |
a1587156 C |
13 | url: string |
14 | comments?: MCommentOwner[] | |
15 | isVideo?: boolean | |
6b9c966f | 16 | commentCreated?: boolean |
2ccaeeb3 | 17 | } |
0283eaac | 18 | type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> |
2ccaeeb3 | 19 | |
6b9c966f | 20 | async function addVideoComments (commentUrls: string[]) { |
f6eebcb3 | 21 | return Bluebird.map(commentUrls, commentUrl => { |
6b9c966f | 22 | return resolveThread({ url: commentUrl, isVideo: false }) |
f6eebcb3 | 23 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) |
2ccaeeb3 C |
24 | } |
25 | ||
6b9c966f C |
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 = [] | |
2ccaeeb3 | 30 | |
a1587156 | 31 | // Already have this comment? |
403c69c5 | 32 | if (isVideo === false) { |
6b9c966f C |
33 | const result = await resolveCommentFromDB(params) |
34 | if (result) return result | |
2ccaeeb3 C |
35 | } |
36 | ||
6b9c966f | 37 | try { |
403c69c5 | 38 | if (isVideo === true) return tryResolveThreadFromVideo(params) |
6b9c966f C |
39 | } catch (err) { |
40 | logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err }) | |
5c6d985f | 41 | } |
403c69c5 C |
42 | |
43 | return resolveParentComment(params) | |
6b9c966f | 44 | } |
5c6d985f | 45 | |
6b9c966f C |
46 | export { |
47 | addVideoComments, | |
48 | resolveThread | |
49 | } | |
2ccaeeb3 | 50 | |
6b9c966f | 51 | // --------------------------------------------------------------------------- |
83e6519b | 52 | |
6b9c966f C |
53 | async function resolveCommentFromDB (params: ResolveThreadParams) { |
54 | const { url, comments, commentCreated } = params | |
2ccaeeb3 | 55 | |
6b9c966f | 56 | const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url) |
2ccaeeb3 C |
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') | |
2ccaeeb3 C |
63 | |
64 | parentComments = parentComments.concat(data) | |
65 | } | |
66 | ||
6b9c966f C |
67 | return resolveThread({ |
68 | url: commentFromDatabase.Video.url, | |
69 | comments: parentComments, | |
70 | isVideo: true, | |
71 | commentCreated | |
72 | }) | |
2ccaeeb3 C |
73 | } |
74 | ||
6b9c966f C |
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 | ||
403c69c5 C |
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 | ||
453e83ea | 90 | let resultComment: MCommentOwnerVideo |
6b9c966f | 91 | if (comments.length !== 0) { |
a1587156 | 92 | const firstReply = comments[comments.length - 1] as MCommentOwnerVideo |
6b9c966f C |
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--) { | |
a1587156 | 102 | const comment = comments[i] as MCommentOwnerVideo |
6b9c966f | 103 | comment.originCommentId = firstReply.id |
a1587156 | 104 | comment.inReplyToCommentId = comments[i + 1].id |
6b9c966f C |
105 | comment.videoId = video.id |
106 | comment.changed('updatedAt', true) | |
107 | comment.Video = video | |
108 | ||
109 | comments[i] = await comment.save() | |
2ccaeeb3 C |
110 | } |
111 | ||
453e83ea | 112 | resultComment = comments[0] as MCommentOwnerVideo |
6b9c966f | 113 | } |
2ccaeeb3 | 114 | |
6b9c966f C |
115 | return { video, comment: resultComment, commentCreated } |
116 | } | |
2ccaeeb3 | 117 | |
6b9c966f C |
118 | async function resolveParentComment (params: ResolveThreadParams) { |
119 | const { url, comments } = params | |
2ccaeeb3 | 120 | |
6b9c966f C |
121 | if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { |
122 | throw new Error('Recursion limit reached when resolving a thread') | |
123 | } | |
2ccaeeb3 | 124 | |
366caf8b | 125 | const { body } = await doRequest<any>({ |
6b9c966f C |
126 | uri: url, |
127 | json: true, | |
128 | activityPub: true | |
129 | }) | |
2ccaeeb3 | 130 | |
6b9c966f C |
131 | if (sanitizeAndCheckVideoCommentObject(body) === false) { |
132 | throw new Error('Remote video comment JSON is not valid:' + JSON.stringify(body)) | |
133 | } | |
5c6d985f | 134 | |
6b9c966f | 135 | const actorUrl = body.attributedTo |
b5206dfc | 136 | if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment') |
5c6d985f | 137 | |
b5206dfc | 138 | if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) { |
6b9c966f C |
139 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`) |
140 | } | |
2ccaeeb3 | 141 | |
6b9c966f C |
142 | if (checkUrlsSameHost(body.id, url) !== true) { |
143 | throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`) | |
2ccaeeb3 | 144 | } |
2ccaeeb3 | 145 | |
c883db6d C |
146 | const actor = actorUrl |
147 | ? await getOrCreateActorAndServerAndModel(actorUrl, 'all') | |
148 | : null | |
149 | ||
6b9c966f C |
150 | const comment = new VideoCommentModel({ |
151 | url: body.id, | |
b5206dfc | 152 | text: body.content ? body.content : '', |
6b9c966f | 153 | videoId: null, |
b5206dfc | 154 | accountId: actor ? actor.Account.id : null, |
6b9c966f C |
155 | inReplyToCommentId: null, |
156 | originCommentId: null, | |
157 | createdAt: new Date(body.published), | |
b5206dfc JM |
158 | updatedAt: new Date(body.updated), |
159 | deletedAt: body.deleted ? new Date(body.deleted) : null | |
453e83ea | 160 | }) as MCommentOwner |
b5206dfc | 161 | comment.Account = actor ? actor.Account : null |
6b9c966f C |
162 | |
163 | return resolveThread({ | |
164 | url: body.inReplyTo, | |
165 | comments: comments.concat([ comment ]), | |
166 | commentCreated: true | |
167 | }) | |
2ccaeeb3 | 168 | } |