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