]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
Flat shared module directory
[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'
2ccaeeb3 9import { getOrCreateAccountAndVideoAndChannel } 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
C
27 let video: VideoModel
28
7acee6f1
C
29 const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
30 video = res.video
4e50b6a1 31
3fd3ab2d 32 return sequelizeTypescript.transaction(async t => {
4e50b6a1 33 // Add share entry
4e50b6a1
C
34
35 const share = {
50d6de9c 36 actorId: actorAnnouncer.id,
4ba3b8ea
C
37 videoId: video.id,
38 url: activity.id
4e50b6a1
C
39 }
40
3fd3ab2d 41 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
42 where: {
43 url: activity.id
44 },
4e50b6a1
C
45 defaults: share,
46 transaction: t
47 })
48
49 if (video.isOwned() && created === true) {
50 // Don't resend the activity to the sender
50d6de9c 51 const exceptions = [ actorAnnouncer ]
9588d4f4
C
52
53 await forwardVideoRelatedActivity(activity, t, exceptions, video)
4e50b6a1
C
54 }
55
56 return undefined
57 })
58}