1
0
Fork 0
peertube/server/lib/activitypub/send/send-create.ts

133 lines
5 KiB
TypeScript
Raw Normal View History

2017-11-20 03:43:39 -05:00
import { Transaction } from 'sequelize'
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub/activity'
2017-11-23 08:19:55 -05:00
import { getServerAccount } from '../../../helpers/utils'
2017-11-20 03:43:39 -05:00
import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../../models'
import { VideoAbuseInstance } from '../../../models/video/video-abuse-interface'
2017-11-23 08:19:55 -05:00
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
import {
broadcastToFollowers,
getAccountsInvolvedInVideo,
2017-11-23 08:19:55 -05:00
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
2017-11-23 08:19:55 -05:00
unicastTo
} from './misc'
2017-11-20 03:43:39 -05:00
async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
const byAccount = videoChannel.Account
const videoChannelObject = videoChannel.toActivityPubObject()
const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject, t)
2017-11-20 03:43:39 -05:00
return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
const url = getVideoAbuseActivityPubUrl(videoAbuse)
2017-11-22 10:25:03 -05:00
const audience = { to: [ video.VideoChannel.Account.url ], cc: [] }
const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject(), t, audience)
2017-11-22 10:25:03 -05:00
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 accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await createActivityData(url, byAccount, viewActivity, t, audience)
2017-11-20 03:43:39 -05:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-11-22 10:25:03 -05:00
async function sendCreateViewToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
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 10:25:03 -05:00
// Use the server account to send the view, because it could be an unregistered account
2017-11-22 10:25:03 -05:00
const serverAccount = await getServerAccount()
2017-11-23 08:19:55 -05:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException)
}
async function sendCreateDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
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 08:19:55 -05:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-11-22 10:25:03 -05:00
2017-11-23 08:19:55 -05:00
async function sendCreateDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
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 08:19:55 -05:00
2017-11-22 10:25:03 -05:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException)
2017-11-22 10:25:03 -05:00
}
async function createActivityData (url: string, byAccount: AccountInstance, object: any, t: Transaction, audience?: ActivityAudience) {
2017-11-22 10:25:03 -05:00
if (!audience) {
audience = await getAudience(byAccount, t)
2017-11-22 10:25:03 -05:00
}
2017-11-22 04:29:55 -05:00
2017-11-20 03:43:39 -05:00
const activity: ActivityCreate = {
type: 'Create',
id: url,
actor: byAccount.url,
2017-11-22 10:25:03 -05:00
to: audience.to,
cc: audience.cc,
2017-11-20 03:43:39 -05:00
object
}
return activity
}
2017-11-23 08:19:55 -05:00
function createDislikeActivityData (byAccount: AccountInstance, video: VideoInstance) {
const obj = {
type: 'Dislike',
actor: byAccount.url,
object: video.url
}
return obj
}
2017-11-20 03:43:39 -05:00
// ---------------------------------------------------------------------------
export {
sendCreateVideoChannel,
sendVideoAbuse,
2017-11-22 10:25:03 -05:00
createActivityData,
sendCreateViewToOrigin,
2017-11-23 08:19:55 -05:00
sendCreateViewToVideoFollowers,
sendCreateDislikeToOrigin,
sendCreateDislikeToVideoFollowers,
createDislikeActivityData
2017-11-22 10:25:03 -05:00
}
// ---------------------------------------------------------------------------
function createViewActivityData (byAccount: AccountInstance, video: VideoInstance) {
const obj = {
type: 'View',
actor: byAccount.url,
object: video.url
}
return obj
2017-11-20 03:43:39 -05:00
}