Activity Pub improvements
[oweals/peertube.git] / server / lib / activitypub / send / send-delete.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityDelete } from '../../../../shared/models/activitypub'
3 import { ActorModel } from '../../../models/activitypub/actor'
4 import { VideoModel } from '../../../models/video/video'
5 import { VideoCommentModel } from '../../../models/video/video-comment'
6 import { VideoShareModel } from '../../../models/video/video-share'
7 import { getDeleteActivityPubUrl } from '../url'
8 import { broadcastToFollowers } from './misc'
9
10 async function sendDeleteVideo (video: VideoModel, t: Transaction) {
11   const url = getDeleteActivityPubUrl(video.url)
12   const byActor = video.VideoChannel.Account.Actor
13
14   const data = deleteActivityData(url, video.url, byActor)
15
16   const actorsInvolved = await VideoShareModel.loadActorsByShare(video.id, t)
17   actorsInvolved.push(byActor)
18
19   return broadcastToFollowers(data, byActor, actorsInvolved, t)
20 }
21
22 async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
23   const url = getDeleteActivityPubUrl(byActor.url)
24   const data = deleteActivityData(url, byActor.url, byActor)
25
26   return broadcastToFollowers(data, byActor, [ byActor ], t)
27 }
28
29 async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
30   const url = getDeleteActivityPubUrl(videoComment.url)
31
32   const byActor = videoComment.Account.Actor
33   const data = deleteActivityData(url, videoComment.url, byActor)
34
35   const actorsInvolved = await VideoShareModel.loadActorsByShare(videoComment.Video.id, t)
36   actorsInvolved.push(videoComment.Video.VideoChannel.Account.Actor)
37   actorsInvolved.push(byActor)
38
39   return broadcastToFollowers(data, byActor, actorsInvolved, t)
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   sendDeleteVideo,
46   sendDeleteActor,
47   sendDeleteVideoComment
48 }
49
50 // ---------------------------------------------------------------------------
51
52 function deleteActivityData (url: string, object: string, byActor: ActorModel): ActivityDelete {
53   return {
54     type: 'Delete',
55     id: url,
56     actor: byActor.url,
57     object
58   }
59 }