2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { VideoPrivacy } from '../../../../shared/models/videos'
|
|
|
|
import { getServerActor } from '../../../helpers'
|
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoAbuseModel } from '../../../models/video/video-abuse'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
|
|
|
|
import {
|
2017-12-19 03:34:56 -06:00
|
|
|
audiencify,
|
2017-11-23 07:19:55 -06:00
|
|
|
broadcastToFollowers,
|
2017-12-14 10:38:41 -06:00
|
|
|
getActorsInvolvedInVideo,
|
2017-11-23 07:19:55 -06:00
|
|
|
getAudience,
|
2017-11-27 07:44:51 -06:00
|
|
|
getObjectFollowersAudience,
|
2017-11-30 04:31:15 -06:00
|
|
|
getOriginVideoAudience,
|
2017-11-23 07:19:55 -06:00
|
|
|
unicastTo
|
|
|
|
} from './misc'
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendCreateVideo (video: VideoModel, t: Transaction) {
|
2017-12-19 03:34:56 -06:00
|
|
|
if (video.privacy === VideoPrivacy.PRIVATE) return
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-12-19 03:34:56 -06:00
|
|
|
const byActor = video.VideoChannel.Account.Actor
|
2017-12-14 10:38:41 -06:00
|
|
|
const videoObject = video.toActivityPubObject()
|
2017-12-19 03:34:56 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
|
|
|
|
const data = await createActivityData(video.url, byActor, videoObject, t, audience)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
return broadcastToFollowers(data, byActor, [ byActor ], t)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
|
2017-11-20 02:43:39 -06:00
|
|
|
const url = getVideoAbuseActivityPubUrl(videoAbuse)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
|
|
|
|
const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
|
2017-11-22 09:25:03 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
|
|
|
const url = getVideoViewActivityPubUrl(byActor, video)
|
|
|
|
const viewActivity = createViewActivityData(byActor, video)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
|
|
|
|
const data = await createActivityData(url, byActor, viewActivity, t, audience)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
|
|
|
const url = getVideoViewActivityPubUrl(byActor, video)
|
|
|
|
const viewActivity = createViewActivityData(byActor, video)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const audience = getObjectFollowersAudience(actorsToForwardView)
|
|
|
|
const data = await createActivityData(url, byActor, viewActivity, t, audience)
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
// Use the server actor to send the view
|
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const followersException = [ byActor ]
|
|
|
|
return broadcastToFollowers(data, serverActor, actorsToForwardView, t, followersException)
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
|
|
|
const url = getVideoDislikeActivityPubUrl(byActor, video)
|
|
|
|
const dislikeActivity = createDislikeActivityData(byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
|
|
|
|
const data = await createActivityData(url, byActor, dislikeActivity, t, audience)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
|
|
|
const url = getVideoDislikeActivityPubUrl(byActor, video)
|
|
|
|
const dislikeActivity = createDislikeActivityData(byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const audience = getObjectFollowersAudience(actorsToForwardView)
|
|
|
|
const data = await createActivityData(url, byActor, dislikeActivity, t, audience)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const followersException = [ byActor ]
|
|
|
|
return broadcastToFollowers(data, byActor, actorsToForwardView, t, followersException)
|
2017-11-22 09:25:03 -06:00
|
|
|
}
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
async function createActivityData (
|
|
|
|
url: string,
|
2017-12-14 10:38:41 -06:00
|
|
|
byActor: ActorModel,
|
2017-12-12 10:53:50 -06:00
|
|
|
object: any,
|
|
|
|
t: Transaction,
|
|
|
|
audience?: ActivityAudience
|
|
|
|
): Promise<ActivityCreate> {
|
2017-11-22 09:25:03 -06:00
|
|
|
if (!audience) {
|
2017-12-14 10:38:41 -06:00
|
|
|
audience = await getAudience(byActor, t)
|
2017-11-22 09:25:03 -06:00
|
|
|
}
|
2017-11-22 03:29:55 -06:00
|
|
|
|
2017-12-19 03:34:56 -06:00
|
|
|
return audiencify({
|
2017-11-20 02:43:39 -06:00
|
|
|
type: 'Create',
|
|
|
|
id: url,
|
2017-12-14 10:38:41 -06:00
|
|
|
actor: byActor.url,
|
2017-12-19 03:34:56 -06:00
|
|
|
object: audiencify(object, audience)
|
|
|
|
}, audience)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
|
2017-12-12 10:53:50 -06:00
|
|
|
return {
|
2017-11-23 07:19:55 -06:00
|
|
|
type: 'Dislike',
|
2017-12-14 10:38:41 -06:00
|
|
|
actor: byActor.url,
|
2017-11-23 07:19:55 -06:00
|
|
|
object: video.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 02:43:39 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-12-14 10:38:41 -06:00
|
|
|
sendCreateVideo,
|
2017-11-20 02:43:39 -06:00
|
|
|
sendVideoAbuse,
|
2017-11-22 09:25:03 -06:00
|
|
|
createActivityData,
|
|
|
|
sendCreateViewToOrigin,
|
2017-11-23 07:19:55 -06:00
|
|
|
sendCreateViewToVideoFollowers,
|
|
|
|
sendCreateDislikeToOrigin,
|
|
|
|
sendCreateDislikeToVideoFollowers,
|
|
|
|
createDislikeActivityData
|
2017-11-22 09:25:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
function createViewActivityData (byActor: ActorModel, video: VideoModel) {
|
2017-12-12 10:53:50 -06:00
|
|
|
return {
|
2017-11-22 09:25:03 -06:00
|
|
|
type: 'View',
|
2017-12-14 10:38:41 -06:00
|
|
|
actor: byActor.url,
|
2017-11-22 09:25:03 -06:00
|
|
|
object: video.url
|
|
|
|
}
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|