]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-announce.ts
Fix various typos
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
... / ...
CommitLineData
1import { getAPId } from '@server/lib/activitypub/activity'
2import { ActivityAnnounce } from '../../../../shared/models/activitypub'
3import { retryTransactionWrapper } from '../../../helpers/database-utils'
4import { logger } from '../../../helpers/logger'
5import { sequelizeTypescript } from '../../../initializers/database'
6import { VideoShareModel } from '../../../models/video/video-share'
7import { APProcessorOptions } from '../../../types/activitypub-processor.model'
8import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../types/models'
9import { Notifier } from '../../notifier'
10import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
11import { getOrCreateAPVideo } from '../videos'
12
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
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
26export {
27 processAnnounceActivity
28}
29
30// ---------------------------------------------------------------------------
31
32async 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}