2017-11-20 03:43:39 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { VideoPrivacy } from '../../../../shared/models/videos'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { getServerActor } from '../../../helpers/utils'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoAbuseModel } from '../../../models/video/video-abuse'
|
2017-12-27 04:39:31 -05:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2017-11-23 08:19:55 -05:00
|
|
|
import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
|
|
|
|
import {
|
2018-01-25 09:05:18 -05:00
|
|
|
audiencify,
|
|
|
|
broadcastToActors,
|
|
|
|
broadcastToFollowers,
|
|
|
|
getActorsInvolvedInVideo,
|
|
|
|
getAudience,
|
|
|
|
getObjectFollowersAudience,
|
|
|
|
getOriginVideoAudience,
|
2018-03-27 04:26:52 -04:00
|
|
|
getVideoCommentAudience,
|
2017-11-23 08:19:55 -05:00
|
|
|
unicastTo
|
|
|
|
} from './misc'
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
async function sendCreateVideo (video: VideoModel, t: Transaction) {
|
2017-12-19 08:22:38 -05:00
|
|
|
if (video.privacy === VideoPrivacy.PRIVATE) return undefined
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2017-12-19 04:34:56 -05:00
|
|
|
const byActor = video.VideoChannel.Account.Actor
|
2017-12-14 11:38:41 -05:00
|
|
|
const videoObject = video.toActivityPubObject()
|
2017-12-19 04:34:56 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const audience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
|
|
|
|
const data = await createActivityData(video.url, byActor, videoObject, t, audience)
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
return broadcastToFollowers(data, byActor, [ byActor ], t)
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) {
|
2017-11-20 03:43:39 -05:00
|
|
|
const url = getVideoAbuseActivityPubUrl(videoAbuse)
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
|
|
|
|
const data = await createActivityData(url, byActor, videoAbuse.toActivityPubObject(), t, audience)
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2017-11-22 10:25:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
|
|
|
|
const isOrigin = comment.Video.isOwned()
|
|
|
|
|
2017-12-27 04:39:31 -05:00
|
|
|
const byActor = comment.Account.Actor
|
2018-01-05 05:19:25 -05:00
|
|
|
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
|
|
|
|
const commentObject = comment.toActivityPubObject(threadParentComments)
|
2017-12-27 04:39:31 -05:00
|
|
|
|
2018-01-08 04:00:35 -05:00
|
|
|
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
|
|
|
|
actorsInvolvedInComment.push(byActor)
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
|
2017-12-27 04:39:31 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
let audience: ActivityAudience
|
|
|
|
if (isOrigin) {
|
|
|
|
audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
|
|
|
|
} else {
|
|
|
|
audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
|
|
|
|
}
|
2018-01-08 04:00:35 -05:00
|
|
|
|
2017-12-27 04:39:31 -05:00
|
|
|
const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
|
|
|
|
|
2018-01-08 04:00:35 -05:00
|
|
|
// This was a reply, send it to the parent actors
|
|
|
|
const actorsException = [ byActor ]
|
2018-03-27 07:33:56 -04:00
|
|
|
await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
|
2018-01-08 04:00:35 -05:00
|
|
|
|
|
|
|
// Broadcast to our followers
|
|
|
|
await broadcastToFollowers(data, byActor, [ byActor ], t)
|
|
|
|
|
|
|
|
// Send to actors involved in the comment
|
2018-03-27 07:33:56 -04:00
|
|
|
if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
|
|
|
|
|
|
|
|
// Send to origin
|
|
|
|
return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2017-12-27 04:39:31 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
2017-12-14 11:38:41 -05:00
|
|
|
const url = getVideoViewActivityPubUrl(byActor, video)
|
2017-12-27 04:39:31 -05:00
|
|
|
const viewActivityData = createViewActivityData(byActor, video)
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
// Send to origin
|
|
|
|
if (video.isOwned() === false) {
|
|
|
|
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
|
|
|
|
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
|
|
|
}
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
// Send to followers
|
|
|
|
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
|
2017-12-27 04:39:31 -05:00
|
|
|
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
// Use the server actor to send the view
|
|
|
|
const serverActor = await getServerActor()
|
2018-01-08 04:00:35 -05:00
|
|
|
const actorsException = [ byActor ]
|
2018-03-27 07:33:56 -04:00
|
|
|
return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
|
2017-11-23 08:19:55 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
2017-12-14 11:38:41 -05:00
|
|
|
const url = getVideoDislikeActivityPubUrl(byActor, video)
|
2017-12-27 04:39:31 -05:00
|
|
|
const dislikeActivityData = createDislikeActivityData(byActor, video)
|
2017-11-23 08:19:55 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
2017-11-23 08:19:55 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
// Send to origin
|
|
|
|
if (video.isOwned() === false) {
|
|
|
|
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
|
|
|
|
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
|
|
|
}
|
2017-11-23 08:19:55 -05:00
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
// Send to followers
|
|
|
|
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
|
2017-12-27 04:39:31 -05:00
|
|
|
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
|
2017-11-23 08:19:55 -05:00
|
|
|
|
2018-01-08 04:00:35 -05:00
|
|
|
const actorsException = [ byActor ]
|
2018-03-27 07:33:56 -04:00
|
|
|
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
|
2017-11-22 10:25:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 07:33:56 -04:00
|
|
|
async function createActivityData (url: string,
|
|
|
|
byActor: ActorModel,
|
|
|
|
object: any,
|
|
|
|
t: Transaction,
|
|
|
|
audience?: ActivityAudience): Promise<ActivityCreate> {
|
2017-11-22 10:25:03 -05:00
|
|
|
if (!audience) {
|
2017-12-14 11:38:41 -05:00
|
|
|
audience = await getAudience(byActor, t)
|
2017-11-22 10:25:03 -05:00
|
|
|
}
|
2017-11-22 04:29:55 -05:00
|
|
|
|
2017-12-19 04:34:56 -05:00
|
|
|
return audiencify({
|
2018-03-27 04:26:52 -04:00
|
|
|
type: 'Create' as 'Create',
|
2018-03-22 11:11:18 -04:00
|
|
|
id: url + '/activity',
|
2017-12-14 11:38:41 -05:00
|
|
|
actor: byActor.url,
|
2017-12-19 04:34:56 -05:00
|
|
|
object: audiencify(object, audience)
|
|
|
|
}, audience)
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
function createDislikeActivityData (byActor: ActorModel, video: VideoModel) {
|
2017-12-12 11:53:50 -05:00
|
|
|
return {
|
2017-11-23 08:19:55 -05:00
|
|
|
type: 'Dislike',
|
2017-12-14 11:38:41 -05:00
|
|
|
actor: byActor.url,
|
2017-11-23 08:19:55 -05:00
|
|
|
object: video.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-27 04:39:31 -05:00
|
|
|
function createViewActivityData (byActor: ActorModel, video: VideoModel) {
|
|
|
|
return {
|
|
|
|
type: 'View',
|
|
|
|
actor: byActor.url,
|
|
|
|
object: video.url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 03:43:39 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-12-14 11:38:41 -05:00
|
|
|
sendCreateVideo,
|
2017-11-20 03:43:39 -05:00
|
|
|
sendVideoAbuse,
|
2017-11-22 10:25:03 -05:00
|
|
|
createActivityData,
|
2018-03-27 07:33:56 -04:00
|
|
|
sendCreateView,
|
|
|
|
sendCreateDislike,
|
2017-12-27 04:39:31 -05:00
|
|
|
createDislikeActivityData,
|
2018-03-27 07:33:56 -04:00
|
|
|
sendCreateVideoComment
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|