From c48e82b5e0478434de30626d14594a97f2402e7c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 11 Sep 2018 16:27:07 +0200 Subject: Basic video redundancy implementation --- server/controllers/activitypub/client.ts | 34 ++++++++++++++++++++++++----- server/controllers/activitypub/outbox.ts | 6 ++--- server/controllers/api/search.ts | 2 -- server/controllers/api/server/follows.ts | 5 +++++ server/controllers/api/server/index.ts | 2 ++ server/controllers/api/server/redundancy.ts | 32 +++++++++++++++++++++++++++ server/controllers/api/videos/abuse.ts | 2 +- 7 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 server/controllers/api/server/redundancy.ts (limited to 'server/controllers') diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 54cf44419..2e168ea78 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -3,9 +3,9 @@ import * as express from 'express' import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' -import { buildVideoAnnounce } from '../../lib/activitypub/send' +import { buildAnnounceWithVideoAudience } from '../../lib/activitypub/send' import { audiencify, getAudience } from '../../lib/activitypub/audience' -import { createActivityData } from '../../lib/activitypub/send/send-create' +import { buildCreateActivity } from '../../lib/activitypub/send/send-create' import { asyncMiddleware, executeIfActivityPub, localAccountValidator, localVideoChannelValidator } from '../../middlewares' import { videosGetValidator, videosShareValidator } from '../../middlewares/validators' import { videoCommentGetValidator } from '../../middlewares/validators/video-comments' @@ -26,6 +26,8 @@ import { getVideoSharesActivityPubUrl } from '../../lib/activitypub' import { VideoCaptionModel } from '../../models/video/video-caption' +import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy' +import { getServerActor } from '../../helpers/utils' const activityPubClientRouter = express.Router() @@ -93,6 +95,11 @@ activityPubClientRouter.get('/video-channels/:name/following', executeIfActivityPub(asyncMiddleware(videoChannelFollowingController)) ) +activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?', + executeIfActivityPub(asyncMiddleware(videoRedundancyGetValidator)), + executeIfActivityPub(asyncMiddleware(videoRedundancyController)) +) + // --------------------------------------------------------------------------- export { @@ -131,7 +138,7 @@ async function videoController (req: express.Request, res: express.Response, nex const videoObject = audiencify(video.toActivityPubObject(), audience) if (req.path.endsWith('/activity')) { - const data = createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, audience) + const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience) return activityPubResponse(activityPubContextify(data), res) } @@ -140,9 +147,9 @@ async function videoController (req: express.Request, res: express.Response, nex async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) { const share = res.locals.videoShare as VideoShareModel - const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined) + const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined) - return activityPubResponse(activityPubContextify(object), res) + return activityPubResponse(activityPubContextify(activity), res) } async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -219,13 +226,28 @@ async function videoCommentController (req: express.Request, res: express.Respon const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience) if (req.path.endsWith('/activity')) { - const data = createActivityData(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) + const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience) return activityPubResponse(activityPubContextify(data), res) } return activityPubResponse(activityPubContextify(videoCommentObject), res) } +async function videoRedundancyController (req: express.Request, res: express.Response) { + const videoRedundancy = res.locals.videoRedundancy + const serverActor = await getServerActor() + + const audience = getAudience(serverActor) + const object = audiencify(videoRedundancy.toActivityPubObject(), audience) + + if (req.path.endsWith('/activity')) { + const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience) + return activityPubResponse(activityPubContextify(data), res) + } + + return activityPubResponse(activityPubContextify(object), res) +} + // --------------------------------------------------------------------------- async function actorFollowing (req: express.Request, actor: ActorModel) { diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index db69ae54b..bd0e4fe9d 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts @@ -3,7 +3,7 @@ import { Activity } from '../../../shared/models/activitypub/activity' import { VideoPrivacy } from '../../../shared/models/videos' import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' import { logger } from '../../helpers/logger' -import { announceActivityData, createActivityData } from '../../lib/activitypub/send' +import { buildAnnounceActivity, buildCreateActivity } from '../../lib/activitypub/send' import { buildAudience } from '../../lib/activitypub/audience' import { asyncMiddleware, localAccountValidator, localVideoChannelValidator } from '../../middlewares' import { AccountModel } from '../../models/account/account' @@ -60,12 +60,12 @@ async function buildActivities (actor: ActorModel, start: number, count: number) // This is a shared video if (video.VideoShares !== undefined && video.VideoShares.length !== 0) { const videoShare = video.VideoShares[0] - const announceActivity = announceActivityData(videoShare.url, actor, video.url, createActivityAudience) + const announceActivity = buildAnnounceActivity(videoShare.url, actor, video.url, createActivityAudience) activities.push(announceActivity) } else { const videoObject = video.toActivityPubObject() - const createActivity = createActivityData(video.url, byActor, videoObject, createActivityAudience) + const createActivity = buildCreateActivity(video.url, byActor, videoObject, createActivityAudience) activities.push(createActivity) } diff --git a/server/controllers/api/search.ts b/server/controllers/api/search.ts index bb7174891..28a7a04ca 100644 --- a/server/controllers/api/search.ts +++ b/server/controllers/api/search.ts @@ -17,8 +17,6 @@ import { import { VideoChannelsSearchQuery, VideosSearchQuery } from '../../../shared/models/search' import { getOrCreateActorAndServerAndModel, getOrCreateVideoAndAccountAndChannel } from '../../lib/activitypub' import { logger } from '../../helpers/logger' -import { User } from '../../../shared/models/users' -import { CONFIG } from '../../initializers/constants' import { VideoChannelModel } from '../../models/video/video-channel' import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index 23308445f..a4eae6b45 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts @@ -96,6 +96,11 @@ async function removeFollow (req: express.Request, res: express.Response, next: await sequelizeTypescript.transaction(async t => { if (follow.state === 'accepted') await sendUndoFollow(follow, t) + // Disable redundancy on unfollowed instances + const server = follow.ActorFollowing.Server + server.redundancyAllowed = false + await server.save({ transaction: t }) + await follow.destroy({ transaction: t }) }) diff --git a/server/controllers/api/server/index.ts b/server/controllers/api/server/index.ts index 850a52cdb..43bca2c10 100644 --- a/server/controllers/api/server/index.ts +++ b/server/controllers/api/server/index.ts @@ -1,10 +1,12 @@ import * as express from 'express' import { serverFollowsRouter } from './follows' import { statsRouter } from './stats' +import { serverRedundancyRouter } from './redundancy' const serverRouter = express.Router() serverRouter.use('/', serverFollowsRouter) +serverRouter.use('/', serverRedundancyRouter) serverRouter.use('/', statsRouter) // --------------------------------------------------------------------------- diff --git a/server/controllers/api/server/redundancy.ts b/server/controllers/api/server/redundancy.ts new file mode 100644 index 000000000..4216b9e35 --- /dev/null +++ b/server/controllers/api/server/redundancy.ts @@ -0,0 +1,32 @@ +import * as express from 'express' +import { UserRight } from '../../../../shared/models/users' +import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' +import { updateServerRedundancyValidator } from '../../../middlewares/validators/redundancy' +import { ServerModel } from '../../../models/server/server' + +const serverRedundancyRouter = express.Router() + +serverRedundancyRouter.put('/redundancy/:host', + authenticate, + ensureUserHasRight(UserRight.MANAGE_SERVER_FOLLOW), + asyncMiddleware(updateServerRedundancyValidator), + asyncMiddleware(updateRedundancy) +) + +// --------------------------------------------------------------------------- + +export { + serverRedundancyRouter +} + +// --------------------------------------------------------------------------- + +async function updateRedundancy (req: express.Request, res: express.Response, next: express.NextFunction) { + const server = res.locals.server as ServerModel + + server.redundancyAllowed = req.body.redundancyAllowed + + await server.save() + + return res.sendStatus(204) +} diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 59bdf6257..08e11b00b 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -112,7 +112,7 @@ async function reportVideoAbuse (req: express.Request, res: express.Response) { // We send the video abuse to the origin server if (videoInstance.isOwned() === false) { - await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance, t) + await sendVideoAbuse(reporterAccount.Actor, videoAbuseInstance, videoInstance) } auditLogger.create(reporterAccount.Actor.getIdentifier(), new VideoAbuseAuditView(videoAbuseInstance.toFormattedJSON())) -- cgit v1.2.3