Add views to claim pages for logged in channels
This commit is contained in:
parent
51dd0c637f
commit
fabc44edcd
10 changed files with 164 additions and 14 deletions
|
@ -91,10 +91,17 @@ export function addRequestToRequestList (id, error, key) {
|
||||||
|
|
||||||
// asset actions
|
// asset actions
|
||||||
|
|
||||||
export function addAssetToAssetList (id, error, name, claimId, shortId, claimData) {
|
export function addAssetToAssetList (id, error, name, claimId, shortId, claimData, claimViews) {
|
||||||
return {
|
return {
|
||||||
type: actions.ASSET_ADD,
|
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 },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,3 +37,8 @@ export function checkClaimAvailability (claim) {
|
||||||
const url = `/api/claim/availability/${claim}`;
|
const url = `/api/claim/availability/${claim}`;
|
||||||
return Request(url);
|
return Request(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getClaimViews (claimId) {
|
||||||
|
const url = `/api/claim/views/${claimId}`;
|
||||||
|
return Request(url);
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,8 @@ export const SPECIAL_ASSET_REQUEST_NEW = 'SPECIAL_ASSET_REQUEST_NEW';
|
||||||
export const REQUEST_LIST_ADD = 'REQUEST_LIST_ADD';
|
export const REQUEST_LIST_ADD = 'REQUEST_LIST_ADD';
|
||||||
|
|
||||||
// asset actions
|
// asset actions
|
||||||
export const ASSET_ADD = `ASSET_ADD`;
|
export const ASSET_ADD = 'ASSET_ADD';
|
||||||
|
export const ASSET_VIEWS_UPDATE = 'ASSET_VIEWS_UPDATE';
|
||||||
|
|
||||||
// channel actions
|
// channel actions
|
||||||
export const CHANNEL_ADD = 'CHANNEL_ADD';
|
export const CHANNEL_ADD = 'CHANNEL_ADD';
|
||||||
|
|
|
@ -9,7 +9,16 @@ import ClickToCopy from '@components/ClickToCopy';
|
||||||
|
|
||||||
class AssetInfo extends React.Component {
|
class AssetInfo extends React.Component {
|
||||||
render () {
|
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 (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{channelName && (
|
{channelName && (
|
||||||
|
@ -27,6 +36,21 @@ class AssetInfo extends React.Component {
|
||||||
</Row>
|
</Row>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{claimViews && (
|
||||||
|
<Row>
|
||||||
|
<RowLabeled
|
||||||
|
label={
|
||||||
|
<Label value={'Views:'} />
|
||||||
|
}
|
||||||
|
content={
|
||||||
|
<span className='text'>
|
||||||
|
{claimViews}
|
||||||
|
</span>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</Row>
|
||||||
|
)}
|
||||||
|
|
||||||
<Row>
|
<Row>
|
||||||
<RowLabeled
|
<RowLabeled
|
||||||
label={
|
label={
|
||||||
|
|
|
@ -51,7 +51,17 @@ export default function (state = initialState, action) {
|
||||||
name : action.data.name,
|
name : action.data.name,
|
||||||
claimId : action.data.claimId,
|
claimId : action.data.claimId,
|
||||||
shortId : action.data.shortId,
|
shortId : action.data.shortId,
|
||||||
claimData: action.data.claimData,
|
claimData : action.data.claimData,
|
||||||
|
claimViews: action.data.claimViews,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
case actions.ASSET_VIEWS_UPDATE:
|
||||||
|
return Object.assign({}, state, {
|
||||||
|
assetList: Object.assign({}, state.assetList, {
|
||||||
|
[action.data.id]: {
|
||||||
|
...state.assetList[action.data.id],
|
||||||
|
claimViews: action.data.claimViews,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { all } from 'redux-saga/effects';
|
import { all } from 'redux-saga/effects';
|
||||||
import { watchHandleShowPageUri, watchHandleShowHomepage } from './show_uri';
|
import { watchHandleShowPageUri, watchHandleShowHomepage } from './show_uri';
|
||||||
import { watchNewAssetRequest } from './show_asset';
|
import { watchNewAssetRequest, watchUpdateAssetViews } from './show_asset';
|
||||||
import { watchNewChannelRequest, watchUpdateChannelClaims } from './show_channel';
|
import { watchNewChannelRequest, watchUpdateChannelClaims } from './show_channel';
|
||||||
import { watchNewSpecialAssetRequest } from './show_special';
|
import { watchNewSpecialAssetRequest } from './show_special';
|
||||||
import { watchFileIsRequested } from './file';
|
import { watchFileIsRequested } from './file';
|
||||||
|
@ -26,5 +26,6 @@ export function * rootSaga () {
|
||||||
watchChannelCreate(),
|
watchChannelCreate(),
|
||||||
watchChannelLoginCheck(),
|
watchChannelLoginCheck(),
|
||||||
watchChannelLogout(),
|
watchChannelLogout(),
|
||||||
|
watchUpdateAssetViews(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
import { call, put, select, takeLatest } from 'redux-saga/effects';
|
import { call, put, select, takeLatest } from 'redux-saga/effects';
|
||||||
import * as actions from '../constants/show_action_types';
|
import * as actions from '../constants/show_action_types';
|
||||||
import { addRequestToRequestList, onRequestError, onRequestUpdate, addAssetToAssetList } from '../actions/show';
|
import * as channelActions from '../constants/channel_action_types';
|
||||||
import { getLongClaimId, getShortId, getClaimData } from '../api/assetApi';
|
import {
|
||||||
|
addRequestToRequestList,
|
||||||
|
onRequestError,
|
||||||
|
onRequestUpdate,
|
||||||
|
addAssetToAssetList,
|
||||||
|
updateAssetViewsInList,
|
||||||
|
} from '../actions/show';
|
||||||
|
import { getLongClaimId, getShortId, getClaimData, getClaimViews } from '../api/assetApi';
|
||||||
|
import { selectChannelState } from '../selectors/channel';
|
||||||
import { selectShowState } from '../selectors/show';
|
import { selectShowState } from '../selectors/show';
|
||||||
import { selectSiteHost } from '../selectors/site';
|
import { selectSiteHost } from '../selectors/site';
|
||||||
|
|
||||||
|
@ -37,19 +45,72 @@ export function * newAssetRequest (action) {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return yield put(onRequestError(error.message));
|
return yield put(onRequestError(error.message));
|
||||||
}
|
}
|
||||||
|
|
||||||
// get asset claim data
|
// get asset claim data
|
||||||
let claimData;
|
let claimData;
|
||||||
|
let claimViews = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
({data: claimData} = yield call(getClaimData, host, name, longId));
|
({data: claimData} = yield call(getClaimData, host, name, longId));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return yield put(onRequestError(error.message));
|
return yield put(onRequestError(error.message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { loggedInChannel } = yield select(selectChannelState);
|
||||||
|
|
||||||
|
if(loggedInChannel && loggedInChannel.longId) {
|
||||||
|
const {
|
||||||
|
data: claimViewData
|
||||||
|
} = yield call(getClaimViews, longId);
|
||||||
|
|
||||||
|
claimViews = claimViewData[longId] || 0;
|
||||||
|
}
|
||||||
|
} catch (error) { }
|
||||||
|
|
||||||
// add asset to asset list
|
// add asset to asset list
|
||||||
yield put(addAssetToAssetList(assetKey, null, name, longId, shortId, claimData));
|
yield put(addAssetToAssetList(assetKey, null, name, longId, shortId, claimData, claimViews));
|
||||||
// clear any errors in request error
|
// clear any errors in request error
|
||||||
yield put(onRequestError(null));
|
yield put(onRequestError(null));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function * updateAssetViews (action) {
|
||||||
|
// update each loaded claim that's in the loggedInChannel
|
||||||
|
try {
|
||||||
|
const showState = yield select(selectShowState);
|
||||||
|
const { data: loggedInChannel } = action;
|
||||||
|
|
||||||
|
const channelId = loggedInChannel.longId;
|
||||||
|
|
||||||
|
for(let key in showState.assetList) {
|
||||||
|
let asset = showState.assetList[key];
|
||||||
|
|
||||||
|
if(asset.claimData && asset.claimData.certificateId === channelId) {
|
||||||
|
const longId = asset.claimId;
|
||||||
|
const assetKey = `a#${asset.name}#${longId}`;
|
||||||
|
|
||||||
|
let claimViews = null;
|
||||||
|
|
||||||
|
if(longId) {
|
||||||
|
const {
|
||||||
|
data: claimViewData
|
||||||
|
} = yield call(getClaimViews, longId);
|
||||||
|
|
||||||
|
claimViews = claimViewData[longId] || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
yield put(updateAssetViewsInList(assetKey, longId, claimViews));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export function * watchUpdateAssetViews (action) {
|
||||||
|
yield takeLatest(channelActions.CHANNEL_UPDATE, updateAssetViews)
|
||||||
|
};
|
||||||
|
|
||||||
export function * watchNewAssetRequest () {
|
export function * watchNewAssetRequest () {
|
||||||
yield takeLatest(actions.ASSET_REQUEST_NEW, newAssetRequest);
|
yield takeLatest(actions.ASSET_REQUEST_NEW, newAssetRequest);
|
||||||
};
|
};
|
||||||
|
|
29
server/controllers/api/claim/views/index.js
Normal file
29
server/controllers/api/claim/views/index.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
||||||
|
const db = require('server/models');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to return data for a claim
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimViews = async ({ ip, originalUrl, body, params }, res) => {
|
||||||
|
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;
|
|
@ -51,7 +51,17 @@ module.exports = (sequelize, { BOOLEAN, DATE, STRING }) => {
|
||||||
`SELECT ${selectString} FROM Views WHERE time > '${sqlTime}' GROUP BY ${groupString}`,
|
`SELECT ${selectString} FROM Views WHERE time > '${sqlTime}' GROUP BY ${groupString}`,
|
||||||
{ type: sequelize.QueryTypes.SELECT }
|
{ type: sequelize.QueryTypes.SELECT }
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Views.getGetUniqueViewsbByClaimId = (claimId) => {
|
||||||
|
return Views.count({
|
||||||
|
where: {
|
||||||
|
claimId,
|
||||||
|
},
|
||||||
|
distinct: true,
|
||||||
|
col: 'ip'
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
return Views;
|
return Views;
|
||||||
};
|
};
|
||||||
|
|
|
@ -16,6 +16,7 @@ const claimLongId = require('../../controllers/api/claim/longId');
|
||||||
const claimPublish = require('../../controllers/api/claim/publish');
|
const claimPublish = require('../../controllers/api/claim/publish');
|
||||||
const claimResolve = require('../../controllers/api/claim/resolve');
|
const claimResolve = require('../../controllers/api/claim/resolve');
|
||||||
const claimShortId = require('../../controllers/api/claim/shortId');
|
const claimShortId = require('../../controllers/api/claim/shortId');
|
||||||
|
const claimViews = require('../../controllers/api/claim/views');
|
||||||
const fileAvailability = require('../../controllers/api/file/availability');
|
const fileAvailability = require('../../controllers/api/file/availability');
|
||||||
const specialClaims = require('../../controllers/api/special/claims');
|
const specialClaims = require('../../controllers/api/special/claims');
|
||||||
const userPassword = require('../../controllers/api/user/password');
|
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/publish': { method: 'post', controller: [ torCheckMiddleware, autoblockPublishMiddleware, multipartMiddleware, claimPublish ] },
|
||||||
'/api/claim/resolve/:name/:claimId': { controller: [ torCheckMiddleware, claimResolve ] },
|
'/api/claim/resolve/:name/:claimId': { controller: [ torCheckMiddleware, claimResolve ] },
|
||||||
'/api/claim/short-id/:longId/:name': { controller: [ torCheckMiddleware, claimShortId ] },
|
'/api/claim/short-id/:longId/:name': { controller: [ torCheckMiddleware, claimShortId ] },
|
||||||
|
'/api/claim/views/:claimId': { controller: [ torCheckMiddleware, claimViews ] },
|
||||||
// file routes
|
// file routes
|
||||||
'/api/file/availability/:name/:claimId': { controller: [ torCheckMiddleware, fileAvailability ] },
|
'/api/file/availability/:name/:claimId': { controller: [ torCheckMiddleware, fileAvailability ] },
|
||||||
// user routes
|
// user routes
|
||||||
|
|
Loading…
Add table
Reference in a new issue