0a45ea10f3054a01e55144172397801309f4f1de
[oweals/peertube.git] / server / lib / activitypub / send / send-delete.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityDelete } from '../../../../shared/models/activitypub'
3 import { AccountModel } from '../../../models/account/account'
4 import { VideoModel } from '../../../models/video/video'
5 import { VideoChannelModel } from '../../../models/video/video-channel'
6 import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
7 import { VideoShareModel } from '../../../models/video/video-share'
8 import { broadcastToFollowers } from './misc'
9
10 async function sendDeleteVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
11   const byAccount = videoChannel.Account
12
13   const data = deleteActivityData(videoChannel.url, byAccount)
14
15   const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
16   accountsInvolved.push(byAccount)
17
18   return broadcastToFollowers(data, byAccount, accountsInvolved, t)
19 }
20
21 async function sendDeleteVideo (video: VideoModel, t: Transaction) {
22   const byAccount = video.VideoChannel.Account
23
24   const data = deleteActivityData(video.url, byAccount)
25
26   const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t)
27   accountsInvolved.push(byAccount)
28
29   return broadcastToFollowers(data, byAccount, accountsInvolved, t)
30 }
31
32 async function sendDeleteAccount (account: AccountModel, t: Transaction) {
33   const data = deleteActivityData(account.url, account)
34
35   return broadcastToFollowers(data, account, [ account ], t)
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41   sendDeleteVideoChannel,
42   sendDeleteVideo,
43   sendDeleteAccount
44 }
45
46 // ---------------------------------------------------------------------------
47
48 function deleteActivityData (url: string, byAccount: AccountModel): ActivityDelete {
49   return {
50     type: 'Delete',
51     id: url,
52     actor: byAccount.url
53   }
54 }