1 import { ActivityCreate, CacheFileObject, VideoAbuseState, VideoTorrentObject } from '../../../../shared'
2 import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
3 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
4 import { retryTransactionWrapper } from '../../../helpers/database-utils'
5 import { logger } from '../../../helpers/logger'
6 import { sequelizeTypescript } from '../../../initializers'
7 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
8 import { ActorModel } from '../../../models/activitypub/actor'
9 import { VideoAbuseModel } from '../../../models/video/video-abuse'
10 import { addVideoComment, resolveThread } from '../video-comments'
11 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
12 import { forwardVideoRelatedActivity } from '../send/utils'
13 import { Redis } from '../../redis'
14 import { createOrUpdateCacheFile } from '../cache-file'
15 import { getVideoDislikeActivityPubUrl } from '../url'
17 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
18 const activityObject = activity.object
19 const activityType = activityObject.type
21 if (activityType === 'View') {
22 return processCreateView(byActor, activity)
23 } else if (activityType === 'Dislike') {
24 return retryTransactionWrapper(processCreateDislike, byActor, activity)
25 } else if (activityType === 'Video') {
26 return processCreateVideo(activity)
27 } else if (activityType === 'Flag') {
28 return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject)
29 } else if (activityType === 'Note') {
30 return retryTransactionWrapper(processCreateVideoComment, byActor, activity)
31 } else if (activityType === 'CacheFile') {
32 return retryTransactionWrapper(processCacheFile, byActor, activity)
35 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
36 return Promise.resolve(undefined)
39 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 async function processCreateVideo (activity: ActivityCreate) {
48 const videoToCreateData = activity.object as VideoTorrentObject
50 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
55 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
56 const dislike = activity.object as DislikeObject
57 const byAccount = byActor.Account
59 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
61 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
63 return sequelizeTypescript.transaction(async t => {
65 type: 'dislike' as 'dislike',
67 accountId: byAccount.id
70 const [ , created ] = await AccountVideoRateModel.findOrCreate({
72 defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }),
75 if (created === true) await video.increment('dislikes', { transaction: t })
77 if (video.isOwned() && created === true) {
78 // Don't resend the activity to the sender
79 const exceptions = [ byActor ]
81 await forwardVideoRelatedActivity(activity, t, exceptions, video)
86 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
87 const view = activity.object as ViewObject
90 videoObject: view.object,
91 fetchType: 'only-video' as 'only-video'
93 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
95 await Redis.Instance.addVideoView(video.id)
97 if (video.isOwned()) {
98 // Don't resend the activity to the sender
99 const exceptions = [ byActor ]
100 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
104 async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
105 const cacheFile = activity.object as CacheFileObject
107 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
109 await sequelizeTypescript.transaction(async t => {
110 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
113 if (video.isOwned()) {
114 // Don't resend the activity to the sender
115 const exceptions = [ byActor ]
116 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
120 async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
121 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
123 const account = byActor.Account
124 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
126 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
128 return sequelizeTypescript.transaction(async t => {
129 const videoAbuseData = {
130 reporterAccountId: account.id,
131 reason: videoAbuseToCreateData.content,
133 state: VideoAbuseState.PENDING
136 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
138 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
142 async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
143 const commentObject = activity.object as VideoCommentObject
144 const byAccount = byActor.Account
146 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
148 const { video } = await resolveThread(commentObject.inReplyTo)
150 const { created } = await addVideoComment(video, commentObject.id)
152 if (video.isOwned() && created === true) {
153 // Don't resend the activity to the sender
154 const exceptions = [ byActor ]
156 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)