Correctly forward like/dislikes and undo
[oweals/peertube.git] / server / lib / activitypub / send / send-add.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityAdd } from '../../../../shared/models/activitypub/activity'
3 import { VideoPrivacy } from '../../../../shared/models/videos/video-privacy.enum'
4 import { AccountInstance, VideoInstance } from '../../../models'
5 import { broadcastToFollowers, getAudience } from './misc'
6
7 async function sendAddVideo (video: VideoInstance, t: Transaction) {
8   const byAccount = video.VideoChannel.Account
9
10   const videoObject = video.toActivityPubObject()
11   const data = await addActivityData(video.url, byAccount, video, video.VideoChannel.url, videoObject)
12
13   return broadcastToFollowers(data, byAccount, [ byAccount ], t)
14 }
15
16 async function addActivityData (url: string, byAccount: AccountInstance, video: VideoInstance, target: string, object: any) {
17   const videoPublic = video.privacy === VideoPrivacy.PUBLIC
18
19   const { to, cc } = await getAudience(byAccount, videoPublic)
20   const activity: ActivityAdd = {
21     type: 'Add',
22     id: url,
23     actor: byAccount.url,
24     to,
25     cc,
26     object,
27     target
28   }
29
30   return activity
31 }
32
33 // ---------------------------------------------------------------------------
34
35 export {
36   addActivityData,
37   sendAddVideo
38 }