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