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

135 lines
4.9 KiB
TypeScript
Raw Normal View History

2017-11-20 09:43:39 +01:00
import { Transaction } from 'sequelize'
2017-12-12 17:53:50 +01:00
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
import { getServerAccount } from '../../../helpers'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoChannelModel } from '../../../models/video/video-channel'
2017-11-23 14:19:55 +01:00
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
import {
broadcastToFollowers,
getAccountsInvolvedInVideo,
2017-11-23 14:19:55 +01:00
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
2017-11-23 14:19:55 +01:00
unicastTo
} from './misc'
2017-11-20 09:43:39 +01:00
2017-12-12 17:53:50 +01:00
async function sendCreateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
2017-11-20 09:43:39 +01:00
const byAccount = videoChannel.Account
const videoChannelObject = videoChannel.toActivityPubObject()
const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject, t)
2017-11-20 09:43:39 +01:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
2017-12-12 17:53:50 +01:00
async function sendVideoAbuse (byAccount: AccountModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
2017-11-20 09:43:39 +01:00
const url = getVideoAbuseActivityPubUrl(videoAbuse)
2017-11-22 16:25:03 +01:00
const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), t, audience)
2017-11-22 16:25:03 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-12-12 17:53:50 +01:00
async function sendCreateViewToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-22 16:25:03 +01:00
const url = getVideoViewActivityPubUrl(byAccount, video)
const viewActivity = createViewActivityData(byAccount, video)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await createActivityData(url, byAccount, viewActivity, t, audience)
2017-11-20 09:43:39 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-12-12 17:53:50 +01:00
async function sendCreateViewToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-22 16:25:03 +01:00
const url = getVideoViewActivityPubUrl(byAccount, video)
const viewActivity = createViewActivityData(byAccount, video)
const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(accountsToForwardView)
const data = await createActivityData(url, byAccount, viewActivity, t, audience)
2017-11-22 16:25:03 +01:00
// Use the server account to send the view, because it could be an unregistered account
2017-11-22 16:25:03 +01:00
const serverAccount = await getServerAccount()
2017-11-23 14:19:55 +01:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
}
2017-12-12 17:53:50 +01:00
async function sendCreateDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-23 14:19:55 +01:00
const url = getVideoDislikeActivityPubUrl(byAccount, video)
const dislikeActivity = createDislikeActivityData(byAccount, video)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await createActivityData(url, byAccount, dislikeActivity, t, audience)
2017-11-23 14:19:55 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-11-22 16:25:03 +01:00
2017-12-12 17:53:50 +01:00
async function sendCreateDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-23 14:19:55 +01:00
const url = getVideoDislikeActivityPubUrl(byAccount, video)
const dislikeActivity = createDislikeActivityData(byAccount, video)
const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(accountsToForwardView)
const data = await createActivityData(url, byAccount, dislikeActivity, t, audience)
2017-11-23 14:19:55 +01:00
2017-11-22 16:25:03 +01:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException)
2017-11-22 16:25:03 +01:00
}
2017-12-12 17:53:50 +01:00
async function createActivityData (
url: string,
byAccount: AccountModel,
object: any,
t: Transaction,
audience?: ActivityAudience
): Promise<ActivityCreate> {
2017-11-22 16:25:03 +01:00
if (!audience) {
audience = await getAudience(byAccount, t)
2017-11-22 16:25:03 +01:00
}
2017-11-22 10:29:55 +01:00
2017-12-12 17:53:50 +01:00
return {
2017-11-20 09:43:39 +01:00
type: 'Create',
id: url,
actor: byAccount.url,
2017-11-22 16:25:03 +01:00
to: audience.to,
cc: audience.cc,
2017-11-20 09:43:39 +01:00
object
}
}
2017-12-12 17:53:50 +01:00
function createDislikeActivityData (byAccount: AccountModel, video: VideoModel) {
return {
2017-11-23 14:19:55 +01:00
type: 'Dislike',
actor: byAccount.url,
object: video.url
}
}
2017-11-20 09:43:39 +01:00
// ---------------------------------------------------------------------------
export {
sendCreateVideoChannel,
sendVideoAbuse,
2017-11-22 16:25:03 +01:00
createActivityData,
sendCreateViewToOrigin,
2017-11-23 14:19:55 +01:00
sendCreateViewToVideoFollowers,
sendCreateDislikeToOrigin,
sendCreateDislikeToVideoFollowers,
createDislikeActivityData
2017-11-22 16:25:03 +01:00
}
// ---------------------------------------------------------------------------
2017-12-12 17:53:50 +01:00
function createViewActivityData (byAccount: AccountModel, video: VideoModel) {
return {
2017-11-22 16:25:03 +01:00
type: 'View',
actor: byAccount.url,
object: video.url
}
2017-11-20 09:43:39 +01:00
}