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'
16 import { Notifier } from '../../notifier'
18 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
19 const activityObject = activity.object
20 const activityType = activityObject.type
22 if (activityType === 'View') {
23 return processCreateView(byActor, activity)
24 } else if (activityType === 'Dislike') {
25 return retryTransactionWrapper(processCreateDislike, byActor, activity)
26 } else if (activityType === 'Video') {
27 return processCreateVideo(activity)
28 } else if (activityType === 'Flag') {
29 return retryTransactionWrapper(processCreateVideoAbuse, byActor, activityObject as VideoAbuseObject)
30 } else if (activityType === 'Note') {
31 return retryTransactionWrapper(processCreateVideoComment, byActor, activity)
32 } else if (activityType === 'CacheFile') {
33 return retryTransactionWrapper(processCacheFile, byActor, activity)
36 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
37 return Promise.resolve(undefined)
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 async function processCreateVideo (activity: ActivityCreate) {
49 const videoToCreateData = activity.object as VideoTorrentObject
51 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
53 if (created) Notifier.Instance.notifyOnNewVideo(video)
58 async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
59 const dislike = activity.object as DislikeObject
60 const byAccount = byActor.Account
62 if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
64 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: dislike.object })
66 return sequelizeTypescript.transaction(async t => {
68 type: 'dislike' as 'dislike',
70 accountId: byAccount.id
73 const [ , created ] = await AccountVideoRateModel.findOrCreate({
75 defaults: Object.assign({}, rate, { url: getVideoDislikeActivityPubUrl(byActor, video) }),
78 if (created === true) await video.increment('dislikes', { transaction: t })
80 if (video.isOwned() && created === true) {
81 // Don't resend the activity to the sender
82 const exceptions = [ byActor ]
84 await forwardVideoRelatedActivity(activity, t, exceptions, video)
89 async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
90 const view = activity.object as ViewObject
93 videoObject: view.object,
94 fetchType: 'only-video' as 'only-video'
96 const { video } = await getOrCreateVideoAndAccountAndChannel(options)
98 await Redis.Instance.addVideoView(video.id)
100 if (video.isOwned()) {
101 // Don't resend the activity to the sender
102 const exceptions = [ byActor ]
103 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
107 async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) {
108 const cacheFile = activity.object as CacheFileObject
110 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
112 await sequelizeTypescript.transaction(async t => {
113 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
116 if (video.isOwned()) {
117 // Don't resend the activity to the sender
118 const exceptions = [ byActor ]
119 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
123 async function processCreateVideoAbuse (byActor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
124 logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
126 const account = byActor.Account
127 if (!account) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
129 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoAbuseToCreateData.object })
131 return sequelizeTypescript.transaction(async t => {
132 const videoAbuseData = {
133 reporterAccountId: account.id,
134 reason: videoAbuseToCreateData.content,
136 state: VideoAbuseState.PENDING
139 const videoAbuseInstance = await VideoAbuseModel.create(videoAbuseData, { transaction: t })
140 videoAbuseInstance.Video = video
142 Notifier.Instance.notifyOnNewVideoAbuse(videoAbuseInstance)
144 logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
148 async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
149 const commentObject = activity.object as VideoCommentObject
150 const byAccount = byActor.Account
152 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
154 const { video } = await resolveThread(commentObject.inReplyTo)
156 const { comment, created } = await addVideoComment(video, commentObject.id)
158 if (video.isOwned() && created === true) {
159 // Don't resend the activity to the sender
160 const exceptions = [ byActor ]
162 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
165 if (created === true) Notifier.Instance.notifyOnNewComment(comment)