d03b358f1a4ff3b5df2ce279863fa345e792b0bf
[oweals/peertube.git] / server / lib / activitypub / send / send-announce.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAnnounce, ActivityAudience } from '../../../../shared/models/activitypub'
3 import { broadcastToFollowers } from './utils'
4 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf } from '../audience'
5 import { logger } from '../../../helpers/logger'
6 import { MActorLight, MVideo } from '../../../typings/models'
7 import { MVideoShare } from '../../../typings/models/video'
8
9 async function buildAnnounceWithVideoAudience (
10   byActor: MActorLight,
11   videoShare: MVideoShare,
12   video: MVideo,
13   t: Transaction
14 ) {
15   const announcedObject = video.url
16
17   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
18   const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
19
20   const activity = buildAnnounceActivity(videoShare.url, byActor, announcedObject, audience)
21
22   return { activity, actorsInvolvedInVideo }
23 }
24
25 async function sendVideoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
26   const { activity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
27
28   logger.info('Creating job to send announce %s.', videoShare.url)
29
30   const followersException = [ byActor ]
31   return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException, 'Announce')
32 }
33
34 function buildAnnounceActivity (url: string, byActor: MActorLight, object: string, audience?: ActivityAudience): ActivityAnnounce {
35   if (!audience) audience = getAudience(byActor)
36
37   return audiencify({
38     type: 'Announce' as 'Announce',
39     id: url,
40     actor: byActor.url,
41     object
42   }, audience)
43 }
44
45 // ---------------------------------------------------------------------------
46
47 export {
48   sendVideoAnnounce,
49   buildAnnounceActivity,
50   buildAnnounceWithVideoAudience
51 }