]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-announce.ts
Flat shared module directory
[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 { VideoModel } from '../../../models/video/video'
6import { VideoShareModel } from '../../../models/video/video-share'
7import { getOrCreateActorAndServerAndModel } from '../actor'
8import { forwardVideoRelatedActivity } from '../send/utils'
9import { getOrCreateAccountAndVideoAndChannel } from '../videos'
10
11async function processAnnounceActivity (activity: ActivityAnnounce) {
12 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
13
14 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
15}
16
17// ---------------------------------------------------------------------------
18
19export {
20 processAnnounceActivity
21}
22
23// ---------------------------------------------------------------------------
24
25async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
26 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
27 let video: VideoModel
28
29 const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
30 video = res.video
31
32 return sequelizeTypescript.transaction(async t => {
33 // Add share entry
34
35 const share = {
36 actorId: actorAnnouncer.id,
37 videoId: video.id,
38 url: activity.id
39 }
40
41 const [ , created ] = await VideoShareModel.findOrCreate({
42 where: {
43 url: activity.id
44 },
45 defaults: share,
46 transaction: t
47 })
48
49 if (video.isOwned() && created === true) {
50 // Don't resend the activity to the sender
51 const exceptions = [ actorAnnouncer ]
52
53 await forwardVideoRelatedActivity(activity, t, exceptions, video)
54 }
55
56 return undefined
57 })
58}