]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send-request.ts
Federate video abuses
[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, VideoAbuseObject } from '../../../shared'
12 import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
13 import { getActivityPubUrl } from '../../helpers/activitypub'
14 import { logger } from '../../helpers/logger'
15
16 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
17 const videoChannelObject = videoChannel.toActivityPubObject()
18 const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
19
20 return broadcastToFollowers(data, [ videoChannel.Account ], t)
21 }
22
23 async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
24 const videoChannelObject = videoChannel.toActivityPubObject()
25 const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
26
27 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
28 accountsInvolved.push(videoChannel.Account)
29
30 return broadcastToFollowers(data, accountsInvolved, t)
31 }
32
33 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
34 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
35
36 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
37 accountsInvolved.push(videoChannel.Account)
38
39 return broadcastToFollowers(data, accountsInvolved, t)
40 }
41
42 async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
43 const videoObject = video.toActivityPubObject()
44 const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
45
46 return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
47 }
48
49 async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
50 const videoObject = video.toActivityPubObject()
51 const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
52
53 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
54 accountsInvolved.push(video.VideoChannel.Account)
55
56 return broadcastToFollowers(data, accountsInvolved, t)
57 }
58
59 async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
60 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
61
62 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
63 accountsInvolved.push(video.VideoChannel.Account)
64
65 return broadcastToFollowers(data, accountsInvolved, t)
66 }
67
68 async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
69 const data = await deleteActivityData(account.url, account)
70
71 return broadcastToFollowers(data, [ account ], t)
72 }
73
74 async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
75 const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
76 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), true)
77
78 const data = await announceActivityData(url, byAccount, announcedActivity)
79 return broadcastToFollowers(data, [ byAccount ], t)
80 }
81
82 async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Sequelize.Transaction) {
83 const url = getActivityPubUrl('video', video.uuid) + '#announce'
84
85 const videoChannel = video.VideoChannel
86 const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject(), true)
87
88 const data = await announceActivityData(url, byAccount, announcedActivity)
89 return broadcastToFollowers(data, [ byAccount ], t)
90 }
91
92 async function sendVideoAbuse (
93 fromAccount: AccountInstance,
94 videoAbuse: VideoAbuseInstance,
95 video: VideoInstance,
96 t: Sequelize.Transaction
97 ) {
98 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
99 const data = await createActivityData(url, fromAccount, videoAbuse.toActivityPubObject())
100
101 return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
102 }
103
104 async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
105 const data = await acceptActivityData(fromAccount)
106
107 return unicastTo(data, toAccount.inboxUrl, t)
108 }
109
110 async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
111 const data = await followActivityData(toAccount.url, fromAccount)
112
113 return unicastTo(data, toAccount.inboxUrl, t)
114 }
115
116 // ---------------------------------------------------------------------------
117
118 export {
119 sendCreateVideoChannel,
120 sendUpdateVideoChannel,
121 sendDeleteVideoChannel,
122 sendAddVideo,
123 sendUpdateVideo,
124 sendDeleteVideo,
125 sendDeleteAccount,
126 sendAccept,
127 sendFollow,
128 sendVideoAbuse,
129 sendVideoChannelAnnounce,
130 sendVideoAnnounce
131 }
132
133 // ---------------------------------------------------------------------------
134
135 async function broadcastToFollowers (data: any, toAccountFollowers: AccountInstance[], t: Sequelize.Transaction) {
136 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
137 const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
138 if (result.data.length === 0) {
139 logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
140 return
141 }
142
143 const jobPayload = {
144 uris: result.data,
145 body: data
146 }
147
148 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
149 }
150
151 async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
152 const jobPayload = {
153 uris: [ toAccountUrl ],
154 body: data
155 }
156
157 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
158 }
159
160 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
161 const activity = activityPubContextify(data)
162
163 return signObject(byAccount, activity) as Promise<Activity>
164 }
165
166 async function getPublicActivityTo (account: AccountInstance) {
167 const inboxUrls = await account.getFollowerSharedInboxUrls()
168
169 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
170 }
171
172 async function createActivityData (url: string, byAccount: AccountInstance, object: any, raw = false) {
173 const to = await getPublicActivityTo(byAccount)
174 const base = {
175 type: 'Create',
176 id: url,
177 actor: byAccount.url,
178 to,
179 object
180 }
181
182 if (raw === true) return base
183
184 return buildSignedActivity(byAccount, base)
185 }
186
187 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
188 const to = await getPublicActivityTo(byAccount)
189 const base = {
190 type: 'Update',
191 id: url,
192 actor: byAccount.url,
193 to,
194 object
195 }
196
197 return buildSignedActivity(byAccount, base)
198 }
199
200 async function deleteActivityData (url: string, byAccount: AccountInstance) {
201 const base = {
202 type: 'Delete',
203 id: url,
204 actor: byAccount.url
205 }
206
207 return buildSignedActivity(byAccount, base)
208 }
209
210 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any, raw = false) {
211 const to = await getPublicActivityTo(byAccount)
212 const base = {
213 type: 'Add',
214 id: url,
215 actor: byAccount.url,
216 to,
217 object,
218 target
219 }
220
221 if (raw === true) return base
222
223 return buildSignedActivity(byAccount, base)
224 }
225
226 async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
227 const base = {
228 type: 'Announce',
229 id: url,
230 actor: byAccount.url,
231 object
232 }
233
234 return buildSignedActivity(byAccount, base)
235 }
236
237 async function followActivityData (url: string, byAccount: AccountInstance) {
238 const base = {
239 type: 'Follow',
240 id: byAccount.url,
241 actor: byAccount.url,
242 object: url
243 }
244
245 return buildSignedActivity(byAccount, base)
246 }
247
248 async function acceptActivityData (byAccount: AccountInstance) {
249 const base = {
250 type: 'Accept',
251 id: byAccount.url,
252 actor: byAccount.url
253 }
254
255 return buildSignedActivity(byAccount, base)
256 }