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