]>
Commit | Line | Data |
---|---|---|
1 | import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object' | |
2 | import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' | |
3 | import { logger } from '../../helpers/logger' | |
4 | import { doRequest } from '../../helpers/requests' | |
5 | import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' | |
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' | |
10 | import { getOrCreateVideoAndAccountAndChannel } from './videos' | |
11 | import * as Bluebird from 'bluebird' | |
12 | import { checkUrlsSameHost } from '../../helpers/activitypub' | |
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) { | |
20 | const { comment: parent } = await addVideoComment(video, comment.inReplyTo) | |
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 { | |
31 | url: comment.id, | |
32 | text: comment.content, | |
33 | videoId: video.id, | |
34 | accountId: actor.Account.id, | |
35 | inReplyToCommentId, | |
36 | originCommentId, | |
37 | createdAt: new Date(comment.published) | |
38 | } | |
39 | } | |
40 | ||
41 | async function addVideoComments (commentUrls: string[], instance: VideoModel) { | |
42 | return Bluebird.map(commentUrls, commentUrl => { | |
43 | return addVideoComment(instance, commentUrl) | |
44 | }, { concurrency: CRAWL_REQUEST_CONCURRENCY }) | |
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 | ||
56 | if (sanitizeAndCheckVideoCommentObject(body) === false) { | |
57 | logger.debug('Remote video comment JSON is not valid.', { body }) | |
58 | return { created: false } | |
59 | } | |
60 | ||
61 | const actorUrl = body.attributedTo | |
62 | if (!actorUrl) return { created: false } | |
63 | ||
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 | ||
72 | const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all') | |
73 | const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body) | |
74 | if (!entry) return { created: false } | |
75 | ||
76 | const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true }) | |
77 | comment.Account = actor.Account | |
78 | comment.Video = videoInstance | |
79 | ||
80 | return { comment, created } | |
81 | } | |
82 | ||
83 | type ResolveThreadResult = Promise<{ video: VideoModel, parents: VideoCommentModel[] }> | |
84 | async function resolveThread (url: string, comments: VideoCommentModel[] = []): ResolveThreadResult { | |
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') | |
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? | |
102 | // If yes, it's done: we resolved all the thread | |
103 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url }) | |
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) { | |
124 | logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err }) | |
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 | ||
136 | if (sanitizeAndCheckVideoCommentObject(body) === false) { | |
137 | throw new Error('Remote video comment JSON is not valid :' + JSON.stringify(body)) | |
138 | } | |
139 | ||
140 | const actorUrl = body.attributedTo | |
141 | if (!actorUrl) throw new Error('Miss attributed to in comment') | |
142 | ||
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 | ||
151 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | |
152 | const comment = new VideoCommentModel({ | |
153 | url: body.id, | |
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 | } | |
165 | } | |
166 | ||
167 | export { | |
168 | videoCommentActivityObjectToDBAttributes, | |
169 | addVideoComments, | |
170 | addVideoComment, | |
171 | resolveThread | |
172 | } |