]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
Don't rehost announced video activities
[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'
50d6de9c 4import { ActorModel } from '../../../models/activitypub/actor'
3fd3ab2d 5import { VideoModel } from '../../../models/video/video'
3fd3ab2d 6import { VideoShareModel } from '../../../models/video/video-share'
50d6de9c 7import { getOrCreateActorAndServerAndModel } from '../actor'
4e50b6a1 8import { forwardActivity } from '../send/misc'
2ccaeeb3 9import { getOrCreateAccountAndVideoAndChannel } from '../videos'
d8465018
C
10
11async function processAnnounceActivity (activity: ActivityAnnounce) {
50d6de9c 12 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
d8465018 13
7acee6f1 14 return processVideoShare(actorAnnouncer, activity)
d8465018
C
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processAnnounceActivity
21}
4e50b6a1
C
22
23// ---------------------------------------------------------------------------
24
50d6de9c 25function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
4e50b6a1 26 const options = {
50d6de9c 27 arguments: [ actorAnnouncer, activity ],
d7a7c248 28 errorMessage: 'Cannot share the video activity with many retries.'
4e50b6a1
C
29 }
30
31 return retryTransactionWrapper(shareVideo, options)
32}
33
2ccaeeb3 34async function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
7acee6f1 35 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2ccaeeb3
C
36 let video: VideoModel
37
7acee6f1
C
38 const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
39 video = res.video
4e50b6a1 40
3fd3ab2d 41 return sequelizeTypescript.transaction(async t => {
4e50b6a1 42 // Add share entry
4e50b6a1
C
43
44 const share = {
50d6de9c 45 actorId: actorAnnouncer.id,
4ba3b8ea
C
46 videoId: video.id,
47 url: activity.id
4e50b6a1
C
48 }
49
3fd3ab2d 50 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
51 where: {
52 url: activity.id
53 },
4e50b6a1
C
54 defaults: share,
55 transaction: t
56 })
57
58 if (video.isOwned() && created === true) {
59 // Don't resend the activity to the sender
50d6de9c 60 const exceptions = [ actorAnnouncer ]
4e50b6a1
C
61 await forwardActivity(activity, t, exceptions)
62 }
63
64 return undefined
65 })
66}