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