]>
Commit | Line | Data |
---|---|---|
50d6de9c C |
1 | import * as Bluebird from 'bluebird' |
2 | import { ActivityCreate, VideoTorrentObject } from '../../../../shared' | |
3fd3ab2d | 3 | import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects' |
6d852470 | 4 | import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object' |
50d6de9c | 5 | import { VideoRateType } from '../../../../shared/models/videos' |
da854ddd C |
6 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
7 | import { logger } from '../../../helpers/logger' | |
3fd3ab2d | 8 | import { sequelizeTypescript } from '../../../initializers' |
3fd3ab2d | 9 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
50d6de9c C |
10 | import { ActorModel } from '../../../models/activitypub/actor' |
11 | import { TagModel } from '../../../models/video/tag' | |
3fd3ab2d C |
12 | import { VideoModel } from '../../../models/video/video' |
13 | import { VideoAbuseModel } from '../../../models/video/video-abuse' | |
6d852470 | 14 | import { VideoCommentModel } from '../../../models/video/video-comment' |
50d6de9c C |
15 | import { VideoFileModel } from '../../../models/video/video-file' |
16 | import { getOrCreateActorAndServerAndModel } from '../actor' | |
63c93323 | 17 | import { forwardActivity } from '../send/misc' |
50d6de9c | 18 | import { generateThumbnailFromUrl } from '../videos' |
da854ddd | 19 | import { addVideoComments, addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc' |
e4f97bab | 20 | |
0d0e8dd0 | 21 | async function processCreateActivity (activity: ActivityCreate) { |
e4f97bab C |
22 | const activityObject = activity.object |
23 | const activityType = activityObject.type | |
50d6de9c | 24 | const actor = await getOrCreateActorAndServerAndModel(activity.actor) |
e4f97bab | 25 | |
40ff5707 | 26 | if (activityType === 'View') { |
50d6de9c | 27 | return processCreateView(actor, activity) |
0032ebe9 | 28 | } else if (activityType === 'Dislike') { |
50d6de9c C |
29 | return processCreateDislike(actor, activity) |
30 | } else if (activityType === 'Video') { | |
31 | return processCreateVideo(actor, activity) | |
8e13fa7d | 32 | } else if (activityType === 'Flag') { |
50d6de9c | 33 | return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject) |
6d852470 C |
34 | } else if (activityType === 'Note') { |
35 | return processCreateVideoComment(actor, activity) | |
e4f97bab C |
36 | } |
37 | ||
38 | logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id }) | |
0d0e8dd0 | 39 | return Promise.resolve(undefined) |
e4f97bab C |
40 | } |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { | |
45 | processCreateActivity | |
46 | } | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50d6de9c C |
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)) { | |
da854ddd | 70 | logger.info('Adding likes of video %s.', video.uuid) |
50d6de9c C |
71 | await createRates(videoToCreateData.likes.orderedItems, video, 'like') |
72 | } | |
73 | ||
74 | if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) { | |
da854ddd | 75 | logger.info('Adding dislikes of video %s.', video.uuid) |
50d6de9c C |
76 | await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike') |
77 | } | |
78 | ||
79 | if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) { | |
da854ddd | 80 | logger.info('Adding shares of video %s.', video.uuid) |
50d6de9c C |
81 | await addVideoShares(video, videoToCreateData.shares.orderedItems) |
82 | } | |
83 | ||
da854ddd C |
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 | ||
50d6de9c C |
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) { | |
0032ebe9 | 162 | const options = { |
50d6de9c | 163 | arguments: [ byActor, activity ], |
0032ebe9 C |
164 | errorMessage: 'Cannot dislike the video with many retries.' |
165 | } | |
166 | ||
167 | return retryTransactionWrapper(createVideoDislike, options) | |
168 | } | |
169 | ||
50d6de9c | 170 | function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 | 171 | const dislike = activity.object as DislikeObject |
50d6de9c C |
172 | const byAccount = byActor.Account |
173 | ||
174 | if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url) | |
0032ebe9 | 175 | |
3fd3ab2d C |
176 | return sequelizeTypescript.transaction(async t => { |
177 | const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t) | |
0032ebe9 C |
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 | } | |
3fd3ab2d | 185 | const [ , created ] = await AccountVideoRateModel.findOrCreate({ |
0032ebe9 | 186 | where: rate, |
63c93323 C |
187 | defaults: rate, |
188 | transaction: t | |
0032ebe9 | 189 | }) |
f00984c0 | 190 | if (created === true) await video.increment('dislikes', { transaction: t }) |
0032ebe9 | 191 | |
63c93323 C |
192 | if (video.isOwned() && created === true) { |
193 | // Don't resend the activity to the sender | |
50d6de9c | 194 | const exceptions = [ byActor ] |
63c93323 C |
195 | await forwardActivity(activity, t, exceptions) |
196 | } | |
0032ebe9 C |
197 | }) |
198 | } | |
199 | ||
6d852470 | 200 | async function processCreateView (byActor: ActorModel, activity: ActivityCreate) { |
63c93323 C |
201 | const view = activity.object as ViewObject |
202 | ||
3fd3ab2d | 203 | const video = await VideoModel.loadByUrlAndPopulateAccount(view.object) |
40ff5707 C |
204 | |
205 | if (!video) throw new Error('Unknown video ' + view.object) | |
206 | ||
50d6de9c | 207 | const account = await ActorModel.loadByUrl(view.actor) |
40ff5707 C |
208 | if (!account) throw new Error('Unknown account ' + view.actor) |
209 | ||
210 | await video.increment('views') | |
211 | ||
63c93323 C |
212 | if (video.isOwned()) { |
213 | // Don't resend the activity to the sender | |
6d852470 | 214 | const exceptions = [ byActor ] |
63c93323 C |
215 | await forwardActivity(activity, undefined, exceptions) |
216 | } | |
40ff5707 C |
217 | } |
218 | ||
50d6de9c | 219 | function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d | 220 | const options = { |
50d6de9c | 221 | arguments: [ actor, videoAbuseToCreateData ], |
8e13fa7d C |
222 | errorMessage: 'Cannot insert the remote video abuse with many retries.' |
223 | } | |
224 | ||
225 | return retryTransactionWrapper(addRemoteVideoAbuse, options) | |
226 | } | |
227 | ||
50d6de9c | 228 | function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) { |
8e13fa7d C |
229 | logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object) |
230 | ||
50d6de9c C |
231 | const account = actor.Account |
232 | if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url) | |
233 | ||
3fd3ab2d C |
234 | return sequelizeTypescript.transaction(async t => { |
235 | const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t) | |
8e13fa7d C |
236 | if (!video) { |
237 | logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object) | |
79d5caf9 | 238 | return undefined |
8e13fa7d C |
239 | } |
240 | ||
241 | const videoAbuseData = { | |
242 | reporterAccountId: account.id, | |
243 | reason: videoAbuseToCreateData.content, | |
244 | videoId: video.id | |
245 | } | |
246 | ||
3fd3ab2d | 247 | await VideoAbuseModel.create(videoAbuseData) |
8e13fa7d C |
248 | |
249 | logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object) | |
250 | }) | |
251 | } | |
6d852470 C |
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 => { | |
ea44f375 | 269 | let video = await VideoModel.loadByUrl(comment.inReplyTo, t) |
6d852470 C |
270 | |
271 | // This is a new thread | |
272 | if (video) { | |
ea44f375 | 273 | await VideoCommentModel.create({ |
6d852470 C |
274 | url: comment.id, |
275 | text: comment.content, | |
276 | originCommentId: null, | |
277 | inReplyToComment: null, | |
278 | videoId: video.id, | |
d3ea8975 | 279 | accountId: byAccount.id |
6d852470 | 280 | }, { transaction: t }) |
ea44f375 C |
281 | } else { |
282 | const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t) | |
283 | if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo) | |
6d852470 | 284 | |
ea44f375 | 285 | video = await VideoModel.load(inReplyToComment.videoId) |
6d852470 | 286 | |
ea44f375 C |
287 | const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id |
288 | await VideoCommentModel.create({ | |
289 | url: comment.id, | |
290 | text: comment.content, | |
291 | originCommentId, | |
292 | inReplyToCommentId: inReplyToComment.id, | |
293 | videoId: video.id, | |
294 | accountId: byAccount.id | |
295 | }, { transaction: t }) | |
296 | } | |
297 | ||
298 | if (video.isOwned()) { | |
299 | // Don't resend the activity to the sender | |
300 | const exceptions = [ byActor ] | |
301 | await forwardActivity(activity, t, exceptions) | |
302 | } | |
6d852470 C |
303 | }) |
304 | } |