]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send-request.ts
Make it compile at least
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send-request.ts
1 import * as Sequelize from 'sequelize'
2
3 import { database as db } from '../../initializers'
4 import {
5 AccountInstance,
6 VideoInstance,
7 VideoChannelInstance
8 } from '../../models'
9 import { httpRequestJobScheduler } from '../jobs'
10 import { signObject, activityPubContextify } from '../../helpers'
11 import { Activity } from '../../../shared'
12
13 function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
14 const videoChannelObject = videoChannel.toActivityPubObject()
15 const data = createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
16
17 return broadcastToFollowers(data, videoChannel.Account, t)
18 }
19
20 function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
21 const videoChannelObject = videoChannel.toActivityPubObject()
22 const data = updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
23
24 return broadcastToFollowers(data, videoChannel.Account, t)
25 }
26
27 function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
28 const videoChannelObject = videoChannel.toActivityPubObject()
29 const data = deleteActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
30
31 return broadcastToFollowers(data, videoChannel.Account, t)
32 }
33
34 function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
35 const videoObject = video.toActivityPubObject()
36 const data = addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
37
38 return broadcastToFollowers(data, video.VideoChannel.Account, t)
39 }
40
41 function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
42 const videoObject = video.toActivityPubObject()
43 const data = updateActivityData(video.url, video.VideoChannel.Account, videoObject)
44
45 return broadcastToFollowers(data, video.VideoChannel.Account, t)
46 }
47
48 function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
49 const videoObject = video.toActivityPubObject()
50 const data = deleteActivityData(video.url, video.VideoChannel.Account, videoObject)
51
52 return broadcastToFollowers(data, video.VideoChannel.Account, t)
53 }
54
55 // ---------------------------------------------------------------------------
56
57 export {
58 sendCreateVideoChannel,
59 sendUpdateVideoChannel,
60 sendDeleteVideoChannel,
61 sendAddVideo,
62 sendUpdateVideo,
63 sendDeleteVideo
64 }
65
66 // ---------------------------------------------------------------------------
67
68 async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
69 const result = await db.Account.listFollowerUrlsForApi(fromAccount.name, 0)
70
71 const jobPayload = {
72 uris: result.data,
73 body: data
74 }
75
76 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
77 }
78
79 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
80 const activity = activityPubContextify(data)
81
82 return signObject(byAccount, activity) as Promise<Activity>
83 }
84
85 async function getPublicActivityTo (account: AccountInstance) {
86 const inboxUrls = await account.getFollowerSharedInboxUrls()
87
88 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
89 }
90
91 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
92 const to = await getPublicActivityTo(byAccount)
93 const base = {
94 type: 'Create',
95 id: url,
96 actor: byAccount.url,
97 to,
98 object
99 }
100
101 return buildSignedActivity(byAccount, base)
102 }
103
104 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
105 const to = await getPublicActivityTo(byAccount)
106 const base = {
107 type: 'Update',
108 id: url,
109 actor: byAccount.url,
110 to,
111 object
112 }
113
114 return buildSignedActivity(byAccount, base)
115 }
116
117 async function deleteActivityData (url: string, byAccount: AccountInstance, object: any) {
118 const to = await getPublicActivityTo(byAccount)
119 const base = {
120 type: 'Update',
121 id: url,
122 actor: byAccount.url,
123 to,
124 object
125 }
126
127 return buildSignedActivity(byAccount, base)
128 }
129
130 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
131 const to = await getPublicActivityTo(byAccount)
132 const base = {
133 type: 'Add',
134 id: url,
135 actor: byAccount.url,
136 to,
137 object,
138 target
139 }
140
141 return buildSignedActivity(byAccount, base)
142 }