1 import * as Sequelize from 'sequelize'
3 import { database as db } from '../../initializers'
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'
16 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
17 const videoChannelObject = videoChannel.toActivityPubObject()
18 const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
20 return broadcastToFollowers(data, [ videoChannel.Account ], t)
23 async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
24 const videoChannelObject = videoChannel.toActivityPubObject()
25 const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
27 return broadcastToFollowers(data, [ videoChannel.Account ], t)
30 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
31 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
33 return broadcastToFollowers(data, [ videoChannel.Account ], t)
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)
40 return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
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)
47 return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
50 async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
51 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
53 return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
56 async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
57 const data = await deleteActivityData(account.url, account)
59 return broadcastToFollowers(data, [ account ], t)
62 async 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)
66 const data = await announceActivityData(url, byAccount, announcedActivity)
67 return broadcastToFollowers(data, [ byAccount ], t)
70 async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Sequelize.Transaction) {
71 const url = getActivityPubUrl('video', video.uuid) + '#announce'
73 const videoChannel = video.VideoChannel
74 const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject(), true)
76 const data = await announceActivityData(url, byAccount, announcedActivity)
77 return broadcastToFollowers(data, [ byAccount ], t)
80 async function sendVideoAbuse (
81 fromAccount: AccountInstance,
82 videoAbuse: VideoAbuseInstance,
84 t: Sequelize.Transaction
86 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
87 const data = await createActivityData(url, fromAccount, video.url)
89 return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
92 async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
93 const data = await acceptActivityData(fromAccount)
95 return unicastTo(data, toAccount.inboxUrl, t)
98 async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
99 const data = await followActivityData(toAccount.url, fromAccount)
101 return unicastTo(data, toAccount.inboxUrl, t)
104 // ---------------------------------------------------------------------------
107 sendCreateVideoChannel,
108 sendUpdateVideoChannel,
109 sendDeleteVideoChannel,
117 sendVideoChannelAnnounce,
121 // ---------------------------------------------------------------------------
123 async 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)
126 if (result.data.length === 0) {
127 logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
136 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
139 async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
141 uris: [ toAccountUrl ],
145 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
148 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
149 const activity = activityPubContextify(data)
151 return signObject(byAccount, activity) as Promise<Activity>
154 async function getPublicActivityTo (account: AccountInstance) {
155 const inboxUrls = await account.getFollowerSharedInboxUrls()
157 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
160 async function createActivityData (url: string, byAccount: AccountInstance, object: any, raw = false) {
161 const to = await getPublicActivityTo(byAccount)
165 actor: byAccount.url,
170 if (raw === true) return base
172 return buildSignedActivity(byAccount, base)
175 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
176 const to = await getPublicActivityTo(byAccount)
180 actor: byAccount.url,
185 return buildSignedActivity(byAccount, base)
188 async function deleteActivityData (url: string, byAccount: AccountInstance) {
195 return buildSignedActivity(byAccount, base)
198 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any, raw = false) {
199 const to = await getPublicActivityTo(byAccount)
203 actor: byAccount.url,
209 if (raw === true) return base
211 return buildSignedActivity(byAccount, base)
214 async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
218 actor: byAccount.url,
222 return buildSignedActivity(byAccount, base)
225 async function followActivityData (url: string, byAccount: AccountInstance) {
229 actor: byAccount.url,
233 return buildSignedActivity(byAccount, base)
236 async function acceptActivityData (byAccount: AccountInstance) {
243 return buildSignedActivity(byAccount, base)