From 8fffe21a7bc96d08b229293d66ddba576e609790 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 25 May 2018 16:21:16 +0200 Subject: Refractor and optimize AP collections Only display urls in general object, and paginate video comments, shares, likes and dislikes --- server/controllers/activitypub/client.ts | 86 +++++++++++++++++++------------- server/controllers/activitypub/outbox.ts | 24 ++++----- server/controllers/activitypub/utils.ts | 11 ++++ 3 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 server/controllers/activitypub/utils.ts (limited to 'server/controllers') diff --git a/server/controllers/activitypub/client.ts b/server/controllers/activitypub/client.ts index 767fde5d9..1c780783c 100644 --- a/server/controllers/activitypub/client.ts +++ b/server/controllers/activitypub/client.ts @@ -1,9 +1,8 @@ // Intercept ActivityPub client requests import * as express from 'express' -import { VideoPrivacy } from '../../../shared/models/videos' +import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos' import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' -import { pageToStartAndCount } from '../../helpers/core-utils' -import { ACTIVITY_PUB, CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' +import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers' import { buildVideoAnnounce } from '../../lib/activitypub/send' import { audiencify, getAudience } from '../../lib/activitypub/audience' import { createActivityData } from '../../lib/activitypub/send/send-create' @@ -18,6 +17,14 @@ import { VideoChannelModel } from '../../models/video/video-channel' import { VideoCommentModel } from '../../models/video/video-comment' import { VideoShareModel } from '../../models/video/video-share' import { cacheRoute } from '../../middlewares/cache' +import { activityPubResponse } from './utils' +import { AccountVideoRateModel } from '../../models/account/account-video-rate' +import { + getVideoCommentsActivityPubUrl, + getVideoDislikesActivityPubUrl, + getVideoLikesActivityPubUrl, + getVideoSharesActivityPubUrl +} from '../../lib/activitypub' const activityPubClientRouter = express.Router() @@ -116,10 +123,8 @@ async function accountFollowingController (req: express.Request, res: express.Re async function videoController (req: express.Request, res: express.Response, next: express.NextFunction) { const video: VideoModel = res.locals.video - // We need more attributes - const videoAll = await VideoModel.loadAndPopulateAll(video.id) const audience = await getAudience(video.VideoChannel.Account.Actor, undefined, video.privacy === VideoPrivacy.PUBLIC) - const videoObject = audiencify(videoAll.toActivityPubObject(), audience) + const videoObject = audiencify(video.toActivityPubObject(), audience) if (req.path.endsWith('/activity')) { const data = await createActivityData(video.url, video.VideoChannel.Account.Actor, videoObject, undefined, audience) @@ -139,41 +144,45 @@ async function videoAnnounceController (req: express.Request, res: express.Respo async function videoAnnouncesController (req: express.Request, res: express.Response, next: express.NextFunction) { const video: VideoModel = res.locals.video - // We need more attributes - const videoAll = await VideoModel.loadAndPopulateAll(video.id) - const object = videoAll.toAnnouncesActivityPubObject() + const handler = async (start: number, count: number) => { + const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count) + return { + total: result.count, + data: result.rows.map(r => r.url) + } + } + const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page) - return activityPubResponse(activityPubContextify(object), res) + return activityPubResponse(activityPubContextify(json), res) } async function videoLikesController (req: express.Request, res: express.Response, next: express.NextFunction) { const video: VideoModel = res.locals.video + const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video)) - // We need more attributes - const videoAll = await VideoModel.loadAndPopulateAll(video.id) - const { likesObject } = videoAll.toRatesActivityPubObjects() - - return activityPubResponse(activityPubContextify(likesObject), res) + return activityPubResponse(activityPubContextify(json), res) } async function videoDislikesController (req: express.Request, res: express.Response, next: express.NextFunction) { const video: VideoModel = res.locals.video + const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video)) - // We need more attributes - const videoAll = await VideoModel.loadAndPopulateAll(video.id) - const { dislikesObject } = videoAll.toRatesActivityPubObjects() - - return activityPubResponse(activityPubContextify(dislikesObject), res) + return activityPubResponse(activityPubContextify(json), res) } async function videoCommentsController (req: express.Request, res: express.Response, next: express.NextFunction) { const video: VideoModel = res.locals.video - // We need more attributes - const videoAll = await VideoModel.loadAndPopulateAll(video.id) - const commentsObject = videoAll.toCommentsActivityPubObject() + const handler = async (start: number, count: number) => { + const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count) + return { + total: result.count, + data: result.rows.map(r => r.url) + } + } + const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page) - return activityPubResponse(activityPubContextify(commentsObject), res) + return activityPubResponse(activityPubContextify(json), res) } async function videoChannelController (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -216,23 +225,28 @@ async function videoCommentController (req: express.Request, res: express.Respon // --------------------------------------------------------------------------- async function actorFollowing (req: express.Request, actor: ActorModel) { - const page = req.query.page || 1 - const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) + const handler = (start: number, count: number) => { + return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) + } - const result = await ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count) - return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) + return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) } async function actorFollowers (req: express.Request, actor: ActorModel) { - const page = req.query.page || 1 - const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) + const handler = (start: number, count: number) => { + return ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) + } - const result = await ActorFollowModel.listAcceptedFollowerUrlsForApi([ actor.id ], undefined, start, count) - return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, page, result) + return activityPubCollectionPagination(CONFIG.WEBSERVER.URL + req.url, handler, req.query.page) } -function activityPubResponse (data: any, res: express.Response) { - return res.type('application/activity+json; charset=utf-8') - .json(data) - .end() +function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) { + const handler = async (start: number, count: number) => { + const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count) + return { + total: result.count, + data: result.rows.map(r => r.Account.Actor.url) + } + } + return activityPubCollectionPagination(url, handler, req.query.page) } diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index c9e087a13..97bf9b052 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts @@ -1,16 +1,15 @@ import * as express from 'express' import { Activity } from '../../../shared/models/activitypub/activity' import { VideoPrivacy } from '../../../shared/models/videos' -import { activityPubCollectionPagination } from '../../helpers/activitypub' -import { pageToStartAndCount } from '../../helpers/core-utils' +import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub' import { logger } from '../../helpers/logger' -import { ACTIVITY_PUB } from '../../initializers/constants' import { announceActivityData, createActivityData } from '../../lib/activitypub/send' import { buildAudience } from '../../lib/activitypub/audience' import { asyncMiddleware, localAccountValidator } from '../../middlewares' import { AccountModel } from '../../models/account/account' import { ActorModel } from '../../models/activitypub/actor' import { VideoModel } from '../../models/video/video' +import { activityPubResponse } from './utils' const outboxRouter = express.Router() @@ -30,10 +29,17 @@ export { async function outboxController (req: express.Request, res: express.Response, next: express.NextFunction) { const account: AccountModel = res.locals.account const actor = account.Actor + const actorOutboxUrl = account.Actor.url + '/outbox' + + logger.info('Receiving outbox request for %s.', actorOutboxUrl) - const page = req.query.page || 1 - const { start, count } = pageToStartAndCount(page, ACTIVITY_PUB.COLLECTION_ITEMS_PER_PAGE) + const handler = (start: number, count: number) => buildActivities(actor, start, count) + const json = await activityPubCollectionPagination(actorOutboxUrl, handler, req.query.page) + + return activityPubResponse(activityPubContextify(json), res) +} +async function buildActivities (actor: ActorModel, start: number, count: number) { const data = await VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count) const activities: Activity[] = [] @@ -62,14 +68,8 @@ async function outboxController (req: express.Request, res: express.Response, ne } } - const newResult = { + return { data: activities, total: data.total } - const actorOutboxUrl = account.Actor.url + '/outbox' - const json = activityPubCollectionPagination(actorOutboxUrl, page, newResult) - - logger.info('Receiving outbox request for %s.', actorOutboxUrl) - - return res.json(json).end() } diff --git a/server/controllers/activitypub/utils.ts b/server/controllers/activitypub/utils.ts new file mode 100644 index 000000000..599cf48ab --- /dev/null +++ b/server/controllers/activitypub/utils.ts @@ -0,0 +1,11 @@ +import * as express from 'express' + +function activityPubResponse (data: any, res: express.Response) { + return res.type('application/activity+json; charset=utf-8') + .json(data) + .end() +} + +export { + activityPubResponse +} -- cgit v1.2.3