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