]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
CommitLineData
cb9244de 1import { ActivityAnnounce } from '../../../../shared/models/activitypub'
da854ddd 2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3fd3ab2d 3import { sequelizeTypescript } from '../../../initializers'
3fd3ab2d 4import { VideoShareModel } from '../../../models/video/video-share'
9588d4f4 5import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 6import { getOrCreateVideoAndAccountAndChannel } from '../videos'
cef534ed 7import { Notifier } from '../../notifier'
ee79b60e
C
8import { VideoModel } from '../../../models/video/video'
9import { logger } from '../../../helpers/logger'
1198edf4 10import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
5224c394 11import { SignatureActorModel } from '../../../typings/models'
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
18 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
d8465018
C
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 processAnnounceActivity
25}
4e50b6a1
C
26
27// ---------------------------------------------------------------------------
28
5224c394 29async function processVideoShare (actorAnnouncer: SignatureActorModel, activity: ActivityAnnounce, notify: boolean) {
7acee6f1 30 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2ccaeeb3 31
ee79b60e
C
32 let video: VideoModel
33 let videoCreated: boolean
34
35 try {
36 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
37 video = result.video
38 videoCreated = result.created
39 } catch (err) {
40 logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
41 return
42 }
4e50b6a1 43
cef534ed 44 await sequelizeTypescript.transaction(async t => {
4e50b6a1 45 // Add share entry
4e50b6a1
C
46
47 const share = {
50d6de9c 48 actorId: actorAnnouncer.id,
4ba3b8ea
C
49 videoId: video.id,
50 url: activity.id
4e50b6a1
C
51 }
52
3fd3ab2d 53 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
54 where: {
55 url: activity.id
56 },
4e50b6a1
C
57 defaults: share,
58 transaction: t
59 })
60
61 if (video.isOwned() && created === true) {
62 // Don't resend the activity to the sender
50d6de9c 63 const exceptions = [ actorAnnouncer ]
9588d4f4
C
64
65 await forwardVideoRelatedActivity(activity, t, exceptions, video)
4e50b6a1
C
66 }
67
68 return undefined
69 })
cef534ed 70
1198edf4 71 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
4e50b6a1 72}