]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-announce.ts
Support refusing remote comments
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
1 import { getAPId } from '@server/lib/activitypub/activity'
2 import { ActivityAnnounce } from '../../../../shared/models/activitypub'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers/database'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8 import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
9 import { Notifier } from '../../notifier'
10 import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
11 import { getOrCreateAPVideo } from '../videos'
12
13 async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
14 const { activity, byActor: actorAnnouncer } = options
15 // Only notify if it is not from a fetcher job
16 const notify = options.fromFetch !== true
17
18 // Announces on accounts are not supported
19 if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return
20
21 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
22 }
23
24 // ---------------------------------------------------------------------------
25
26 export {
27 processAnnounceActivity
28 }
29
30 // ---------------------------------------------------------------------------
31
32 async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
33 const objectUri = getAPId(activity.object)
34
35 let video: MVideoAccountLightBlacklistAllFiles
36 let videoCreated: boolean
37
38 try {
39 const result = await getOrCreateAPVideo({ videoObject: objectUri })
40 video = result.video
41 videoCreated = result.created
42 } catch (err) {
43 logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
44 return
45 }
46
47 await sequelizeTypescript.transaction(async t => {
48 // Add share entry
49
50 const share = {
51 actorId: actorAnnouncer.id,
52 videoId: video.id,
53 url: activity.id
54 }
55
56 const [ , created ] = await VideoShareModel.findOrCreate({
57 where: {
58 url: activity.id
59 },
60 defaults: share,
61 transaction: t
62 })
63
64 if (video.isOwned() && created === true) {
65 // Don't resend the activity to the sender
66 const exceptions = [ actorAnnouncer ]
67
68 await forwardVideoRelatedActivity(activity, t, exceptions, video)
69 }
70
71 return undefined
72 })
73
74 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
75 }