diff options
author | Chocobozzz <me@florianbigard.com> | 2020-02-04 16:14:33 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-02-04 16:18:01 +0100 |
commit | 2c8776fc316da9719e5ebc55dfabdcac9e197ac4 (patch) | |
tree | b8e8365092437ee69f2ef88d0e9d858fae15ec45 | |
parent | 943e5193905908dd1f2800d8810c635d86e3b28f (diff) | |
download | PeerTube-2c8776fc316da9719e5ebc55dfabdcac9e197ac4.tar.gz PeerTube-2c8776fc316da9719e5ebc55dfabdcac9e197ac4.tar.zst PeerTube-2c8776fc316da9719e5ebc55dfabdcac9e197ac4.zip |
Optimize view endpoint
-rw-r--r-- | server/controllers/activitypub/client.ts | 5 | ||||
-rw-r--r-- | server/controllers/api/videos/index.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/audience.ts | 16 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-view.ts | 4 | ||||
-rw-r--r-- | server/lib/activitypub/send/utils.ts | 10 | ||||
-rw-r--r-- | server/models/activitypub/actor.ts | 30 |
6 files changed, 47 insertions, 22 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 @@ | |||
1 | // Intercept ActivityPub client requests | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
3 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' | 2 | import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' |
4 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' | 3 | import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' |
@@ -37,10 +36,12 @@ import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike' | |||
37 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' | 36 | import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists' |
38 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 37 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
39 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' | 38 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
40 | import { MAccountId, MActorId, MVideo, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models' | 39 | import { MAccountId, MActorId, MVideoAPWithoutCaption, MVideoId } from '@server/typings/models' |
41 | 40 | ||
42 | const activityPubClientRouter = express.Router() | 41 | const activityPubClientRouter = express.Router() |
43 | 42 | ||
43 | // Intercept ActivityPub client requests | ||
44 | |||
44 | activityPubClientRouter.get('/accounts?/:name', | 45 | activityPubClientRouter.get('/accounts?/:name', |
45 | executeIfActivityPub, | 46 | executeIfActivityPub, |
46 | asyncMiddleware(localAccountValidator), | 47 | 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', | |||
135 | asyncMiddleware(getVideo) | 135 | asyncMiddleware(getVideo) |
136 | ) | 136 | ) |
137 | videosRouter.post('/:id/views', | 137 | videosRouter.post('/:id/views', |
138 | asyncMiddleware(videosGetValidator), | 138 | asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')), |
139 | asyncMiddleware(viewVideo) | 139 | asyncMiddleware(viewVideo) |
140 | ) | 140 | ) |
141 | 141 | ||
@@ -458,7 +458,7 @@ async function getVideo (req: express.Request, res: express.Response) { | |||
458 | } | 458 | } |
459 | 459 | ||
460 | async function viewVideo (req: express.Request, res: express.Response) { | 460 | async function viewVideo (req: express.Request, res: express.Response) { |
461 | const videoInstance = res.locals.videoAll | 461 | const videoInstance = res.locals.onlyImmutableVideo |
462 | 462 | ||
463 | const ip = req.ip | 463 | const ip = req.ip |
464 | const exists = await Redis.Instance.doesVideoIPViewExist(ip, videoInstance.uuid) | 464 | 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' | |||
4 | import { ActorModel } from '../../models/activitypub/actor' | 4 | import { ActorModel } from '../../models/activitypub/actor' |
5 | import { VideoModel } from '../../models/video/video' | 5 | import { VideoModel } from '../../models/video/video' |
6 | import { VideoShareModel } from '../../models/video/video-share' | 6 | import { VideoShareModel } from '../../models/video/video-share' |
7 | import { | 7 | import { MActorFollowersUrl, MActorLight, MActorUrl, MCommentOwner, MCommentOwnerVideo, MVideoId } from '../../typings/models' |
8 | MActorFollowersUrl, | 8 | |
9 | MActorLight, | 9 | function getRemoteVideoAudience (accountActor: MActorUrl, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience { |
10 | MCommentOwner, | ||
11 | MCommentOwnerVideo, | ||
12 | MVideo, | ||
13 | MVideoAccountLight, | ||
14 | MVideoId | ||
15 | } from '../../typings/models' | ||
16 | |||
17 | function getRemoteVideoAudience (video: MVideoAccountLight, actorsInvolvedInVideo: MActorFollowersUrl[]): ActivityAudience { | ||
18 | return { | 10 | return { |
19 | to: [ video.VideoChannel.Account.Actor.url ], | 11 | to: [ accountActor.url ], |
20 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) | 12 | cc: actorsInvolvedInVideo.map(a => a.followersUrl) |
21 | } | 13 | } |
22 | } | 14 | } |
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' | |||
5 | import { sendVideoRelatedActivity } from './utils' | 5 | import { sendVideoRelatedActivity } from './utils' |
6 | import { audiencify, getAudience } from '../audience' | 6 | import { audiencify, getAudience } from '../audience' |
7 | import { logger } from '../../../helpers/logger' | 7 | import { logger } from '../../../helpers/logger' |
8 | import { MActorAudience, MVideoAccountLight, MVideoUrl } from '@server/typings/models' | 8 | import { MActorAudience, MVideoImmutable, MVideoUrl } from '@server/typings/models' |
9 | 9 | ||
10 | async function sendView (byActor: ActorModel, video: MVideoAccountLight, t: Transaction) { | 10 | async function sendView (byActor: ActorModel, video: MVideoImmutable, t: Transaction) { |
11 | logger.info('Creating job to send view of %s.', video.url) | 11 | logger.info('Creating job to send view of %s.', video.url) |
12 | 12 | ||
13 | const activityBuilder = (audience: ActivityAudience) => { | 13 | 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' | |||
7 | import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' | 7 | import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience' |
8 | import { getServerActor } from '../../../helpers/utils' | 8 | import { getServerActor } from '../../../helpers/utils' |
9 | import { afterCommitIfTransaction } from '../../../helpers/database-utils' | 9 | import { afterCommitIfTransaction } from '../../../helpers/database-utils' |
10 | import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight, MVideoId } from '../../../typings/models' | 10 | import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../typings/models' |
11 | import { ContextType } from '@server/helpers/activitypub' | 11 | import { ContextType } from '@server/helpers/activitypub' |
12 | 12 | ||
13 | async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { | 13 | async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { |
14 | byActor: MActorLight | 14 | byActor: MActorLight |
15 | video: MVideoAccountLight | 15 | video: MVideoImmutable | MVideoAccountLight |
16 | transaction?: Transaction | 16 | transaction?: Transaction |
17 | contextType?: ContextType | 17 | contextType?: ContextType |
18 | }) { | 18 | }) { |
@@ -22,11 +22,13 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud | |||
22 | 22 | ||
23 | // Send to origin | 23 | // Send to origin |
24 | if (video.isOwned() === false) { | 24 | if (video.isOwned() === false) { |
25 | const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo) | 25 | const accountActor = (video as MVideoAccountLight).VideoChannel?.Account?.Actor || await ActorModel.loadAccountActorByVideoId(video.id) |
26 | |||
27 | const audience = getRemoteVideoAudience(accountActor, actorsInvolvedInVideo) | ||
26 | const activity = activityBuilder(audience) | 28 | const activity = activityBuilder(audience) |
27 | 29 | ||
28 | return afterCommitIfTransaction(transaction, () => { | 30 | return afterCommitIfTransaction(transaction, () => { |
29 | return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType) | 31 | return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType) |
30 | }) | 32 | }) |
31 | } | 33 | } |
32 | 34 | ||
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<ActorModel> { | |||
462 | }, { where, transaction }) | 462 | }, { where, transaction }) |
463 | } | 463 | } |
464 | 464 | ||
465 | static loadAccountActorByVideoId (videoId: number): Bluebird<MActor> { | ||
466 | const query = { | ||
467 | include: [ | ||
468 | { | ||
469 | attributes: [ 'id' ], | ||
470 | model: AccountModel.unscoped(), | ||
471 | required: true, | ||
472 | include: [ | ||
473 | { | ||
474 | attributes: [ 'id', 'accountId' ], | ||
475 | model: VideoChannelModel.unscoped(), | ||
476 | required: true, | ||
477 | include: [ | ||
478 | { | ||
479 | attributes: [ 'id', 'channelId' ], | ||
480 | model: VideoModel.unscoped(), | ||
481 | where: { | ||
482 | id: videoId | ||
483 | } | ||
484 | } | ||
485 | ] | ||
486 | } | ||
487 | ] | ||
488 | } | ||
489 | ] | ||
490 | } | ||
491 | |||
492 | return ActorModel.unscoped().findOne(query) | ||
493 | } | ||
494 | |||
465 | getSharedInbox (this: MActorWithInboxes) { | 495 | getSharedInbox (this: MActorWithInboxes) { |
466 | return this.sharedInboxUrl || this.inboxUrl | 496 | return this.sharedInboxUrl || this.inboxUrl |
467 | } | 497 | } |