]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-announce.ts
More robust federation
[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 8import { Notifier } from '../../notifier'
ee79b60e
C
9import { VideoModel } from '../../../models/video/video'
10import { logger } from '../../../helpers/logger'
d8465018 11
e587e0ec 12async function processAnnounceActivity (activity: ActivityAnnounce, actorAnnouncer: ActorModel) {
90d4bb81 13 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
d8465018
C
14}
15
16// ---------------------------------------------------------------------------
17
18export {
19 processAnnounceActivity
20}
4e50b6a1
C
21
22// ---------------------------------------------------------------------------
23
90d4bb81 24async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
7acee6f1 25 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
2ccaeeb3 26
ee79b60e
C
27 let video: VideoModel
28 let videoCreated: boolean
29
30 try {
31 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
32 video = result.video
33 videoCreated = result.created
34 } catch (err) {
35 logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
36 return
37 }
4e50b6a1 38
cef534ed 39 await sequelizeTypescript.transaction(async t => {
4e50b6a1 40 // Add share entry
4e50b6a1
C
41
42 const share = {
50d6de9c 43 actorId: actorAnnouncer.id,
4ba3b8ea
C
44 videoId: video.id,
45 url: activity.id
4e50b6a1
C
46 }
47
3fd3ab2d 48 const [ , created ] = await VideoShareModel.findOrCreate({
4ba3b8ea
C
49 where: {
50 url: activity.id
51 },
4e50b6a1
C
52 defaults: share,
53 transaction: t
54 })
55
56 if (video.isOwned() && created === true) {
57 // Don't resend the activity to the sender
50d6de9c 58 const exceptions = [ actorAnnouncer ]
9588d4f4
C
59
60 await forwardVideoRelatedActivity(activity, t, exceptions, video)
4e50b6a1
C
61 }
62
63 return undefined
64 })
cef534ed
C
65
66 if (videoCreated) Notifier.Instance.notifyOnNewVideo(video)
4e50b6a1 67}