2017-11-20 03:43:39 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2017-11-24 07:41:10 -05:00
|
|
|
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,
|
2017-11-24 07:41:10 -05:00
|
|
|
getAccountsInvolvedInVideo,
|
2017-11-23 08:19:55 -05:00
|
|
|
getAudience,
|
|
|
|
getOriginVideoAudience,
|
|
|
|
getVideoFollowersAudience,
|
|
|
|
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)
|
|
|
|
|
|
|
|
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(), 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)
|
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video)
|
|
|
|
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
|
2017-11-22 10:25:03 -05:00
|
|
|
const data = await createActivityData(url, byAccount, viewActivity, 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)
|
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
const accountsToForwardView = await getAccountsInvolvedInVideo(video)
|
|
|
|
const audience = getVideoFollowersAudience(accountsToForwardView)
|
2017-11-22 10:25:03 -05:00
|
|
|
const data = await createActivityData(url, byAccount, viewActivity, audience)
|
|
|
|
|
2017-11-24 07:41:10 -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)
|
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video)
|
|
|
|
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
|
2017-11-23 08:19:55 -05:00
|
|
|
const data = await createActivityData(url, byAccount, dislikeActivity, audience)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
const accountsToForwardView = await getAccountsInvolvedInVideo(video)
|
|
|
|
const audience = getVideoFollowersAudience(accountsToForwardView)
|
2017-11-23 08:19:55 -05:00
|
|
|
const data = await createActivityData(url, byAccount, dislikeActivity, audience)
|
|
|
|
|
2017-11-22 10:25:03 -05:00
|
|
|
const followersException = [ byAccount ]
|
2017-11-24 07:41:10 -05:00
|
|
|
return broadcastToFollowers(data, byAccount, accountsToForwardView, t, followersException)
|
2017-11-22 10:25:03 -05:00
|
|
|
}
|
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
async function createActivityData (url: string, byAccount: AccountInstance, object: any, audience?: ActivityAudience) {
|
2017-11-22 10:25:03 -05:00
|
|
|
if (!audience) {
|
|
|
|
audience = await getAudience(byAccount)
|
|
|
|
}
|
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
|
|
|
}
|