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