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 { logger, retryTransactionWrapper } from '../../../helpers'
7 import { sequelizeTypescript } from '../../../initializers'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { ActorModel } from '../../../models/activitypub/actor'
10 import { TagModel } from '../../../models/video/tag'
11 import { VideoModel } from '../../../models/video/video'
12 import { VideoAbuseModel } from '../../../models/video/video-abuse'
13 import { VideoCommentModel } from '../../../models/video/video-comment'
14 import { VideoFileModel } from '../../../models/video/video-file'
15 import { getOrCreateActorAndServerAndModel } from '../actor'
16 import { forwardActivity } from '../send/misc'
17 import { generateThumbnailFromUrl } from '../videos'
18 import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
20 async function processCreateActivity (activity: ActivityCreate) {
21 const activityObject = activity.object
22 const activityType = activityObject.type
23 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
25 if (activityType === 'View') {
26 return processCreateView(actor, activity)
27 } else if (activityType === 'Dislike') {
28 return processCreateDislike(actor, activity)
29 } else if (activityType === 'Video') {
30 return processCreateVideo(actor, activity)
31 } else if (activityType === 'Flag') {
32 return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
33 } else if (activityType === 'Note') {
34 return processCreateVideoComment(actor, activity)
37 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
38 return Promise.resolve(undefined)
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 async function processCreateVideo (
51 activity: ActivityCreate
53 const videoToCreateData = activity.object as VideoTorrentObject
55 const channel = videoToCreateData.attributedTo.find(a => a.type === 'Group')
56 if (!channel) throw new Error('Cannot find associated video channel to video ' + videoToCreateData.url)
58 const channelActor = await getOrCreateActorAndServerAndModel(channel.id)
61 arguments: [ actor, activity, videoToCreateData, channelActor ],
62 errorMessage: 'Cannot insert the remote video with many retries.'
65 const video = await retryTransactionWrapper(createRemoteVideo, options)
67 // Process outside the transaction because we could fetch remote data
68 if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
69 await createRates(videoToCreateData.likes.orderedItems, video, 'like')
72 if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
73 await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
76 if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
77 await addVideoShares(video, videoToCreateData.shares.orderedItems)
83 function createRemoteVideo (
85 activity: ActivityCreate,
86 videoToCreateData: VideoTorrentObject,
87 channelActor: ActorModel
89 logger.debug('Adding remote video %s.', videoToCreateData.id)
91 return sequelizeTypescript.transaction(async t => {
92 const sequelizeOptions = {
95 const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
96 if (videoFromDatabase) return videoFromDatabase
98 const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc)
99 const video = VideoModel.build(videoData)
101 // Don't block on request
102 generateThumbnailFromUrl(video, videoToCreateData.icon)
103 .catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
105 const videoCreated = await video.save(sequelizeOptions)
107 const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
108 if (videoFileAttributes.length === 0) {
109 throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
112 const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
113 await Promise.all(tasks)
115 const tags = videoToCreateData.tag.map(t => t.name)
116 const tagInstances = await TagModel.findOrCreateTags(tags, t)
117 await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
119 logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
125 async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
127 const tasks: Bluebird<any>[] = []
129 for (const actorUrl of actorUrls) {
130 const actor = await getOrCreateActorAndServerAndModel(actorUrl)
131 const p = AccountVideoRateModel
134 accountId: actor.Account.id,
137 .then(() => rateCounts += 1)
142 await Promise.all(tasks)
144 logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
146 // This is "likes" and "dislikes"
147 await video.increment(rate + 's', { by: rateCounts })
152 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
154 arguments: [ byActor, activity ],
155 errorMessage: 'Cannot dislike the video with many retries.'
158 return retryTransactionWrapper(createVideoDislike, options)
161 function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
162 const dislike = activity.object as DislikeObject
163 const byAccount = byActor.Account
165 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
167 return sequelizeTypescript.transaction(async t => {
168 const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
169 if (!video) throw new Error('Unknown video ' + dislike.object)
172 type: 'dislike' as 'dislike',
174 accountId: byAccount.id
176 const [ , created ] = await AccountVideoRateModel.findOrCreate({
181 if (created === true) await video.increment('dislikes', { transaction: t })
183 if (video.isOwned() && created === true) {
184 // Don't resend the activity to the sender
185 const exceptions = [ byActor ]
186 await forwardActivity(activity, t, exceptions)
191 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
192 const view = activity.object as ViewObject
194 const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
196 if (!video) throw new Error('Unknown video ' + view.object)
198 const account = await ActorModel.loadByUrl(view.actor)
199 if (!account) throw new Error('Unknown account ' + view.actor)
201 await video.increment('views')
203 if (video.isOwned()) {
204 // Don't resend the activity to the sender
205 const exceptions = [ byActor ]
206 await forwardActivity(activity, undefined, exceptions)
210 function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
212 arguments: [ actor, videoAbuseToCreateData ],
213 errorMessage: 'Cannot insert the remote video abuse with many retries.'
216 return retryTransactionWrapper(addRemoteVideoAbuse, options)
219 function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
220 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
222 const account = actor.Account
223 if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
225 return sequelizeTypescript.transaction(async t => {
226 const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
228 logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
232 const videoAbuseData = {
233 reporterAccountId: account.id,
234 reason: videoAbuseToCreateData.content,
238 await VideoAbuseModel.create(videoAbuseData)
240 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
244 function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
246 arguments: [ byActor, activity ],
247 errorMessage: 'Cannot create video comment with many retries.'
250 return retryTransactionWrapper(createVideoComment, options)
253 function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
254 const comment = activity.object as VideoCommentObject
255 const byAccount = byActor.Account
257 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
259 return sequelizeTypescript.transaction(async t => {
260 let video = await VideoModel.loadByUrl(comment.inReplyTo, t)
262 // This is a new thread
264 await VideoCommentModel.create({
266 text: comment.content,
267 originCommentId: null,
268 inReplyToComment: null,
270 accountId: byAccount.id
271 }, { transaction: t })
273 const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t)
274 if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo)
276 video = await VideoModel.load(inReplyToComment.videoId)
278 const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id
279 await VideoCommentModel.create({
281 text: comment.content,
283 inReplyToCommentId: inReplyToComment.id,
285 accountId: byAccount.id
286 }, { transaction: t })
289 if (video.isOwned()) {
290 // Don't resend the activity to the sender
291 const exceptions = [ byActor ]
292 await forwardActivity(activity, t, exceptions)