]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-announce.ts
Fix user notifications on new follow
[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 import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
12
13 async function processAnnounceActivity (options: APProcessorOptions<ActivityAnnounce>) {
14 const { activity, byActor: actorAnnouncer } = options
15 // Only notify if it is not from a fetcher job
16 const notify = options.fromFetch !== true
17
18 return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity, notify)
19 }
20
21 // ---------------------------------------------------------------------------
22
23 export {
24 processAnnounceActivity
25 }
26
27 // ---------------------------------------------------------------------------
28
29 async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce, notify: boolean) {
30 const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
31
32 let video: VideoModel
33 let videoCreated: boolean
34
35 try {
36 const result = await getOrCreateVideoAndAccountAndChannel({ videoObject: objectUri })
37 video = result.video
38 videoCreated = result.created
39 } catch (err) {
40 logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err })
41 return
42 }
43
44 await sequelizeTypescript.transaction(async t => {
45 // Add share entry
46
47 const share = {
48 actorId: actorAnnouncer.id,
49 videoId: video.id,
50 url: activity.id
51 }
52
53 const [ , created ] = await VideoShareModel.findOrCreate({
54 where: {
55 url: activity.id
56 },
57 defaults: share,
58 transaction: t
59 })
60
61 if (video.isOwned() && created === true) {
62 // Don't resend the activity to the sender
63 const exceptions = [ actorAnnouncer ]
64
65 await forwardVideoRelatedActivity(activity, t, exceptions, video)
66 }
67
68 return undefined
69 })
70
71 if (videoCreated && notify) Notifier.Instance.notifyOnNewVideoIfNeeded(video)
72 }