1 import { Transaction } from 'sequelize'
9 } from '../../../shared/models/activitypub/activity'
10 import { getActivityPubUrl } from '../../helpers/activitypub'
11 import { logger } from '../../helpers/logger'
12 import { database as db } from '../../initializers'
13 import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../models'
14 import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
15 import { activitypubHttpJobScheduler } from '../jobs'
17 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
18 const byAccount = videoChannel.Account
20 const videoChannelObject = videoChannel.toActivityPubObject()
21 const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
23 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
26 async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
27 const byAccount = videoChannel.Account
29 const videoChannelObject = videoChannel.toActivityPubObject()
30 const data = await updateActivityData(videoChannel.url, byAccount, videoChannelObject)
32 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
33 accountsInvolved.push(byAccount)
35 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
38 async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
39 const byAccount = videoChannel.Account
41 const data = await deleteActivityData(videoChannel.url, byAccount)
43 const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
44 accountsInvolved.push(byAccount)
46 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
49 async function sendAddVideo (video: VideoInstance, t: Transaction) {
50 const byAccount = video.VideoChannel.Account
52 const videoObject = video.toActivityPubObject()
53 const data = await addActivityData(video.url, byAccount, video.VideoChannel.url, videoObject)
55 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
58 async function sendUpdateVideo (video: VideoInstance, t: Transaction) {
59 const byAccount = video.VideoChannel.Account
61 const videoObject = video.toActivityPubObject()
62 const data = await updateActivityData(video.url, byAccount, videoObject)
64 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
65 accountsInvolved.push(byAccount)
67 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
70 async function sendDeleteVideo (video: VideoInstance, t: Transaction) {
71 const byAccount = video.VideoChannel.Account
73 const data = await deleteActivityData(video.url, byAccount)
75 const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
76 accountsInvolved.push(byAccount)
78 return broadcastToFollowers(data, byAccount, accountsInvolved, t)
81 async function sendDeleteAccount (account: AccountInstance, t: Transaction) {
82 const data = await deleteActivityData(account.url, account)
84 return broadcastToFollowers(data, account, [ account ], t)
87 async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) {
88 const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
89 const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject())
91 const data = await announceActivityData(url, byAccount, announcedActivity)
92 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
95 async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
96 const url = getActivityPubUrl('video', video.uuid) + '#announce'
98 const videoChannel = video.VideoChannel
99 const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject())
101 const data = await announceActivityData(url, byAccount, announcedActivity)
102 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
105 async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
106 const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
107 const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject())
109 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
112 async function sendAccept (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
113 const data = await acceptActivityData(byAccount)
115 return unicastTo(data, byAccount, toAccount.inboxUrl, t)
118 async function sendFollow (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
119 const data = await followActivityData(toAccount.url, byAccount)
121 return unicastTo(data, byAccount, toAccount.inboxUrl, t)
124 // ---------------------------------------------------------------------------
127 sendCreateVideoChannel,
128 sendUpdateVideoChannel,
129 sendDeleteVideoChannel,
137 sendVideoChannelAnnounce,
141 // ---------------------------------------------------------------------------
143 async function broadcastToFollowers (data: any, byAccount: AccountInstance, toAccountFollowers: AccountInstance[], t: Transaction) {
144 const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
145 const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
146 if (result.data.length === 0) {
147 logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
153 signatureAccountId: byAccount.id,
157 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
160 async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) {
162 uris: [ toAccountUrl ],
163 signatureAccountId: byAccount.id,
167 return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
170 async function getPublicActivityTo (account: AccountInstance) {
171 const inboxUrls = await account.getFollowerSharedInboxUrls()
173 return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
176 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
177 const to = await getPublicActivityTo(byAccount)
178 const activity: ActivityCreate = {
181 actor: byAccount.url,
189 async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
190 const to = await getPublicActivityTo(byAccount)
191 const activity: ActivityUpdate = {
194 actor: byAccount.url,
202 async function deleteActivityData (url: string, byAccount: AccountInstance) {
203 const activity: ActivityDelete = {
212 async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
213 const to = await getPublicActivityTo(byAccount)
214 const activity: ActivityAdd = {
217 actor: byAccount.url,
226 async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
230 actor: byAccount.url,
237 async function followActivityData (url: string, byAccount: AccountInstance) {
238 const activity: ActivityFollow = {
241 actor: byAccount.url,
248 async function acceptActivityData (byAccount: AccountInstance) {
249 const activity: ActivityAccept = {