Detect posting request in our own inbox
[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 { ActorModel } from '../../../models/activitypub/actor'
4 import { VideoModel } from '../../../models/video/video'
5 import { VideoShareModel } from '../../../models/video/video-share'
6 import { broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience } from './misc'
7
8 async function buildVideoAnnounceToFollowers (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
9   const announcedObject = video.url
10
11   const accountsToForwardView = await getActorsInvolvedInVideo(video, t)
12   const audience = getObjectFollowersAudience(accountsToForwardView)
13   return announceActivityData(videoShare.url, byActor, announcedObject, t, audience)
14 }
15
16 async function sendVideoAnnounceToFollowers (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
17   const data = await buildVideoAnnounceToFollowers(byActor, videoShare, video, t)
18
19   return broadcastToFollowers(data, byActor, [ byActor ], t)
20 }
21
22 async function announceActivityData (
23   url: string,
24   byActor: ActorModel,
25   object: string,
26   t: Transaction,
27   audience?: ActivityAudience
28 ): Promise<ActivityAnnounce> {
29   if (!audience) {
30     audience = await getAudience(byActor, t)
31   }
32
33   return {
34     type: 'Announce',
35     to: audience.to,
36     cc: audience.cc,
37     id: url,
38     actor: byActor.url,
39     object
40   }
41 }
42
43 // ---------------------------------------------------------------------------
44
45 export {
46   sendVideoAnnounceToFollowers,
47   announceActivityData,
48   buildVideoAnnounceToFollowers
49 }