From 2c8776fc316da9719e5ebc55dfabdcac9e197ac4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 4 Feb 2020 16:14:33 +0100 Subject: [PATCH] Optimize view endpoint --- server/controllers/activitypub/client.ts | 5 ++-- server/controllers/api/videos/index.ts | 4 ++-- server/lib/activitypub/audience.ts | 14 +++-------- server/lib/activitypub/send/send-view.ts | 4 ++-- server/lib/activitypub/send/utils.ts | 10 ++++---- server/models/activitypub/actor.ts | 30 ++++++++++++++++++++++++ 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 9a5fd6084..395cfa0d5 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -1,4 +1,3 @@ -// Intercept ActivityPub client requests import * as express from 'express' import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' @@ -37,10 +36,12 @@ import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' import { VideoPlaylistModel } from '../../models/video/video-playlist' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' -import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models' +import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models' const activityPubClientRouter = express.Router() +// Intercept ActivityPub client requests + activityPubClientRouter.get('/accounts?/:name', executeIfActivityPub, asyncMiddleware(localAccountValidator), diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 1d61f8427..eb46ea01f 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -135,7 +135,7 @@ videosRouter.get('/:id', asyncMiddleware(getVideo) ) videosRouter.post('/:id/views', - asyncMiddleware(videosGetValidator), + asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), asyncMiddleware(viewVideo) ) @@ -458,7 +458,7 @@ async function getVideo (req: express.Request, res: express.Response) { } async function viewVideo (req: express.Request, res: express.Response) { - const videoInstance = res.locals.videoAll + const videoInstance = res.locals.onlyImmutableVideo const ip = req.ip const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) diff --git a/server/lib/activitypub/audience.ts b/server/lib/activitypub/audience.ts index 39caeef7b..9933ae2b5 100644 --- a/server/lib/activitypub/audience.ts +++ b/server/lib/activitypub/audience.ts @@ -4,19 +4,11 @@ import { ACTIVITY_PUB } from '../../initializers/constants' import { ActorModel } from '../../models/activitypub/actor' import { VideoModel } from '../../models/video/video' import { VideoShareModel } from '../../models/video/video-share' -import { - MActorFollowersUrl, - MActorLight, - MCommentOwner, - MCommentOwnerVideo, - MVideo, - MVideoAccountLight, - MVideoId -} from '../../typings/models' +import { MActorFollowersUrl, MActorLight, MActorUrl, MCommentOwner, MCommentOwnerVideo, MVideoId } from '../../typings/models' -function getRemoteVideoAudience (video: MVideoAccountLight, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience { +function getRemoteVideoAudience (accountActor: MActorUrl, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience { return { - to: [ video.VideoChannel.Account.Actor.url ], + to: [ accountActor.url ], cc: actorsInvolvedInVideo.map(a => a.followersUrl) } } diff --git a/server/lib/activitypub/send/send-view.ts b/server/lib/activitypub/send/send-view.ts index 47482b9a9..1f864ea52 100644 --- a/server/lib/activitypub/send/send-view.ts +++ b/server/lib/activitypub/send/send-view.ts @@ -5,9 +5,9 @@ import { getVideoLikeActivityPubUrl } from '../url' import { sendVideoRelatedActivity } from './utils' import { audiencify, getAudience } from '../audience' import { logger } from '../../../helpers/logger' -import { MActorAudience, MVideoAccountLight, MVideoUrl } from '@server/typings/models' +import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/typings/models' -async function sendView (byActor: ActorModel, video: MVideoAccountLight, t: Transaction) { +async function sendView (byActor: ActorModel, video: MVideoImmutable, t: Transaction) { logger.info('Creating job to send view of %s.', video.url) const activityBuilder = (audience: ActivityAudience) => { diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts index 9436daf17..b57bae8fd 100644 --- a/server/lib/activitypub/send/utils.ts +++ b/server/lib/activitypub/send/utils.ts @@ -7,12 +7,12 @@ import { JobQueue } from '../../job-queue' import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' import { getServerActor } from '../../../helpers/utils' import { afterCommitIfTransaction } from '../../../helpers/database-utils' -import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../../typings/models' +import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../typings/models' import { ContextType } from '@server/helpers/activitypub' async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { byActor: MActorLight - video: MVideoAccountLight + video: MVideoImmutable | MVideoAccountLight transaction?: Transaction contextType?: ContextType }) { @@ -22,11 +22,13 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud // Send to origin if (video.isOwned() === false) { - const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo) + const accountActor = (video as MVideoAccountLight).VideoChannel?.Account?.Actor || await ActorModel.loadAccountActorByVideoId(video.id) + + const audience = getRemoteVideoAudience(accountActor, actorsInvolvedInVideo) const activity = activityBuilder(audience) return afterCommitIfTransaction(transaction, () => { - return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType) + return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType) }) } diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 9e8303a7b..e547d2c0c 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -462,6 +462,36 @@ export class ActorModel extends Model { }, { where, transaction }) } + static loadAccountActorByVideoId (videoId: number): Bluebird { + const query = { + include: [ + { + attributes: [ 'id' ], + model: AccountModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'id', 'accountId' ], + model: VideoChannelModel.unscoped(), + required: true, + include: [ + { + attributes: [ 'id', 'channelId' ], + model: VideoModel.unscoped(), + where: { + id: videoId + } + } + ] + } + ] + } + ] + } + + return ActorModel.unscoped().findOne(query) + } + getSharedInbox (this: MActorWithInboxes) { return this.sharedInboxUrl || this.inboxUrl }