]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-announce.ts
Merge branch 'release/v1.2.0'
[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 { ActorModel } from '../../../models/activitypub/actor'
5import { VideoShareModel } from '../../../models/video/video-share'
6import { forwardVideoRelatedActivity } from '../send/utils'
7import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8import { VideoPrivacy } from '../../../../shared/models/videos'
9import { Notifier } from '../../notifier'
10
11async function processAnnounceActivity (activity: ActivityAnnounce, actorAnnouncer: ActorModel) {
12 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
13}
14
15// ---------------------------------------------------------------------------
16
17export {
18 processAnnounceActivity
19}
20
21// ---------------------------------------------------------------------------
22
23async 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}