]>
Commit | Line | Data |
---|---|---|
1 | import * as Bluebird from 'bluebird' | |
2 | import { ActivityCreate, VideoTorrentObject } from '../../../../shared' | |
3 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' | |
4 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' | |
5 | import { VideoRateType } from '../../../../shared/models/videos' | |
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | |
7 | import { logger } from '../../../helpers/logger' | |
8 | import { sequelizeTypescript } from '../../../initializers' | |
9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
10 | import { ActorModel } from '../../../models/activitypub/actor' | |
11 | import { TagModel } from '../../../models/video/tag' | |
12 | import { VideoModel } from '../../../models/video/video' | |
13 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
14 | import { VideoCommentModel } from '../../../models/video/video-comment' | |
15 | import { VideoFileModel } from '../../../models/video/video-file' | |
16 | import { getOrCreateActorAndServerAndModel } from '../actor' | |
17 | import { forwardActivity, getActorsInvolvedInVideo } from '../send/misc' | |
18 | import { generateThumbnailFromUrl } from '../videos' | |
19 | import { addVideoComments, addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' | |
20 | ||
21 | async function processCreateActivity (activity: ActivityCreate) { | |
22 | const activityObject = activity.object | |
23 | const activityType = activityObject.type | |
24 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) | |
25 | ||
26 | if (activityType === 'View') { | |
27 | return processCreateView(actor, activity) | |
28 | } else if (activityType === 'Dislike') { | |
29 | return processCreateDislike(actor, activity) | |
30 | } else if (activityType === 'Video') { | |
31 | return processCreateVideo(actor, activity) | |
32 | } else if (activityType === 'Flag') { | |
33 | return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) | |
34 | } else if (activityType === 'Note') { | |
35 | return processCreateVideoComment(actor, activity) | |
36 | } | |
37 | ||
38 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
39 | return Promise.resolve(undefined) | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { | |
45 | processCreateActivity | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | async function processCreateVideo ( | |
51 | actor: ActorModel, | |
52 | activity: ActivityCreate | |
53 | ) { | |
54 | const videoToCreateData = activity.object as VideoTorrentObject | |
55 | ||
56 | const channel = videoToCreateData.attributedTo.find(a => a.type === 'Group') | |
57 | if (!channel) throw new Error('Cannot find associated video channel to video ' + videoToCreateData.url) | |
58 | ||
59 | const channelActor = await getOrCreateActorAndServerAndModel(channel.id) | |
60 | ||
61 | const options = { | |
62 | arguments: [ actor, activity, videoToCreateData, channelActor ], | |
63 | errorMessage: 'Cannot insert the remote video with many retries.' | |
64 | } | |
65 | ||
66 | const video = await retryTransactionWrapper(createRemoteVideo, options) | |
67 | ||
68 | // Process outside the transaction because we could fetch remote data | |
69 | if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) { | |
70 | logger.info('Adding likes of video %s.', video.uuid) | |
71 | await createRates(videoToCreateData.likes.orderedItems, video, 'like') | |
72 | } | |
73 | ||
74 | if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { | |
75 | logger.info('Adding dislikes of video %s.', video.uuid) | |
76 | await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') | |
77 | } | |
78 | ||
79 | if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) { | |
80 | logger.info('Adding shares of video %s.', video.uuid) | |
81 | await addVideoShares(video, videoToCreateData.shares.orderedItems) | |
82 | } | |
83 | ||
84 | if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) { | |
85 | logger.info('Adding comments of video %s.', video.uuid) | |
86 | await addVideoComments(video, videoToCreateData.comments.orderedItems) | |
87 | } | |
88 | ||
89 | return video | |
90 | } | |
91 | ||
92 | function createRemoteVideo ( | |
93 | account: ActorModel, | |
94 | activity: ActivityCreate, | |
95 | videoToCreateData: VideoTorrentObject, | |
96 | channelActor: ActorModel | |
97 | ) { | |
98 | logger.debug('Adding remote video %s.', videoToCreateData.id) | |
99 | ||
100 | return sequelizeTypescript.transaction(async t => { | |
101 | const sequelizeOptions = { | |
102 | transaction: t | |
103 | } | |
104 | const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t) | |
105 | if (videoFromDatabase) return videoFromDatabase | |
106 | ||
107 | const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc) | |
108 | const video = VideoModel.build(videoData) | |
109 | ||
110 | // Don't block on request | |
111 | generateThumbnailFromUrl(video, videoToCreateData.icon) | |
112 | .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err)) | |
113 | ||
114 | const videoCreated = await video.save(sequelizeOptions) | |
115 | ||
116 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData) | |
117 | if (videoFileAttributes.length === 0) { | |
118 | throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url) | |
119 | } | |
120 | ||
121 | const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) | |
122 | await Promise.all(tasks) | |
123 | ||
124 | const tags = videoToCreateData.tag.map(t => t.name) | |
125 | const tagInstances = await TagModel.findOrCreateTags(tags, t) | |
126 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) | |
127 | ||
128 | logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid) | |
129 | ||
130 | return videoCreated | |
131 | }) | |
132 | } | |
133 | ||
134 | async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) { | |
135 | let rateCounts = 0 | |
136 | const tasks: Bluebird<any>[] = [] | |
137 | ||
138 | for (const actorUrl of actorUrls) { | |
139 | const actor = await getOrCreateActorAndServerAndModel(actorUrl) | |
140 | const p = AccountVideoRateModel | |
141 | .create({ | |
142 | videoId: video.id, | |
143 | accountId: actor.Account.id, | |
144 | type: rate | |
145 | }) | |
146 | .then(() => rateCounts += 1) | |
147 | ||
148 | tasks.push(p) | |
149 | } | |
150 | ||
151 | await Promise.all(tasks) | |
152 | ||
153 | logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid) | |
154 | ||
155 | // This is "likes" and "dislikes" | |
156 | await video.increment(rate + 's', { by: rateCounts }) | |
157 | ||
158 | return | |
159 | } | |
160 | ||
161 | async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) { | |
162 | const options = { | |
163 | arguments: [ byActor, activity ], | |
164 | errorMessage: 'Cannot dislike the video with many retries.' | |
165 | } | |
166 | ||
167 | return retryTransactionWrapper(createVideoDislike, options) | |
168 | } | |
169 | ||
170 | function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { | |
171 | const dislike = activity.object as DislikeObject | |
172 | const byAccount = byActor.Account | |
173 | ||
174 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
175 | ||
176 | return sequelizeTypescript.transaction(async t => { | |
177 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) | |
178 | if (!video) throw new Error('Unknown video ' + dislike.object) | |
179 | ||
180 | const rate = { | |
181 | type: 'dislike' as 'dislike', | |
182 | videoId: video.id, | |
183 | accountId: byAccount.id | |
184 | } | |
185 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ | |
186 | where: rate, | |
187 | defaults: rate, | |
188 | transaction: t | |
189 | }) | |
190 | if (created === true) await video.increment('dislikes', { transaction: t }) | |
191 | ||
192 | if (video.isOwned() && created === true) { | |
193 | // Don't resend the activity to the sender | |
194 | const exceptions = [ byActor ] | |
195 | await forwardActivity(activity, t, exceptions) | |
196 | } | |
197 | }) | |
198 | } | |
199 | ||
200 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { | |
201 | const view = activity.object as ViewObject | |
202 | ||
203 | const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) | |
204 | ||
205 | if (!video) throw new Error('Unknown video ' + view.object) | |
206 | ||
207 | const account = await ActorModel.loadByUrl(view.actor) | |
208 | if (!account) throw new Error('Unknown account ' + view.actor) | |
209 | ||
210 | await video.increment('views') | |
211 | ||
212 | if (video.isOwned()) { | |
213 | // Don't resend the activity to the sender | |
214 | const exceptions = [ byActor ] | |
215 | await forwardActivity(activity, undefined, exceptions) | |
216 | } | |
217 | } | |
218 | ||
219 | function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | |
220 | const options = { | |
221 | arguments: [ actor, videoAbuseToCreateData ], | |
222 | errorMessage: 'Cannot insert the remote video abuse with many retries.' | |
223 | } | |
224 | ||
225 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
226 | } | |
227 | ||
228 | function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { | |
229 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) | |
230 | ||
231 | const account = actor.Account | |
232 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
233 | ||
234 | return sequelizeTypescript.transaction(async t => { | |
235 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | |
236 | if (!video) { | |
237 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | |
238 | return undefined | |
239 | } | |
240 | ||
241 | const videoAbuseData = { | |
242 | reporterAccountId: account.id, | |
243 | reason: videoAbuseToCreateData.content, | |
244 | videoId: video.id | |
245 | } | |
246 | ||
247 | await VideoAbuseModel.create(videoAbuseData) | |
248 | ||
249 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
250 | }) | |
251 | } | |
252 | ||
253 | function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { | |
254 | const options = { | |
255 | arguments: [ byActor, activity ], | |
256 | errorMessage: 'Cannot create video comment with many retries.' | |
257 | } | |
258 | ||
259 | return retryTransactionWrapper(createVideoComment, options) | |
260 | } | |
261 | ||
262 | function createVideoComment (byActor: ActorModel, activity: ActivityCreate) { | |
263 | const comment = activity.object as VideoCommentObject | |
264 | const byAccount = byActor.Account | |
265 | ||
266 | if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) | |
267 | ||
268 | return sequelizeTypescript.transaction(async t => { | |
269 | let video = await VideoModel.loadByUrlAndPopulateAccount(comment.inReplyTo, t) | |
270 | let objectToCreate | |
271 | ||
272 | // This is a new thread | |
273 | if (video) { | |
274 | objectToCreate = { | |
275 | url: comment.id, | |
276 | text: comment.content, | |
277 | originCommentId: null, | |
278 | inReplyToComment: null, | |
279 | videoId: video.id, | |
280 | accountId: byAccount.id | |
281 | } | |
282 | } else { | |
283 | const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t) | |
284 | if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo) | |
285 | ||
286 | video = await VideoModel.load(inReplyToComment.videoId) | |
287 | ||
288 | const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id | |
289 | objectToCreate = { | |
290 | url: comment.id, | |
291 | text: comment.content, | |
292 | originCommentId, | |
293 | inReplyToCommentId: inReplyToComment.id, | |
294 | videoId: video.id, | |
295 | accountId: byAccount.id | |
296 | } | |
297 | } | |
298 | ||
299 | const options = { | |
300 | where: { | |
301 | url: objectToCreate.url | |
302 | }, | |
303 | defaults: objectToCreate, | |
304 | transaction: t | |
305 | } | |
306 | const [ ,created ] = await VideoCommentModel.findOrCreate(options) | |
307 | ||
308 | if (video.isOwned() && created === true) { | |
309 | // Don't resend the activity to the sender | |
310 | const exceptions = [ byActor ] | |
311 | ||
312 | // Mastodon does not add our announces in audience, so we forward to them manually | |
313 | const additionalActors = await getActorsInvolvedInVideo(video, t) | |
314 | const additionalFollowerUrls = additionalActors.map(a => a.followersUrl) | |
315 | ||
316 | await forwardActivity(activity, t, exceptions, additionalFollowerUrls) | |
317 | } | |
318 | }) | |
319 | } |