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

257 lines
8.0 KiB
TypeScript
Raw Normal View History

2017-11-17 11:35:10 +01:00
import { Transaction } from 'sequelize'
2017-11-09 17:51:58 +01:00
import {
2017-11-17 11:35:10 +01:00
ActivityAccept,
ActivityAdd,
ActivityCreate,
ActivityDelete,
ActivityFollow,
ActivityUpdate
} from '../../../shared/models/activitypub/activity'
2017-11-15 15:12:23 +01:00
import { getActivityPubUrl } from '../../helpers/activitypub'
2017-11-15 16:28:35 +01:00
import { logger } from '../../helpers/logger'
2017-11-17 11:35:10 +01:00
import { database as db } from '../../initializers'
import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../models'
import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
import { activitypubHttpJobScheduler } from '../jobs'
async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
const byAccount = videoChannel.Account
2017-11-09 17:51:58 +01:00
const videoChannelObject = videoChannel.toActivityPubObject()
2017-11-17 11:35:10 +01:00
const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
2017-11-09 17:51:58 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
const byAccount = videoChannel.Account
2017-11-09 17:51:58 +01:00
const videoChannelObject = videoChannel.toActivityPubObject()
2017-11-17 11:35:10 +01:00
const data = await updateActivityData(videoChannel.url, byAccount, videoChannelObject)
2017-11-09 17:51:58 +01:00
2017-11-16 15:55:01 +01:00
const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
2017-11-17 11:35:10 +01:00
accountsInvolved.push(byAccount)
2017-11-16 15:55:01 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, accountsInvolved, t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
const byAccount = videoChannel.Account
const data = await deleteActivityData(videoChannel.url, byAccount)
2017-11-09 17:51:58 +01:00
2017-11-16 15:55:01 +01:00
const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
2017-11-17 11:35:10 +01:00
accountsInvolved.push(byAccount)
2017-11-16 15:55:01 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, accountsInvolved, t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendAddVideo (video: VideoInstance, t: Transaction) {
const byAccount = video.VideoChannel.Account
2017-11-09 17:51:58 +01:00
const videoObject = video.toActivityPubObject()
2017-11-17 11:35:10 +01:00
const data = await addActivityData(video.url, byAccount, video.VideoChannel.url, videoObject)
2017-11-09 17:51:58 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendUpdateVideo (video: VideoInstance, t: Transaction) {
const byAccount = video.VideoChannel.Account
2017-11-09 17:51:58 +01:00
const videoObject = video.toActivityPubObject()
2017-11-17 11:35:10 +01:00
const data = await updateActivityData(video.url, byAccount, videoObject)
2017-11-09 17:51:58 +01:00
2017-11-16 15:55:01 +01:00
const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
2017-11-17 11:35:10 +01:00
accountsInvolved.push(byAccount)
2017-11-16 15:55:01 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, accountsInvolved, t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendDeleteVideo (video: VideoInstance, t: Transaction) {
const byAccount = video.VideoChannel.Account
const data = await deleteActivityData(video.url, byAccount)
2017-11-09 17:51:58 +01:00
2017-11-16 15:55:01 +01:00
const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
2017-11-17 11:35:10 +01:00
accountsInvolved.push(byAccount)
2017-11-16 15:55:01 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, accountsInvolved, t)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendDeleteAccount (account: AccountInstance, t: Transaction) {
2017-11-14 17:31:26 +01:00
const data = await deleteActivityData(account.url, account)
2017-11-13 17:39:41 +01:00
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, account, [ account ], t)
}
2017-11-17 11:35:10 +01:00
async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) {
2017-11-16 15:22:39 +01:00
const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
2017-11-17 11:35:10 +01:00
const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject())
2017-11-16 15:22:39 +01:00
const data = await announceActivityData(url, byAccount, announcedActivity)
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
2017-11-16 15:22:39 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
2017-11-16 15:22:39 +01:00
const url = getActivityPubUrl('video', video.uuid) + '#announce'
2017-11-16 15:22:39 +01:00
const videoChannel = video.VideoChannel
2017-11-17 11:35:10 +01:00
const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject())
2017-11-16 15:22:39 +01:00
const data = await announceActivityData(url, byAccount, announcedActivity)
2017-11-17 11:35:10 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
2017-11-13 17:39:41 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
2017-11-15 15:12:23 +01:00
const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
2017-11-17 11:35:10 +01:00
const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject())
2017-11-15 15:12:23 +01:00
2017-11-17 11:35:10 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
2017-11-15 15:12:23 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendAccept (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
const data = await acceptActivityData(byAccount)
2017-11-13 18:48:28 +01:00
2017-11-17 11:35:10 +01:00
return unicastTo(data, byAccount, toAccount.inboxUrl, t)
2017-11-13 18:48:28 +01:00
}
2017-11-17 11:35:10 +01:00
async function sendFollow (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
const data = await followActivityData(toAccount.url, byAccount)
2017-11-13 18:48:28 +01:00
2017-11-17 11:35:10 +01:00
return unicastTo(data, byAccount, toAccount.inboxUrl, t)
2017-11-13 18:48:28 +01:00
}
2017-11-09 17:51:58 +01:00
// ---------------------------------------------------------------------------
export {
2017-11-10 17:27:49 +01:00
sendCreateVideoChannel,
sendUpdateVideoChannel,
sendDeleteVideoChannel,
sendAddVideo,
sendUpdateVideo,
2017-11-13 17:39:41 +01:00
sendDeleteVideo,
2017-11-13 18:48:28 +01:00
sendDeleteAccount,
sendAccept,
2017-11-15 15:12:23 +01:00
sendFollow,
sendVideoAbuse,
2017-11-16 15:22:39 +01:00
sendVideoChannelAnnounce,
sendVideoAnnounce
2017-11-09 17:51:58 +01:00
}
// ---------------------------------------------------------------------------
2017-11-17 11:35:10 +01:00
async function broadcastToFollowers (data: any, byAccount: AccountInstance, toAccountFollowers: AccountInstance[], t: Transaction) {
const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
2017-11-15 16:28:35 +01:00
if (result.data.length === 0) {
logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
2017-11-15 16:28:35 +01:00
return
}
2017-11-10 17:27:49 +01:00
const jobPayload = {
uris: result.data,
2017-11-17 11:35:10 +01:00
signatureAccountId: byAccount.id,
2017-11-10 17:27:49 +01:00
body: data
}
2017-11-17 11:35:10 +01:00
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) {
2017-11-13 18:48:28 +01:00
const jobPayload = {
2017-11-15 15:12:23 +01:00
uris: [ toAccountUrl ],
2017-11-17 11:35:10 +01:00
signatureAccountId: byAccount.id,
2017-11-13 18:48:28 +01:00
body: data
}
2017-11-17 11:35:10 +01:00
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
2017-11-09 17:51:58 +01:00
}
async function getPublicActivityTo (account: AccountInstance) {
const inboxUrls = await account.getFollowerSharedInboxUrls()
return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public')
}
2017-11-17 11:35:10 +01:00
async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
2017-11-09 17:51:58 +01:00
const to = await getPublicActivityTo(byAccount)
2017-11-17 11:35:10 +01:00
const activity: ActivityCreate = {
2017-11-09 17:51:58 +01:00
type: 'Create',
id: url,
actor: byAccount.url,
to,
object
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-09 17:51:58 +01:00
}
async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
const to = await getPublicActivityTo(byAccount)
2017-11-17 11:35:10 +01:00
const activity: ActivityUpdate = {
2017-11-09 17:51:58 +01:00
type: 'Update',
id: url,
actor: byAccount.url,
to,
object
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-09 17:51:58 +01:00
}
2017-11-13 17:39:41 +01:00
async function deleteActivityData (url: string, byAccount: AccountInstance) {
2017-11-17 11:35:10 +01:00
const activity: ActivityDelete = {
2017-11-13 17:39:41 +01:00
type: 'Delete',
2017-11-09 17:51:58 +01:00
id: url,
2017-11-13 17:39:41 +01:00
actor: byAccount.url
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-09 17:51:58 +01:00
}
2017-11-17 11:35:10 +01:00
async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
2017-11-09 17:51:58 +01:00
const to = await getPublicActivityTo(byAccount)
2017-11-17 11:35:10 +01:00
const activity: ActivityAdd = {
2017-11-09 17:51:58 +01:00
type: 'Add',
id: url,
actor: byAccount.url,
to,
object,
target
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-09 17:51:58 +01:00
}
2017-11-13 18:48:28 +01:00
async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
2017-11-17 11:35:10 +01:00
const activity = {
type: 'Announce',
id: url,
actor: byAccount.url,
object
}
2017-11-17 11:35:10 +01:00
return activity
}
2017-11-13 18:48:28 +01:00
async function followActivityData (url: string, byAccount: AccountInstance) {
2017-11-17 11:35:10 +01:00
const activity: ActivityFollow = {
2017-11-13 18:48:28 +01:00
type: 'Follow',
id: byAccount.url,
actor: byAccount.url,
object: url
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-13 18:48:28 +01:00
}
async function acceptActivityData (byAccount: AccountInstance) {
2017-11-17 11:35:10 +01:00
const activity: ActivityAccept = {
2017-11-13 18:48:28 +01:00
type: 'Accept',
id: byAccount.url,
actor: byAccount.url
}
2017-11-17 11:35:10 +01:00
return activity
2017-11-13 18:48:28 +01:00
}