1 import { Transaction } from 'sequelize'
2 import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
3 import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
4 import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
5 import { broadcastToFollowers, getAudience, unicastTo } from './misc'
6 import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
7 import { getServerAccount } from '../../../helpers/utils'
8 import { database as db } from '../../../initializers'
10 async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
11 const byAccount = videoChannel.Account
13 const videoChannelObject = videoChannel.toActivityPubObject()
14 const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
16 return broadcastToFollowers(data, byAccount, [ byAccount ], t)
19 async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
20 const url = getVideoAbuseActivityPubUrl(videoAbuse)
22 const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
23 const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience)
25 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
28 async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
29 const url = getVideoViewActivityPubUrl(byAccount, video)
30 const viewActivity = createViewActivityData(byAccount, video)
32 const audience = { to: [ video.VideoChannel.Account.url ], cc: [ video.VideoChannel.Account.url + '/followers' ] }
33 const data = await createActivityData(url, byAccount, viewActivity, audience)
35 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
38 async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
39 const url = getVideoViewActivityPubUrl(byAccount, video)
40 const viewActivity = createViewActivityData(byAccount, video)
42 const audience = { to: [ video.VideoChannel.Account.url + '/followers' ], cc: [] }
43 const data = await createActivityData(url, byAccount, viewActivity, audience)
45 const serverAccount = await getServerAccount()
46 const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
47 accountsToForwardView.push(video.VideoChannel.Account)
49 // Don't forward view to server that sent it to us
50 const index = accountsToForwardView.findIndex(a => a.id === byAccount.id)
51 if (index) accountsToForwardView.splice(index, 1)
53 const followersException = [ byAccount ]
54 return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
57 async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
59 audience = await getAudience(byAccount)
62 const activity: ActivityCreate = {
74 // ---------------------------------------------------------------------------
77 sendCreateVideoChannel,
80 sendCreateViewToOrigin,
81 sendCreateViewToVideoFollowers
84 // ---------------------------------------------------------------------------
86 function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {