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, VideoAbuseObject } 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 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
28 accountsInvolved.push(videoChannel.Account)
30 return broadcastToFollowers(data, accountsInvolved, t)
33 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
34 const data = await deleteActivityData(videoChannel.url, videoChannel.Account)
36 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
37 accountsInvolved.push(videoChannel.Account)
39 return broadcastToFollowers(data, accountsInvolved, t)
42 async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
43 const videoObject = video.toActivityPubObject()
44 const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
46 return broadcastToFollowers(data, [ video.VideoChannel.Account ], t)
49 async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
50 const videoObject = video.toActivityPubObject()
51 const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject)
53 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
54 accountsInvolved.push(video.VideoChannel.Account)
56 return broadcastToFollowers(data, accountsInvolved, t)
59 async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
60 const data = await deleteActivityData(video.url, video.VideoChannel.Account)
62 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
63 accountsInvolved.push(video.VideoChannel.Account)
65 return broadcastToFollowers(data, accountsInvolved, t)
68 async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
69 const data = await deleteActivityData(account.url, account)
71 return broadcastToFollowers(data, [ account ], t)
74 async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
75 const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
76 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), true)
78 const data = await announceActivityData(url, byAccount, announcedActivity)
79 return broadcastToFollowers(data, [ byAccount ], t)
82 async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Sequelize.Transaction) {
83 const url = getActivityPubUrl('video', video.uuid) + '#announce'
85 const videoChannel = video.VideoChannel
86 const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject(), true)
88 const data = await announceActivityData(url, byAccount, announcedActivity)
89 return broadcastToFollowers(data, [ byAccount ], t)
92 async function sendVideoAbuse (
93 fromAccount: AccountInstance,
94 videoAbuse: VideoAbuseInstance,
96 t: Sequelize.Transaction
98 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
99 const data = await createActivityData(url, fromAccount, videoAbuse.toActivityPubObject())
101 return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t)
104 async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
105 const data = await acceptActivityData(fromAccount)
107 return unicastTo(data, toAccount.inboxUrl, t)
110 async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
111 const data = await followActivityData(toAccount.url, fromAccount)
113 return unicastTo(data, toAccount.inboxUrl, t)
116 // ---------------------------------------------------------------------------
119 sendCreateVideoChannel,
120 sendUpdateVideoChannel,
121 sendDeleteVideoChannel,
129 sendVideoChannelAnnounce,
133 // ---------------------------------------------------------------------------
135 async function broadcastToFollowers (data: any, toAccountFollowers: AccountInstance[], t: Sequelize.Transaction) {
136 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
137 const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
138 if (result.data.length === 0) {
139 logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
148 return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
151 async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) {
153 uris: [ toAccountUrl ],
157 return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
160 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
161 const activity = activityPubContextify(data)
163 return signObject(byAccount, activity) as Promise<Activity>
166 async function getPublicActivityTo (account: AccountInstance) {
167 const inboxUrls = await account.getFollowerSharedInboxUrls()
169 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
172 async function createActivityData (url: string, byAccount: AccountInstance, object: any, raw = false) {
173 const to = await getPublicActivityTo(byAccount)
177 actor: byAccount.url,
182 if (raw === true) return base
184 return buildSignedActivity(byAccount, base)
187 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
188 const to = await getPublicActivityTo(byAccount)
192 actor: byAccount.url,
197 return buildSignedActivity(byAccount, base)
200 async function deleteActivityData (url: string, byAccount: AccountInstance) {
207 return buildSignedActivity(byAccount, base)
210 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any, raw = false) {
211 const to = await getPublicActivityTo(byAccount)
215 actor: byAccount.url,
221 if (raw === true) return base
223 return buildSignedActivity(byAccount, base)
226 async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
230 actor: byAccount.url,
234 return buildSignedActivity(byAccount, base)
237 async function followActivityData (url: string, byAccount: AccountInstance) {
241 actor: byAccount.url,
245 return buildSignedActivity(byAccount, base)
248 async function acceptActivityData (byAccount: AccountInstance) {
255 return buildSignedActivity(byAccount, base)