PeerTube/server/lib/activitypub/send/send-create.ts

95 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-11-20 02:43:39 -06:00
import { Transaction } from 'sequelize'
import { ActivityCreate } from '../../../../shared/models/activitypub/activity'
import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
import { broadcastToFollowers, getAudience, unicastTo } from './misc'
2017-11-22 09:25:03 -06:00
import { getVideoAbuseActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
import { getServerAccount } from '../../../helpers/utils'
import { database as db } from '../../../initializers'
2017-11-20 02:43:39 -06:00
async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
const byAccount = videoChannel.Account
const videoChannelObject = videoChannel.toActivityPubObject()
const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
const url = getVideoAbuseActivityPubUrl(videoAbuse)
2017-11-22 09:25:03 -06:00
const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), audience)
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
async function sendCreateViewToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const url = getVideoViewActivityPubUrl(byAccount, video)
const viewActivity = createViewActivityData(byAccount, video)
const audience = { to: [ video.VideoChannel.Account.url ], cc: [ video.VideoChannel.Account.url + '/followers' ] }
const data = await createActivityData(url, byAccount, viewActivity, audience)
2017-11-20 02:43:39 -06:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-11-22 09:25:03 -06:00
async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const url = getVideoViewActivityPubUrl(byAccount, video)
const viewActivity = createViewActivityData(byAccount, video)
const audience = { to: [ video.VideoChannel.Account.url + '/followers' ], cc: [] }
const data = await createActivityData(url, byAccount, viewActivity, audience)
const serverAccount = await getServerAccount()
const accountsToForwardView = await db.VideoShare.loadAccountsByShare(video.id)
accountsToForwardView.push(video.VideoChannel.Account)
// Don't forward view to server that sent it to us
const index = accountsToForwardView.findIndex(a => a.id === byAccount.id)
if (index) accountsToForwardView.splice(index, 1)
const followersException = [ byAccount ]
return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
}
async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: { to: string[], cc: string[] }) {
if (!audience) {
audience = await getAudience(byAccount)
}
2017-11-22 03:29:55 -06:00
2017-11-20 02:43:39 -06:00
const activity: ActivityCreate = {
type: 'Create',
id: url,
actor: byAccount.url,
2017-11-22 09:25:03 -06:00
to: audience.to,
cc: audience.cc,
2017-11-20 02:43:39 -06:00
object
}
return activity
}
// ---------------------------------------------------------------------------
export {
sendCreateVideoChannel,
sendVideoAbuse,
2017-11-22 09:25:03 -06:00
createActivityData,
sendCreateViewToOrigin,
sendCreateViewToVideoFollowers
}
// ---------------------------------------------------------------------------
function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {
const obj = {
type: 'View',
actor: byAccount.url,
object: video.url
}
return obj
2017-11-20 02:43:39 -06:00
}