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

89 lines
3.4 KiB
TypeScript
Raw Normal View History

2017-11-20 03:43:39 -05:00
import { Transaction } from 'sequelize'
2018-03-27 04:26:52 -04:00
import { ActivityAudience, ActivityDelete } from '../../../../shared/models/activitypub'
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'
2018-01-04 05:19:16 -05:00
import { VideoCommentModel } from '../../../models/video/video-comment'
2017-12-12 11:53:50 -05:00
import { VideoShareModel } from '../../../models/video/video-share'
2018-01-04 10:56:36 -05:00
import { getDeleteActivityPubUrl } from '../url'
2018-09-14 10:51:35 -04:00
import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
2018-05-25 05:32:36 -04:00
import { audiencify, getActorsInvolvedInVideo, getVideoCommentAudience } from '../audience'
2018-07-30 11:02:40 -04:00
import { logger } from '../../../helpers/logger'
2017-11-20 03:43:39 -05:00
2018-09-14 10:51:35 -04:00
async function sendDeleteVideo (video: VideoModel, transaction: Transaction) {
2018-07-30 11:02:40 -04:00
logger.info('Creating job to broadcast delete of video %s.', video.url)
2017-12-14 11:38:41 -05:00
const byActor = video.VideoChannel.Account.Actor
2017-11-20 03:43:39 -05:00
2018-09-14 10:51:35 -04:00
const activityBuilder = (audience: ActivityAudience) => {
const url = getDeleteActivityPubUrl(video.url)
2017-11-20 03:43:39 -05:00
2018-09-14 10:51:35 -04:00
return buildDeleteActivity(url, video.url, byActor, audience)
}
2017-11-20 03:43:39 -05:00
2018-09-14 10:51:35 -04:00
return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction })
2017-11-20 03:43:39 -05:00
}
2017-12-14 11:38:41 -05:00
async function sendDeleteActor (byActor: ActorModel, t: Transaction) {
2018-07-30 11:02:40 -04:00
logger.info('Creating job to broadcast delete of actor %s.', byActor.url)
2018-01-04 10:56:36 -05:00
const url = getDeleteActivityPubUrl(byActor.url)
2018-09-11 10:27:07 -04:00
const activity = buildDeleteActivity(url, byActor.url, byActor)
2017-11-20 03:43:39 -05:00
const actorsInvolved = await VideoShareModel.loadActorsByVideoOwner(byActor.id, t)
actorsInvolved.push(byActor)
2018-09-11 10:27:07 -04:00
return broadcastToFollowers(activity, byActor, actorsInvolved, t)
2017-11-20 03:43:39 -05:00
}
2018-01-04 05:19:16 -05:00
async function sendDeleteVideoComment (videoComment: VideoCommentModel, t: Transaction) {
2018-07-30 11:02:40 -04:00
logger.info('Creating job to send delete of comment %s.', videoComment.url)
2018-03-27 04:26:52 -04:00
const isVideoOrigin = videoComment.Video.isOwned()
2018-01-04 05:19:16 -05:00
2018-03-27 04:26:52 -04:00
const url = getDeleteActivityPubUrl(videoComment.url)
2018-01-04 10:56:36 -05:00
const byActor = videoComment.Account.Actor
2018-03-27 04:26:52 -04:00
const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, t)
2018-01-04 05:19:16 -05:00
2018-03-27 04:26:52 -04:00
const actorsInvolvedInComment = await getActorsInvolvedInVideo(videoComment.Video, t)
2018-09-11 10:27:07 -04:00
actorsInvolvedInComment.push(byActor) // Add the actor that commented the video
2018-01-04 05:19:16 -05:00
2018-03-27 04:26:52 -04:00
const audience = getVideoCommentAudience(videoComment, threadParentComments, actorsInvolvedInComment, isVideoOrigin)
2018-09-11 10:27:07 -04:00
const activity = buildDeleteActivity(url, videoComment.url, byActor, audience)
2018-03-27 04:26:52 -04:00
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
2018-09-11 10:27:07 -04:00
await broadcastToActors(activity, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
2018-03-27 04:26:52 -04:00
// Broadcast to our followers
2018-09-11 10:27:07 -04:00
await broadcastToFollowers(activity, byActor, [ byActor ], t)
2018-03-27 04:26:52 -04:00
// Send to actors involved in the comment
2018-09-11 10:27:07 -04:00
if (isVideoOrigin) return broadcastToFollowers(activity, byActor, actorsInvolvedInComment, t, actorsException)
2018-03-27 04:26:52 -04:00
// Send to origin
2018-09-11 10:27:07 -04:00
return unicastTo(activity, byActor, videoComment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
2018-01-04 05:19:16 -05:00
}
2017-11-20 03:43:39 -05:00
// ---------------------------------------------------------------------------
export {
sendDeleteVideo,
2018-01-04 05:19:16 -05:00
sendDeleteActor,
sendDeleteVideoComment
2017-11-20 03:43:39 -05:00
}
// ---------------------------------------------------------------------------
2018-09-11 10:27:07 -04:00
function buildDeleteActivity (url: string, object: string, byActor: ActorModel, audience?: ActivityAudience): ActivityDelete {
2018-03-27 04:26:52 -04:00
const activity = {
type: 'Delete' as 'Delete',
2017-11-20 03:43:39 -05:00
id: url,
2018-01-04 10:56:36 -05:00
actor: byActor.url,
object
2017-11-20 03:43:39 -05:00
}
2018-03-27 04:26:52 -04:00
if (audience) return audiencify(activity, audience)
return activity
2017-11-20 03:43:39 -05:00
}