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 { resolveThread } from '../video-comments'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { forwardVideoRelatedActivity } from '../send/utils'
9 import { createOrUpdateCacheFile } from '../cache-file'
10 import { Notifier } from '../../notifier'
11 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
12 import { createOrUpdateVideoPlaylist } from '../playlist'
13 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
14 import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models'
16 async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
17 const { activity, byActor } = options
19 // Only notify if it is not from a fetcher job
20 const notify = options.fromFetch !== true
21 const activityObject = activity.object
22 const activityType = activityObject.type
24 if (activityType === 'Video') {
25 return processCreateVideo(activity, notify)
28 if (activityType === 'Note') {
29 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
32 if (activityType === 'CacheFile') {
33 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
36 if (activityType === 'Playlist') {
37 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
40 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
41 return Promise.resolve(undefined)
44 // ---------------------------------------------------------------------------
50 // ---------------------------------------------------------------------------
52 async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
53 const videoToCreateData = activity.object as VideoTorrentObject
55 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
57 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
62 async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
63 const cacheFile = activity.object as CacheFileObject
65 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
67 await sequelizeTypescript.transaction(async t => {
68 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
71 if (video.isOwned()) {
72 // Don't resend the activity to the sender
73 const exceptions = [ byActor ]
74 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
78 async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
79 const commentObject = activity.object as VideoCommentObject
80 const byAccount = byActor.Account
82 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
84 let video: MVideoAccountLightBlacklistAllFiles
86 let comment: MCommentOwnerVideo
88 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
89 video = resolveThreadResult.video
90 created = resolveThreadResult.commentCreated
91 comment = resolveThreadResult.comment
94 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
95 commentObject.inReplyTo,
101 if (video.isOwned() && created === true) {
102 // Don't resend the activity to the sender
103 const exceptions = [ byActor ]
105 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
108 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
111 async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
112 const playlistObject = activity.object as PlaylistObject
113 const byAccount = byActor.Account
115 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
117 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)