From e5aff3f3663b8828e2208e21b6134d6e000b3519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Wed, 28 Mar 2018 14:06:23 +0200 Subject: [PATCH] Extract layergroup-metadata middleware --- lib/cartodb/controllers/map/anonymous.js | 62 +------------------ .../map/middlewares/layergroup-metadata.js | 14 +++++ lib/cartodb/controllers/map/named.js | 62 +------------------ 3 files changed, 18 insertions(+), 120 deletions(-) create mode 100644 lib/cartodb/controllers/map/middlewares/layergroup-metadata.js diff --git a/lib/cartodb/controllers/map/anonymous.js b/lib/cartodb/controllers/map/anonymous.js index 2ddd12c7..665ba799 100644 --- a/lib/cartodb/controllers/map/anonymous.js +++ b/lib/cartodb/controllers/map/anonymous.js @@ -18,6 +18,7 @@ const lastModifiedHeader = require('../../middleware/last-modified-header'); const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup'); const layerStats = require('./middlewares/layer-stats'); const layergroupIdHeader = require('./middlewares/layergroup-id-header'); +const layergroupMetadata = require('./middlewares/layergroup-metadata'); const sendResponse = require('../../middleware/send-response'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); const LayergroupMetadata = require('../../utils/layergroup-metadata'); @@ -105,11 +106,7 @@ AnonymousMapController.prototype.composeCreateMapMiddleware = function () { lastUpdatedTimeLayergroup(), layerStats(this.pgConnection, this.statsBackend), layergroupIdHeader(this.templateMaps ,useTemplateHash), - setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.layergroupMetadata), - setAnalysesMetadataToLayergroup(this.layergroupMetadata, includeQuery), - setTurboCartoMetadataToLayergroup(this.layergroupMetadata), - setAggregationMetadataToLayergroup(this.layergroupMetadata), - setTilejsonMetadataToLayergroup(this.layergroupMetadata), + layergroupMetadata(this.layergroupMetadata, includeQuery), sendResponse(), augmentError({ label, addContext }) ]; @@ -219,61 +216,6 @@ function createLayergroup (mapBackend, userLimitsApi, pgConnection, affectedTabl }; } -function setDataviewsAndWidgetsUrlsToLayergroupMetadata (layergroupMetadata) { - return function setDataviewsAndWidgetsUrlsToLayergroupMetadataMiddleware (req, res, next) { - const { user, mapConfig } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj()); - - next(); - }; -} - -function setAnalysesMetadataToLayergroup (layergroupMetadata, includeQuery) { - return function setAnalysesMetadataToLayergroupMiddleware (req, res, next) { - const { user, analysesResults = [] } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery); - - next(); - }; -} - -function setTurboCartoMetadataToLayergroup (layergroupMetadata) { - return function setTurboCartoMetadataToLayergroupMiddleware (req, res, next) { - const { mapConfig, context } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context); - - next(); - }; -} - -function setAggregationMetadataToLayergroup (layergroupMetadata) { - return function setAggregationMetadataToLayergroupMiddleware (req, res, next) { - const { mapConfig, context } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context); - - next(); - }; -} - -function setTilejsonMetadataToLayergroup (layergroupMetadata) { - return function augmentLayergroupTilejsonMiddleware (req, res, next) { - const { user, mapConfig } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig); - - next(); - }; -} - function augmentError (options) { const { addContext = false, label = 'MAPS CONTROLLER' } = options; diff --git a/lib/cartodb/controllers/map/middlewares/layergroup-metadata.js b/lib/cartodb/controllers/map/middlewares/layergroup-metadata.js new file mode 100644 index 00000000..301089df --- /dev/null +++ b/lib/cartodb/controllers/map/middlewares/layergroup-metadata.js @@ -0,0 +1,14 @@ +module.exports = function setMetadataToLayergroup (layergroupMetadata, includeQuery) { + return function setMetadataToLayergroupMiddleware (req, res, next) { + const { user, mapConfig, analysesResults = [], context } = res.locals; + const layergroup = res.body; + + layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj()); + layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery); + layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context); + layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context); + layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig); + + next(); + }; +}; diff --git a/lib/cartodb/controllers/map/named.js b/lib/cartodb/controllers/map/named.js index e599212e..5707053a 100644 --- a/lib/cartodb/controllers/map/named.js +++ b/lib/cartodb/controllers/map/named.js @@ -15,6 +15,7 @@ const lastModifiedHeader = require('../../middleware/last-modified-header'); const lastUpdatedTimeLayergroup = require('./middlewares/last-updated-time-layergroup'); const layerStats = require('./middlewares/layer-stats'); const layergroupIdHeader = require('./middlewares/layergroup-id-header'); +const layergroupMetadata = require('./middlewares/layergroup-metadata'); const sendResponse = require('../../middleware/send-response'); const NamedMapMapConfigProvider = require('../../models/mapconfig/provider/named-map-provider'); const CreateLayergroupMapConfigProvider = require('../../models/mapconfig/provider/create-layergroup-provider'); @@ -116,11 +117,7 @@ NamedMapController.prototype.composeInstantiateTemplateMiddleware = function () lastUpdatedTimeLayergroup(), layerStats(this.pgConnection, this.statsBackend), layergroupIdHeader(this.templateMaps ,useTemplateHash), - setDataviewsAndWidgetsUrlsToLayergroupMetadata(this.layergroupMetadata), - setAnalysesMetadataToLayergroup(this.layergroupMetadata, includeQuery), - setTurboCartoMetadataToLayergroup(this.layergroupMetadata), - setAggregationMetadataToLayergroup(this.layergroupMetadata), - setTilejsonMetadataToLayergroup(this.layergroupMetadata), + layergroupMetadata(this.layergroupMetadata, includeQuery), sendResponse(), augmentError({ label, addContext }) ]; @@ -226,61 +223,6 @@ function instantiateLayergroup (mapBackend, userLimitsApi, pgConnection, affecte }; } -function setDataviewsAndWidgetsUrlsToLayergroupMetadata (layergroupMetadata) { - return function setDataviewsAndWidgetsUrlsToLayergroupMetadataMiddleware (req, res, next) { - const { user, mapConfig } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addDataviewsAndWidgetsUrls(user, layergroup, mapConfig.obj()); - - next(); - }; -} - -function setAnalysesMetadataToLayergroup (layergroupMetadata, includeQuery) { - return function setAnalysesMetadataToLayergroupMiddleware (req, res, next) { - const { user, analysesResults = [] } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addAnalysesMetadata(user, layergroup, analysesResults, includeQuery); - - next(); - }; -} - -function setTurboCartoMetadataToLayergroup (layergroupMetadata) { - return function setTurboCartoMetadataToLayergroupMiddleware (req, res, next) { - const { mapConfig, context } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addTurboCartoContextMetadata(layergroup, mapConfig.obj(), context); - - next(); - }; -} - -function setAggregationMetadataToLayergroup (layergroupMetadata) { - return function setAggregationMetadataToLayergroupMiddleware (req, res, next) { - const { mapConfig, context } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addAggregationContextMetadata(layergroup, mapConfig.obj(), context); - - next(); - }; -} - -function setTilejsonMetadataToLayergroup (layergroupMetadata) { - return function augmentLayergroupTilejsonMiddleware (req, res, next) { - const { user, mapConfig } = res.locals; - const layergroup = res.body; - - layergroupMetadata.addTileJsonMetadata(layergroup, user, mapConfig); - - next(); - }; -} - function augmentError (options) { const { addContext = false, label = 'MAPS CONTROLLER' } = options;