]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-announce.ts
Merge branch 'release/v1.2.0'
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
1 import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoShareModel } from '../../../models/video/video-share'
6 import { forwardVideoRelatedActivity } from '../send/utils'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { VideoPrivacy } from '../../../../shared/models/videos'
9 import { Notifier } from '../../notifier'
10
11 async function processAnnounceActivity (activity: ActivityAnnounce, actorAnnouncer: ActorModel) {
12 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
13 }
14
15 // ---------------------------------------------------------------------------
16
17 export {
18 processAnnounceActivity
19 }
20
21 // ---------------------------------------------------------------------------
22
23 async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
24 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
25
26 const { video, created: videoCreated } = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
27
28 await sequelizeTypescript.transaction(async t => {
29 // Add share entry
30
31 const share = {
32 actorId: actorAnnouncer.id,
33 videoId: video.id,
34 url: activity.id
35 }
36
37 const [ , created ] = await VideoShareModel.findOrCreate({
38 where: {
39 url: activity.id
40 },
41 defaults: share,
42 transaction: t
43 })
44
45 if (video.isOwned() && created === true) {
46 // Don't resend the activity to the sender
47 const exceptions = [ actorAnnouncer ]
48
49 await forwardVideoRelatedActivity(activity, t, exceptions, video)
50 }
51
52 return undefined
53 })
54
55 if (videoCreated) Notifier.Instance.notifyOnNewVideo(video)
56 }