]>
Commit | Line | Data |
---|---|---|
2ccaeeb3 | 1 | import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' |
5cf13500 | 2 | import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' |
2ccaeeb3 C |
3 | import { logger } from '../../helpers/logger' |
4 | import { doRequest } from '../../helpers/requests' | |
f6eebcb3 | 5 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers' |
2ccaeeb3 C |
6 | import { ActorModel } from '../../models/activitypub/actor' |
7 | import { VideoModel } from '../../models/video/video' | |
8 | import { VideoCommentModel } from '../../models/video/video-comment' | |
9 | import { getOrCreateActorAndServerAndModel } from './actor' | |
1297eb5d | 10 | import { getOrCreateVideoAndAccountAndChannel } from './videos' |
f6eebcb3 | 11 | import * as Bluebird from 'bluebird' |
5c6d985f | 12 | import { checkUrlsSameHost } from '../../helpers/activitypub' |
2ccaeeb3 C |
13 | |
14 | async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) { | |
15 | let originCommentId: number = null | |
16 | let inReplyToCommentId: number = null | |
17 | ||
18 | // If this is not a reply to the video (thread), create or get the parent comment | |
19 | if (video.url !== comment.inReplyTo) { | |
83e6519b | 20 | const { comment: parent } = await addVideoComment(video, comment.inReplyTo) |
2ccaeeb3 C |
21 | if (!parent) { |
22 | logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) | |
23 | return undefined | |
24 | } | |
25 | ||
26 | originCommentId = parent.originCommentId || parent.id | |
27 | inReplyToCommentId = parent.id | |
28 | } | |
29 | ||
30 | return { | |
47d0b3ee | 31 | url: comment.id, |
2ccaeeb3 C |
32 | text: comment.content, |
33 | videoId: video.id, | |
34 | accountId: actor.Account.id, | |
35 | inReplyToCommentId, | |
36 | originCommentId, | |
37 | createdAt: new Date(comment.published), | |
38 | updatedAt: new Date(comment.updated) | |
39 | } | |
40 | } | |
41 | ||
8fffe21a | 42 | async function addVideoComments (commentUrls: string[], instance: VideoModel) { |
f6eebcb3 C |
43 | return Bluebird.map(commentUrls, commentUrl => { |
44 | return addVideoComment(instance, commentUrl) | |
45 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
2ccaeeb3 C |
46 | } |
47 | ||
48 | async function addVideoComment (videoInstance: VideoModel, commentUrl: string) { | |
49 | logger.info('Fetching remote video comment %s.', commentUrl) | |
50 | ||
51 | const { body } = await doRequest({ | |
52 | uri: commentUrl, | |
53 | json: true, | |
54 | activityPub: true | |
55 | }) | |
56 | ||
5cf13500 | 57 | if (sanitizeAndCheckVideoCommentObject(body) === false) { |
2ccaeeb3 | 58 | logger.debug('Remote video comment JSON is not valid.', { body }) |
83e6519b | 59 | return { created: false } |
2ccaeeb3 C |
60 | } |
61 | ||
62 | const actorUrl = body.attributedTo | |
83e6519b | 63 | if (!actorUrl) return { created: false } |
2ccaeeb3 | 64 | |
5c6d985f C |
65 | if (checkUrlsSameHost(commentUrl, actorUrl) !== true) { |
66 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`) | |
67 | } | |
68 | ||
69 | if (checkUrlsSameHost(body.id, commentUrl) !== true) { | |
70 | throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`) | |
71 | } | |
72 | ||
cef534ed | 73 | const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all') |
2ccaeeb3 | 74 | const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body) |
83e6519b | 75 | if (!entry) return { created: false } |
2ccaeeb3 | 76 | |
83e6519b | 77 | const [ comment, created ] = await VideoCommentModel.findOrCreate({ |
2ccaeeb3 C |
78 | where: { |
79 | url: body.id | |
80 | }, | |
81 | defaults: entry | |
82 | }) | |
cef534ed C |
83 | comment.Account = actor.Account |
84 | comment.Video = videoInstance | |
83e6519b C |
85 | |
86 | return { comment, created } | |
2ccaeeb3 C |
87 | } |
88 | ||
89 | async function resolveThread (url: string, comments: VideoCommentModel[] = []) { | |
90 | // Already have this comment? | |
91 | const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url) | |
92 | if (commentFromDatabase) { | |
93 | let parentComments = comments.concat([ commentFromDatabase ]) | |
94 | ||
95 | // Speed up things and resolve directly the thread | |
96 | if (commentFromDatabase.InReplyToVideoComment) { | |
97 | const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC') | |
2ccaeeb3 C |
98 | |
99 | parentComments = parentComments.concat(data) | |
100 | } | |
101 | ||
102 | return resolveThread(commentFromDatabase.Video.url, parentComments) | |
103 | } | |
104 | ||
105 | try { | |
106 | // Maybe it's a reply to a video? | |
83e6519b | 107 | // If yes, it's done: we resolved all the thread |
4157cdb1 | 108 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url }) |
2ccaeeb3 C |
109 | |
110 | if (comments.length !== 0) { | |
111 | const firstReply = comments[ comments.length - 1 ] | |
112 | firstReply.inReplyToCommentId = null | |
113 | firstReply.originCommentId = null | |
114 | firstReply.videoId = video.id | |
115 | comments[comments.length - 1] = await firstReply.save() | |
116 | ||
117 | for (let i = comments.length - 2; i >= 0; i--) { | |
118 | const comment = comments[ i ] | |
119 | comment.originCommentId = firstReply.id | |
120 | comment.inReplyToCommentId = comments[ i + 1 ].id | |
121 | comment.videoId = video.id | |
122 | ||
123 | comments[i] = await comment.save() | |
124 | } | |
125 | } | |
126 | ||
127 | return { video, parents: comments } | |
128 | } catch (err) { | |
d5b7d911 | 129 | logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err }) |
2ccaeeb3 C |
130 | |
131 | if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) { | |
132 | throw new Error('Recursion limit reached when resolving a thread') | |
133 | } | |
134 | ||
135 | const { body } = await doRequest({ | |
136 | uri: url, | |
137 | json: true, | |
138 | activityPub: true | |
139 | }) | |
140 | ||
5cf13500 | 141 | if (sanitizeAndCheckVideoCommentObject(body) === false) { |
2ccaeeb3 C |
142 | throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body)) |
143 | } | |
144 | ||
145 | const actorUrl = body.attributedTo | |
146 | if (!actorUrl) throw new Error('Miss attributed to in comment') | |
147 | ||
5c6d985f C |
148 | if (checkUrlsSameHost(url, actorUrl) !== true) { |
149 | throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`) | |
150 | } | |
151 | ||
152 | if (checkUrlsSameHost(body.id, url) !== true) { | |
153 | throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`) | |
154 | } | |
155 | ||
2ccaeeb3 C |
156 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) |
157 | const comment = new VideoCommentModel({ | |
8578e3b5 | 158 | url: body.id, |
2ccaeeb3 C |
159 | text: body.content, |
160 | videoId: null, | |
161 | accountId: actor.Account.id, | |
162 | inReplyToCommentId: null, | |
163 | originCommentId: null, | |
164 | createdAt: new Date(body.published), | |
165 | updatedAt: new Date(body.updated) | |
166 | }) | |
167 | ||
168 | return resolveThread(body.inReplyTo, comments.concat([ comment ])) | |
169 | } | |
170 | ||
171 | } | |
172 | ||
173 | export { | |
174 | videoCommentActivityObjectToDBAttributes, | |
175 | addVideoComments, | |
176 | addVideoComment, | |
177 | resolveThread | |
178 | } |