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