1 import { isBlockedByServerOrAccount } from '@server/lib/blocklist'
2 import { isRedundancyAccepted } from '@server/lib/redundancy'
3 import { ActivityCreate, CacheFileObject, VideoObject } from '../../../../shared'
4 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
5 import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
6 import { retryTransactionWrapper } from '../../../helpers/database-utils'
7 import { logger } from '../../../helpers/logger'
8 import { sequelizeTypescript } from '../../../initializers/database'
9 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
10 import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
11 import { Notifier } from '../../notifier'
12 import { createOrUpdateCacheFile } from '../cache-file'
13 import { createOrUpdateVideoPlaylist } from '../playlists'
14 import { forwardVideoRelatedActivity } from '../send/utils'
15 import { resolveThread } from '../video-comments'
16 import { getOrCreateAPVideo } from '../videos'
18 async function processCreateActivity (options: APProcessorOptions<ActivityCreate>) {
19 const { activity, byActor } = options
21 // Only notify if it is not from a fetcher job
22 const notify = options.fromFetch !== true
23 const activityObject = activity.object
24 const activityType = activityObject.type
26 if (activityType === 'Video') {
27 return processCreateVideo(activity, notify)
30 if (activityType === 'Note') {
31 // Comments will be fetched from videos
32 if (options.fromFetch) return
34 return retryTransactionWrapper(processCreateVideoComment, activity, byActor, notify)
37 if (activityType === 'CacheFile') {
38 return retryTransactionWrapper(processCreateCacheFile, activity, byActor)
41 if (activityType === 'Playlist') {
42 return retryTransactionWrapper(processCreatePlaylist, activity, byActor)
45 logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
46 return Promise.resolve(undefined)
49 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 async function processCreateVideo (activity: ActivityCreate, notify: boolean) {
58 const videoToCreateData = activity.object as VideoObject
60 const syncParam = { likes: false, dislikes: false, shares: false, comments: false, thumbnail: true, refreshVideo: false }
61 const { video, created } = await getOrCreateAPVideo({ videoObject: videoToCreateData, syncParam })
63 if (created && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
68 async function processCreateCacheFile (activity: ActivityCreate, byActor: MActorSignature) {
69 if (await isRedundancyAccepted(activity, byActor) !== true) return
71 const cacheFile = activity.object as CacheFileObject
73 const { video } = await getOrCreateAPVideo({ videoObject: cacheFile.object })
75 await sequelizeTypescript.transaction(async t => {
76 return createOrUpdateCacheFile(cacheFile, video, byActor, t)
79 if (video.isOwned()) {
80 // Don't resend the activity to the sender
81 const exceptions = [ byActor ]
82 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
86 async function processCreateVideoComment (activity: ActivityCreate, byActor: MActorSignature, notify: boolean) {
87 const commentObject = activity.object as VideoCommentObject
88 const byAccount = byActor.Account
90 if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
92 let video: MVideoAccountLightBlacklistAllFiles
94 let comment: MCommentOwnerVideo
96 const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false })
98 video = resolveThreadResult.video
99 created = resolveThreadResult.commentCreated
100 comment = resolveThreadResult.comment
103 'Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.',
104 commentObject.inReplyTo,
110 // Try to not forward unwanted commments on our videos
111 if (video.isOwned()) {
112 if (await isBlockedByServerOrAccount(comment.Account, video.VideoChannel.Account)) {
113 logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url)
117 if (created === true) {
118 // Don't resend the activity to the sender
119 const exceptions = [ byActor ]
121 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
125 if (created && notify) Notifier.Instance.notifyOnNewComment(comment)
128 async function processCreatePlaylist (activity: ActivityCreate, byActor: MActorSignature) {
129 const playlistObject = activity.object as PlaylistObject
130 const byAccount = byActor.Account
132 if (!byAccount) throw new Error('Cannot create video playlist with the non account actor ' + byActor.url)
134 await createOrUpdateVideoPlaylist(playlistObject, activity.to)