]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-announce.ts
Correctly notify on auto blacklist
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
1 import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2 import { retryTransactionWrapper } from '../../../helpers/database-utils'
3 import { sequelizeTypescript } from '../../../initializers'
4 import { ActorModel } from '../../../models/activitypub/actor'
5 import { VideoShareModel } from '../../../models/video/video-share'
6 import { forwardVideoRelatedActivity } from '../send/utils'
7 import { getOrCreateVideoAndAccountAndChannel } from '../videos'
8 import { Notifier } from '../../notifier'
9 import { VideoModel } from '../../../models/video/video'
10 import { logger } from '../../../helpers/logger'
11
12 async function processAnnounceActivity (activity: ActivityAnnounce, actorAnnouncer: ActorModel) {
13 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
14 }
15
16 // ---------------------------------------------------------------------------
17
18 export {
19 processAnnounceActivity
20 }
21
22 // ---------------------------------------------------------------------------
23
24 async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
25 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
26
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 }
38
39 await sequelizeTypescript.transaction(async t => {
40 // Add share entry
41
42 const share = {
43 actorId: actorAnnouncer.id,
44 videoId: video.id,
45 url: activity.id
46 }
47
48 const [ , created ] = await VideoShareModel.findOrCreate({
49 where: {
50 url: activity.id
51 },
52 defaults: share,
53 transaction: t
54 })
55
56 if (video.isOwned() && created === true) {
57 // Don't resend the activity to the sender
58 const exceptions = [ actorAnnouncer ]
59
60 await forwardVideoRelatedActivity(activity, t, exceptions, video)
61 }
62
63 return undefined
64 })
65
66 if (videoCreated) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
67 }