]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
Remove activitypub helper
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
CommitLineData
7e98a7df 1import { getAPId } from '@server/lib/activitypub/activity'
cb9244de 2import { ActivityAnnounce } from '../../../../shared/models/activitypub'
da854ddd 3import { retryTransactionWrapper } from '../../../helpers/database-utils'
7e98a7df 4import { logger } from '../../../helpers/logger'
80fdaf06 5import { sequelizeTypescript } from '../../../initializers/database'
3fd3ab2d 6import { VideoShareModel } from '../../../models/video/video-share'
26d6bf65
C
7import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
7e98a7df
C
9import { Notifier } from '../../notifier'
10import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
11import { getOrCreateAPVideo } from '../videos'
d8465018 12
1198edf4
C
13async 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
a2f99b54
C
18 // Announces on accounts are not supported
19 if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group') return
20
1198edf4 21 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
d8465018
C
22}
23
24// ---------------------------------------------------------------------------
25
26export {
27 processAnnounceActivity
28}
4e50b6a1
C
29
30// ---------------------------------------------------------------------------
31
453e83ea 32async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) {
a2f99b54 33 const objectUri = getAPId(activity.object)
2ccaeeb3 34
0283eaac 35 let video: MVideoAccountLightBlacklistAllFiles
ee79b60e
C
36 let videoCreated: boolean
37
38 try {
304a84d5 39 const result = await getOrCreateAPVideo({ videoObject: objectUri })
ee79b60e
C
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 }
4e50b6a1 46
cef534ed 47 await sequelizeTypescript.transaction(async t => {
4e50b6a1 48 // Add share entry
4e50b6a1
C
49
50 const share = {
50d6de9c 51 actorId: actorAnnouncer.id,
4ba3b8ea
C
52 videoId: video.id,
53 url: activity.id
4e50b6a1
C
54 }
55
3fd3ab2d 56 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
57 where: {
58 url: activity.id
59 },
4e50b6a1
C
60 defaults: share,
61 transaction: t
62 })
63
64 if (video.isOwned() && created === true) {
65 // Don't resend the activity to the sender
50d6de9c 66 const exceptions = [ actorAnnouncer ]
9588d4f4
C
67
68 await forwardVideoRelatedActivity(activity, t, exceptions, video)
4e50b6a1
C
69 }
70
71 return undefined
72 })
cef534ed 73
1198edf4 74 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
4e50b6a1 75}