]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { sequelizeTypescript } from '../../../initializers'
4import { VideoShareModel } from '../../../models/video/video-share'
5import { forwardVideoRelatedActivity } from '../send/utils'
6import { getOrCreateVideoAndAccountAndChannel } from '../videos'
7import { Notifier } from '../../notifier'
8import { VideoModel } from '../../../models/video/video'
9import { logger } from '../../../helpers/logger'
10import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
11import { SignatureActorModel } from '../../../typings/models'
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 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 processAnnounceActivity
25}
26
27// ---------------------------------------------------------------------------
28
29async function processVideoShare (actorAnnouncer: SignatureActorModel, activity: ActivityAnnounce, notify: boolean) {
30 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
31
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 }
43
44 await sequelizeTypescript.transaction(async t => {
45 // Add share entry
46
47 const share = {
48 actorId: actorAnnouncer.id,
49 videoId: video.id,
50 url: activity.id
51 }
52
53 const [ , created ] = await VideoShareModel.findOrCreate({
54 where: {
55 url: activity.id
56 },
57 defaults: share,
58 transaction: t
59 })
60
61 if (video.isOwned() && created === true) {
62 // Don't resend the activity to the sender
63 const exceptions = [ actorAnnouncer ]
64
65 await forwardVideoRelatedActivity(activity, t, exceptions, video)
66 }
67
68 return undefined
69 })
70
71 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
72}