1 import { ActivityCreate, CacheFileObject, VideoTorrentObject } from '../../../../shared'
2 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { ActorModel } from '../../../models/activitypub/actor'
7 import { addVideoComment, resolveThread } from '../video-comments'
8 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
9 import { forwardVideoRelatedActivity } from '../send/utils'
10 import { createOrUpdateCacheFile } from '../cache-file'
11 import { Notifier } from '../../notifier'
12 import { processViewActivity } from './process-view'
13 import { processDislikeActivity } from './process-dislike'
14 import { processFlagActivity } from './process-flag'
16 async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) {
17 const activityObject = activity.object
18 const activityType = activityObject.type
20 if (activityType === 'View') {
21 return processViewActivity(activity, byActor)
24 if (activityType === 'Dislike') {
25 return retryTransactionWrapper(processDislikeActivity, activity, byActor)
28 if (activityType === 'Flag') {
29 return retryTransactionWrapper(processFlagActivity, activity, byActor)
32 if (activityType === 'Video') {
33 return processCreateVideo(activity)
36 if (activityType === 'Note') {
37 return retryTransactionWrapper(processCreateVideoComment, activity, byActor)
40 if (activityType === 'CacheFile') {
41 return retryTransactionWrapper(processCacheFile, activity, byActor)
44 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
45 return Promise.resolve(undefined)
48 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 async function processCreateVideo (activity: ActivityCreate) {
57 const videoToCreateData = activity.object as VideoTorrentObject
59 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
61 if (created) Notifier.Instance.notifyOnNewVideo(video)
66 async function processCacheFile (activity: ActivityCreate, byActor: ActorModel) {
67 const cacheFile = activity.object as CacheFileObject
69 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
71 await sequelizeTypescript.transaction(async t => {
72 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
75 if (video.isOwned()) {
76 // Don't resend the activity to the sender
77 const exceptions = [ byActor ]
78 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
82 async function processCreateVideoComment (activity: ActivityCreate, byActor: ActorModel) {
83 const commentObject = activity.object as VideoCommentObject
84 const byAccount = byActor.Account
86 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
88 const { video } = await resolveThread(commentObject.inReplyTo)
90 const { comment, created } = await addVideoComment(video, commentObject.id)
92 if (video.isOwned() && created === true) {
93 // Don't resend the activity to the sender
94 const exceptions = [ byActor ]
96 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
99 if (created === true) Notifier.Instance.notifyOnNewComment(comment)