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'
15 import { isRedundancyAccepted } from '@server/lib/redundancy'
17 async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
18 const { activity, byActor } = options
20 // Only notify if it is not from a fetcher job
21 const notify = options.fromFetch !== true
22 const activityObject = activity.object
23 const activityType = activityObject.type
25 if (activityType === 'Video') {
26 return processCreateVideo(activity, notify)
29 if (activityType === 'Note') {
30 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
33 if (activityType === 'CacheFile') {
34 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
37 if (activityType === 'Playlist') {
38 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
41 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
42 return Promise.resolve(undefined)
45 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
54 const videoToCreateData = activity.object as VideoTorrentObject
56 const { video, created } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoToCreateData })
58 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
63 async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
64 if (await isRedundancyAccepted(activity, byActor) !== true) return
66 const cacheFile = activity.object as CacheFileObject
68 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object })
70 await sequelizeTypescript.transaction(async t => {
71 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
74 if (video.isOwned()) {
75 // Don't resend the activity to the sender
76 const exceptions = [ byActor ]
77 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
81 async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
82 const commentObject = activity.object as VideoCommentObject
83 const byAccount = byActor.Account
85 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
87 let video: MVideoAccountLightBlacklistAllFiles
89 let comment: MCommentOwnerVideo
91 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
92 video = resolveThreadResult.video
93 created = resolveThreadResult.commentCreated
94 comment = resolveThreadResult.comment
97 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
98 commentObject.inReplyTo,
104 if (video.isOwned() && created === true) {
105 // Don't resend the activity to the sender
106 const exceptions = [ byActor ]
108 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
111 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
114 async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
115 const playlistObject = activity.object as PlaylistObject
116 const byAccount = byActor.Account
118 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
120 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)