aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send-request.ts
blob: d5d07011ab0d36eea214b68eabd59f4f6cca8c20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as Sequelize from 'sequelize'

import { database as db } from '../../initializers'
import {
  AccountInstance,
  VideoInstance,
  VideoChannelInstance
} from '../../models'
import { httpRequestJobScheduler } from '../jobs'
import { signObject, activityPubContextify } from '../../helpers'
import { Activity } from '../../../shared'
import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
import { getActivityPubUrl } from '../../helpers/activitypub'
import { logger } from '../../helpers/logger'

async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
  const videoChannelObject = videoChannel.toActivityPubObject()
  const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)

  return broadcastToFollowers(data, [ videoChannel.Account ], t)
}

async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
  const videoChannelObject = videoChannel.toActivityPubObject()
  const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)

  const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
  accountsInvolved.push(videoChannel.Account)

  return broadcastToFollowers(data, accountsInvolved, t)
}

async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
  const data = await deleteActivityData(videoChannel.url, videoChannel.Account)

  const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
  accountsInvolved.push(videoChannel.Account)

  return broadcastToFollowers(data, accountsInvolved, t)
}

async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
  const videoObject = video.toActivityPubObject()
  const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)

  return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
}

async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
  const videoObject = video.toActivityPubObject()
  const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)

  const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
  accountsInvolved.push(video.VideoChannel.Account)

  return broadcastToFollowers(data, accountsInvolved, t)
}

async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
  const data = await deleteActivityData(video.url, video.VideoChannel.Account)

  const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
  accountsInvolved.push(video.VideoChannel.Account)

  return broadcastToFollowers(data, accountsInvolved, t)
}

async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
  const data = await deleteActivityData(account.url, account)

  return broadcastToFollowers(data, [ account ], t)
}

async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
  const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
  const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), true)

  const data = await announceActivityData(url, byAccount, announcedActivity)
  return broadcastToFollowers(data, [ byAccount ], t)
}

async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Sequelize.Transaction) {
  const url = getActivityPubUrl('video', video.uuid) + '#announce'

  const videoChannel = video.VideoChannel
  const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject(), true)

  const data = await announceActivityData(url, byAccount, announcedActivity)
  return broadcastToFollowers(data, [ byAccount ], t)
}

async function sendVideoAbuse (
  fromAccount: AccountInstance,
  videoAbuse: VideoAbuseInstance,
  video: VideoInstance,
  t: Sequelize.Transaction
) {
  const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
  const data = await createActivityData(url, fromAccount, video.url)

  return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
}

async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
  const data = await acceptActivityData(fromAccount)

  return unicastTo(data, toAccount.inboxUrl, t)
}

async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
  const data = await followActivityData(toAccount.url, fromAccount)

  return unicastTo(data, toAccount.inboxUrl, t)
}

// ---------------------------------------------------------------------------

export {
  sendCreateVideoChannel,
  sendUpdateVideoChannel,
  sendDeleteVideoChannel,
  sendAddVideo,
  sendUpdateVideo,
  sendDeleteVideo,
  sendDeleteAccount,
  sendAccept,
  sendFollow,
  sendVideoAbuse,
  sendVideoChannelAnnounce,
  sendVideoAnnounce
}

// ---------------------------------------------------------------------------

async function broadcastToFollowers (data: any, toAccountFollowers: AccountInstance[], t: Sequelize.Transaction) {
  const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
  const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
  if (result.data.length === 0) {
    logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
    return
  }

  const jobPayload = {
    uris: result.data,
    body: data
  }

  return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
}

async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
  const jobPayload = {
    uris: [ toAccountUrl ],
    body: data
  }

  return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
}

function buildSignedActivity (byAccount: AccountInstance, data: Object) {
  const activity = activityPubContextify(data)

  return signObject(byAccount, activity) as Promise<Activity>
}

async function getPublicActivityTo (account: AccountInstance) {
  const inboxUrls = await account.getFollowerSharedInboxUrls()

  return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
}

async function createActivityData (url: string, byAccount: AccountInstance, object: any, raw = false) {
  const to = await getPublicActivityTo(byAccount)
  const base = {
    type: 'Create',
    id: url,
    actor: byAccount.url,
    to,
    object
  }

  if (raw === true) return base

  return buildSignedActivity(byAccount, base)
}

async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
  const to = await getPublicActivityTo(byAccount)
  const base = {
    type: 'Update',
    id: url,
    actor: byAccount.url,
    to,
    object
  }

  return buildSignedActivity(byAccount, base)
}

async function deleteActivityData (url: string, byAccount: AccountInstance) {
  const base = {
    type: 'Delete',
    id: url,
    actor: byAccount.url
  }

  return buildSignedActivity(byAccount, base)
}

async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any, raw = false) {
  const to = await getPublicActivityTo(byAccount)
  const base = {
    type: 'Add',
    id: url,
    actor: byAccount.url,
    to,
    object,
    target
  }

  if (raw === true) return base

  return buildSignedActivity(byAccount, base)
}

async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
  const base = {
    type: 'Announce',
    id: url,
    actor: byAccount.url,
    object
  }

  return buildSignedActivity(byAccount, base)
}

async function followActivityData (url: string, byAccount: AccountInstance) {
  const base = {
    type: 'Follow',
    id: byAccount.url,
    actor: byAccount.url,
    object: url
  }

  return buildSignedActivity(byAccount, base)
}

async function acceptActivityData (byAccount: AccountInstance) {
  const base = {
    type: 'Accept',
    id: byAccount.url,
    actor: byAccount.url
  }

  return buildSignedActivity(byAccount, base)
}