]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
Bumped to version v1.0.0-beta.13
[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'
9588d4f4 8import { forwardVideoRelatedActivity } from '../send/utils'
1297eb5d 9import { getOrCreateVideoAndAccountAndChannel } from '../videos'
d8465018
C
10
11async function processAnnounceActivity (activity: ActivityAnnounce) {
50d6de9c 12 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
d8465018 13
90d4bb81 14 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
d8465018
C
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processAnnounceActivity
21}
4e50b6a1
C
22
23// ---------------------------------------------------------------------------
24
90d4bb81 25async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
7acee6f1 26 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2ccaeeb3 27
1297eb5d 28 const { video } = await getOrCreateVideoAndAccountAndChannel(objectUri)
4e50b6a1 29
3fd3ab2d 30 return sequelizeTypescript.transaction(async t => {
4e50b6a1 31 // Add share entry
4e50b6a1
C
32
33 const share = {
50d6de9c 34 actorId: actorAnnouncer.id,
4ba3b8ea
C
35 videoId: video.id,
36 url: activity.id
4e50b6a1
C
37 }
38
3fd3ab2d 39 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
40 where: {
41 url: activity.id
42 },
4e50b6a1
C
43 defaults: share,
44 transaction: t
45 })
46
47 if (video.isOwned() && created === true) {
48 // Don't resend the activity to the sender
50d6de9c 49 const exceptions = [ actorAnnouncer ]
9588d4f4
C
50
51 await forwardVideoRelatedActivity(activity, t, exceptions, video)
4e50b6a1
C
52 }
53
54 return undefined
55 })
56}