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