]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/send-request.ts
1dad51828b15085f76dc8f91d767a3d0b221af96
[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 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 return broadcastToFollowers(data, videoChannel.Account, t)
28 }
29
30 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
31 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
32
33 return broadcastToFollowers(data, videoChannel.Account, t)
34 }
35
36 async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
37 const videoObject = video.toActivityPubObject()
38 const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
39
40 return broadcastToFollowers(data, video.VideoChannel.Account, t)
41 }
42
43 async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
44 const videoObject = video.toActivityPubObject()
45 const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
46
47 return broadcastToFollowers(data, video.VideoChannel.Account, t)
48 }
49
50 async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
51 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
52
53 return broadcastToFollowers(data, video.VideoChannel.Account, t)
54 }
55
56 async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
57 const data = await deleteActivityData(account.url, account)
58
59 return broadcastToFollowers(data, account, t)
60 }
61
62 async function sendVideoAbuse (
63 fromAccount: AccountInstance,
64 videoAbuse: VideoAbuseInstance,
65 video: VideoInstance,
66 t: Sequelize.Transaction
67 ) {
68 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
69 const data = await createActivityData(url, fromAccount, video.url)
70
71 return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
72 }
73
74 async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
75 const data = await acceptActivityData(fromAccount)
76
77 return unicastTo(data, toAccount.inboxUrl, t)
78 }
79
80 async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
81 const data = await followActivityData(toAccount.url, fromAccount)
82
83 return unicastTo(data, toAccount.inboxUrl, t)
84 }
85
86 // ---------------------------------------------------------------------------
87
88 export {
89 sendCreateVideoChannel,
90 sendUpdateVideoChannel,
91 sendDeleteVideoChannel,
92 sendAddVideo,
93 sendUpdateVideo,
94 sendDeleteVideo,
95 sendDeleteAccount,
96 sendAccept,
97 sendFollow,
98 sendVideoAbuse
99 }
100
101 // ---------------------------------------------------------------------------
102
103 async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
104 const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(fromAccount.id)
105 if (result.data.length === 0) {
106 logger.info('Not broadcast because of 0 followers.')
107 return
108 }
109
110 const jobPayload = {
111 uris: result.data,
112 body: data
113 }
114
115 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
116 }
117
118 async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
119 const jobPayload = {
120 uris: [ toAccountUrl ],
121 body: data
122 }
123
124 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
125 }
126
127 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
128 const activity = activityPubContextify(data)
129
130 return signObject(byAccount, activity) as Promise<Activity>
131 }
132
133 async function getPublicActivityTo (account: AccountInstance) {
134 const inboxUrls = await account.getFollowerSharedInboxUrls()
135
136 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
137 }
138
139 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
140 const to = await getPublicActivityTo(byAccount)
141 const base = {
142 type: 'Create',
143 id: url,
144 actor: byAccount.url,
145 to,
146 object
147 }
148
149 return buildSignedActivity(byAccount, base)
150 }
151
152 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
153 const to = await getPublicActivityTo(byAccount)
154 const base = {
155 type: 'Update',
156 id: url,
157 actor: byAccount.url,
158 to,
159 object
160 }
161
162 return buildSignedActivity(byAccount, base)
163 }
164
165 async function deleteActivityData (url: string, byAccount: AccountInstance) {
166 const base = {
167 type: 'Delete',
168 id: url,
169 actor: byAccount.url
170 }
171
172 return buildSignedActivity(byAccount, base)
173 }
174
175 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
176 const to = await getPublicActivityTo(byAccount)
177 const base = {
178 type: 'Add',
179 id: url,
180 actor: byAccount.url,
181 to,
182 object,
183 target
184 }
185
186 return buildSignedActivity(byAccount, base)
187 }
188
189 async function followActivityData (url: string, byAccount: AccountInstance) {
190 const base = {
191 type: 'Follow',
192 id: byAccount.url,
193 actor: byAccount.url,
194 object: url
195 }
196
197 return buildSignedActivity(byAccount, base)
198 }
199
200 async function acceptActivityData (byAccount: AccountInstance) {
201 const base = {
202 type: 'Accept',
203 id: byAccount.url,
204 actor: byAccount.url
205 }
206
207 return buildSignedActivity(byAccount, base)
208 }