1 import * as Bluebird from 'bluebird'
2 import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
3 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
4 import { VideoRateType } from '../../../../shared/models/videos'
5 import { logger, retryTransactionWrapper } from '../../../helpers'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { TagModel } from '../../../models/video/tag'
10 import { VideoModel } from '../../../models/video/video'
11 import { VideoAbuseModel } from '../../../models/video/video-abuse'
12 import { VideoFileModel } from '../../../models/video/video-file'
13 import { getOrCreateActorAndServerAndModel } from '../actor'
14 import { forwardActivity } from '../send/misc'
15 import { generateThumbnailFromUrl } from '../videos'
16 import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
18 async function processCreateActivity (activity: ActivityCreate) {
19 const activityObject = activity.object
20 const activityType = activityObject.type
21 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
23 if (activityType === 'View') {
24 return processCreateView(actor, activity)
25 } else if (activityType === 'Dislike') {
26 return processCreateDislike(actor, activity)
27 } else if (activityType === 'Video') {
28 return processCreateVideo(actor, activity)
29 } else if (activityType === 'Flag') {
30 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
33 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
34 return Promise.resolve(undefined)
37 // ---------------------------------------------------------------------------
43 // ---------------------------------------------------------------------------
45 async function processCreateVideo (
47 activity: ActivityCreate
49 const videoToCreateData = activity.object as VideoTorrentObject
51 const channel = videoToCreateData.attributedTo.find(a => a.type === 'Group')
52 if (!channel) throw new Error('Cannot find associated video channel to video ' + videoToCreateData.url)
54 const channelActor = await getOrCreateActorAndServerAndModel(channel.id)
57 arguments: [ actor, activity, videoToCreateData, channelActor ],
58 errorMessage: 'Cannot insert the remote video with many retries.'
61 const video = await retryTransactionWrapper(createRemoteVideo, options)
63 // Process outside the transaction because we could fetch remote data
64 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
65 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
68 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
69 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
72 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
73 await addVideoShares(video, videoToCreateData.shares.orderedItems)
79 function createRemoteVideo (
81 activity: ActivityCreate,
82 videoToCreateData: VideoTorrentObject,
83 channelActor: ActorModel
85 logger.debug('Adding remote video %s.', videoToCreateData.id)
87 return sequelizeTypescript.transaction(async t => {
88 const sequelizeOptions = {
91 const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
92 if (videoFromDatabase) return videoFromDatabase
94 const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc)
95 const video = VideoModel.build(videoData)
97 // Don't block on request
98 generateThumbnailFromUrl(video, videoToCreateData.icon)
99 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
101 const videoCreated = await video.save(sequelizeOptions)
103 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
104 if (videoFileAttributes.length === 0) {
105 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
108 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
109 await Promise.all(tasks)
111 const tags = videoToCreateData.tag.map(t => t.name)
112 const tagInstances = await TagModel.findOrCreateTags(tags, t)
113 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
115 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
121 async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
123 const tasks: Bluebird<any>[] = []
125 for (const actorUrl of actorUrls) {
126 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
127 const p = AccountVideoRateModel
130 accountId: actor.Account.id,
133 .then(() => rateCounts += 1)
138 await Promise.all(tasks)
140 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
142 // This is "likes" and "dislikes"
143 await video.increment(rate + 's', { by: rateCounts })
148 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
150 arguments: [ byActor, activity ],
151 errorMessage: 'Cannot dislike the video with many retries.'
154 return retryTransactionWrapper(createVideoDislike, options)
157 function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
158 const dislike = activity.object as DislikeObject
159 const byAccount = byActor.Account
161 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
163 return sequelizeTypescript.transaction(async t => {
164 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
165 if (!video) throw new Error('Unknown video ' + dislike.object)
168 type: 'dislike' as 'dislike',
170 accountId: byAccount.id
172 const [ , created ] = await AccountVideoRateModel.findOrCreate({
177 if (created === true) await video.increment('dislikes', { transaction: t })
179 if (video.isOwned() && created === true) {
180 // Don't resend the activity to the sender
181 const exceptions = [ byActor ]
182 await forwardActivity(activity, t, exceptions)
187 async function processCreateView (byAccount: ActorModel, activity: ActivityCreate) {
188 const view = activity.object as ViewObject
190 const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
192 if (!video) throw new Error('Unknown video ' + view.object)
194 const account = await ActorModel.loadByUrl(view.actor)
195 if (!account) throw new Error('Unknown account ' + view.actor)
197 await video.increment('views')
199 if (video.isOwned()) {
200 // Don't resend the activity to the sender
201 const exceptions = [ byAccount ]
202 await forwardActivity(activity, undefined, exceptions)
206 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
208 arguments: [ actor, videoAbuseToCreateData ],
209 errorMessage: 'Cannot insert the remote video abuse with many retries.'
212 return retryTransactionWrapper(addRemoteVideoAbuse, options)
215 function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
216 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
218 const account = actor.Account
219 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
221 return sequelizeTypescript.transaction(async t => {
222 const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
224 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
228 const videoAbuseData = {
229 reporterAccountId: account.id,
230 reason: videoAbuseToCreateData.content,
234 await VideoAbuseModel.create(videoAbuseData)
236 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)