From fabc44edcdf94d0133e51b2022d9822b82e892c4 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 29 Oct 2018 02:04:43 -0500 Subject: [PATCH 1/2] Add views to claim pages for logged in channels --- client/src/actions/show.js | 11 +++- client/src/api/assetApi.js | 5 ++ client/src/constants/show_action_types.js | 3 +- client/src/containers/AssetInfo/view.jsx | 26 +++++++- client/src/reducers/show.js | 20 ++++-- client/src/sagas/rootSaga.js | 3 +- client/src/sagas/show_asset.js | 67 ++++++++++++++++++++- server/controllers/api/claim/views/index.js | 29 +++++++++ server/models/views.js | 12 +++- server/routes/api/index.js | 2 + 10 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 server/controllers/api/claim/views/index.js diff --git a/client/src/actions/show.js b/client/src/actions/show.js index bdffb1df..1bac1310 100644 --- a/client/src/actions/show.js +++ b/client/src/actions/show.js @@ -91,10 +91,17 @@ export function addRequestToRequestList (id, error, key) { // asset actions -export function addAssetToAssetList (id, error, name, claimId, shortId, claimData) { +export function addAssetToAssetList (id, error, name, claimId, shortId, claimData, claimViews) { return { type: actions.ASSET_ADD, - data: { id, error, name, claimId, shortId, claimData }, + data: { id, error, name, claimId, shortId, claimData, claimViews }, + }; +} + +export function updateAssetViewsInList (id, claimId, claimViews) { + return { + type: actions.ASSET_VIEWS_UPDATE, + data: { id, claimId, claimViews }, }; } diff --git a/client/src/api/assetApi.js b/client/src/api/assetApi.js index 20e09b21..5290d19f 100644 --- a/client/src/api/assetApi.js +++ b/client/src/api/assetApi.js @@ -37,3 +37,8 @@ export function checkClaimAvailability (claim) { const url = `/api/claim/availability/${claim}`; return Request(url); } + +export function getClaimViews (claimId) { + const url = `/api/claim/views/${claimId}`; + return Request(url); +} diff --git a/client/src/constants/show_action_types.js b/client/src/constants/show_action_types.js index 02c79813..50996b2c 100644 --- a/client/src/constants/show_action_types.js +++ b/client/src/constants/show_action_types.js @@ -9,7 +9,8 @@ export const SPECIAL_ASSET_REQUEST_NEW = 'SPECIAL_ASSET_REQUEST_NEW'; export const REQUEST_LIST_ADD = 'REQUEST_LIST_ADD'; // asset actions -export const ASSET_ADD = `ASSET_ADD`; +export const ASSET_ADD = 'ASSET_ADD'; +export const ASSET_VIEWS_UPDATE = 'ASSET_VIEWS_UPDATE'; // channel actions export const CHANNEL_ADD = 'CHANNEL_ADD'; diff --git a/client/src/containers/AssetInfo/view.jsx b/client/src/containers/AssetInfo/view.jsx index ccf88fbd..10ec295c 100644 --- a/client/src/containers/AssetInfo/view.jsx +++ b/client/src/containers/AssetInfo/view.jsx @@ -9,7 +9,16 @@ import ClickToCopy from '@components/ClickToCopy'; class AssetInfo extends React.Component { render () { - const { asset: { shortId, claimData : { channelName, certificateId, description, name, claimId, fileExt, contentType, thumbnail, host } } } = this.props; + const { + asset: { + shortId, + claimData : { + channelName, certificateId, description, name, claimId, fileExt, contentType, thumbnail, host + }, + claimViews, + } + } = this.props; + return (
{channelName && ( @@ -27,6 +36,21 @@ class AssetInfo extends React.Component { )} + {claimViews && ( + + + } + content={ + + {claimViews} + + } + /> + + )} + { + const claimName = params.claimName; + let claimId = params.claimId; + if (claimId === 'none') claimId = null; + + try { + const viewCount = await db.Views.getGetUniqueViewsbByClaimId(claimId); + + res.status(200).json({ + success: true, + data : { + [claimId]: viewCount, + }, + }); + } catch(error) { + handleErrorResponse(originalUrl, ip, error, res); + } +}; + +module.exports = claimViews; diff --git a/server/models/views.js b/server/models/views.js index 1e6a9769..ccf49e92 100644 --- a/server/models/views.js +++ b/server/models/views.js @@ -51,7 +51,17 @@ module.exports = (sequelize, { BOOLEAN, DATE, STRING }) => { `SELECT ${selectString} FROM Views WHERE time > '${sqlTime}' GROUP BY ${groupString}`, { type: sequelize.QueryTypes.SELECT } ); - } + }; + + Views.getGetUniqueViewsbByClaimId = (claimId) => { + return Views.count({ + where: { + claimId, + }, + distinct: true, + col: 'ip' + }) + }; return Views; }; diff --git a/server/routes/api/index.js b/server/routes/api/index.js index 150a61c9..6f102dac 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -16,6 +16,7 @@ const claimLongId = require('../../controllers/api/claim/longId'); const claimPublish = require('../../controllers/api/claim/publish'); const claimResolve = require('../../controllers/api/claim/resolve'); const claimShortId = require('../../controllers/api/claim/shortId'); +const claimViews = require('../../controllers/api/claim/views'); const fileAvailability = require('../../controllers/api/file/availability'); const specialClaims = require('../../controllers/api/special/claims'); const userPassword = require('../../controllers/api/user/password'); @@ -97,6 +98,7 @@ module.exports = { '/api/claim/publish': { method: 'post', controller: [ torCheckMiddleware, autoblockPublishMiddleware, multipartMiddleware, claimPublish ] }, '/api/claim/resolve/:name/:claimId': { controller: [ torCheckMiddleware, claimResolve ] }, '/api/claim/short-id/:longId/:name': { controller: [ torCheckMiddleware, claimShortId ] }, + '/api/claim/views/:claimId': { controller: [ torCheckMiddleware, claimViews ] }, // file routes '/api/file/availability/:name/:claimId': { controller: [ torCheckMiddleware, fileAvailability ] }, // user routes From 0ebddd5311c585c423aa0b3a0d39f8f893857391 Mon Sep 17 00:00:00 2001 From: Shawn Date: Mon, 29 Oct 2018 14:09:42 -0500 Subject: [PATCH 2/2] Remove unused var --- server/controllers/api/claim/views/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/server/controllers/api/claim/views/index.js b/server/controllers/api/claim/views/index.js index 31fc9e33..f3a1c993 100644 --- a/server/controllers/api/claim/views/index.js +++ b/server/controllers/api/claim/views/index.js @@ -8,7 +8,6 @@ const db = require('server/models'); */ const claimViews = async ({ ip, originalUrl, body, params }, res) => { - const claimName = params.claimName; let claimId = params.claimId; if (claimId === 'none') claimId = null;