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 { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
13 import { Redis } from '../../redis'
14 import { createCacheFile } from '../cache-file'
16 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
20 if (activityType === 'View') {
21 return processCreateView(byActor, activity)
22 } else if (activityType === 'Dislike') {
23 return retryTransactionWrapper(processCreateDislike, byActor, activity)
24 } else if (activityType === 'Video') {
25 return processCreateVideo(activity)
26 } else if (activityType === 'Flag') {
27 return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject)
28 } else if (activityType === 'Note') {
29 return retryTransactionWrapper(processCreateVideoComment, byActor, activity)
30 } else if (activityType === 'CacheFile') {
31 return retryTransactionWrapper(processCacheFile, byActor, activity)
34 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
35 return Promise.resolve(undefined)
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 async function processCreateVideo (activity: ActivityCreate) {
47 const videoToCreateData = activity.object as VideoTorrentObject
49 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
54 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
55 const dislike = activity.object as DislikeObject
56 const byAccount = byActor.Account
58 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
60 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
62 return sequelizeTypescript.transaction(async t => {
64 type: 'dislike' as 'dislike',
66 accountId: byAccount.id
68 const [ , created ] = await AccountVideoRateModel.findOrCreate({
73 if (created === true) await video.increment('dislikes', { transaction: t })
75 if (video.isOwned() && created === true) {
76 // Don't resend the activity to the sender
77 const exceptions = [ byActor ]
79 await forwardVideoRelatedActivity(activity, t, exceptions, video)
84 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
85 const view = activity.object as ViewObject
88 videoObject: view.object,
89 fetchType: 'only-video' as 'only-video'
91 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
93 const actorExists = await ActorModel.isActorUrlExist(view.actor)
94 if (actorExists === false) throw new Error('Unknown actor ' + view.actor)
96 await Redis.Instance.addVideoView(video.id)
98 if (video.isOwned()) {
99 // Don't resend the activity to the sender
100 const exceptions = [ byActor ]
101 await forwardActivity(activity, undefined, exceptions)
105 async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
106 const cacheFile = activity.object as CacheFileObject
108 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
110 await createCacheFile(cacheFile, video, byActor)
112 if (video.isOwned()) {
113 // Don't resend the activity to the sender
114 const exceptions = [ byActor ]
115 await forwardActivity(activity, undefined, exceptions)
119 async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
120 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
122 const account = byActor.Account
123 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
125 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
127 return sequelizeTypescript.transaction(async t => {
128 const videoAbuseData = {
129 reporterAccountId: account.id,
130 reason: videoAbuseToCreateData.content,
132 state: VideoAbuseState.PENDING
135 await VideoAbuseModel.create(videoAbuseData, { transaction: t })
137 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
141 async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
142 const commentObject = activity.object as VideoCommentObject
143 const byAccount = byActor.Account
145 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
147 const { video } = await resolveThread(commentObject.inReplyTo)
149 const { created } = await addVideoComment(video, commentObject.id)
151 if (video.isOwned() && created === true) {
152 // Don't resend the activity to the sender
153 const exceptions = [ byActor ]
155 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)