2018-04-27 18:54:36 +02:00
|
|
|
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
2018-04-27 19:24:40 +02:00
|
|
|
const getChannelData = require('./getChannelData.js');
|
2018-09-21 22:16:01 +02:00
|
|
|
const isApprovedChannel = require('../../../../../utils/isApprovedChannel');
|
|
|
|
const { publishing: { serveOnlyApproved, approvedChannels } } = require('@config/siteConfig');
|
2018-03-29 02:35:41 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
route to get data for a channel
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-03-29 20:24:52 +02:00
|
|
|
const channelData = ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
const channelName = params.channelName;
|
|
|
|
let channelClaimId = params.channelClaimId;
|
|
|
|
if (channelClaimId === 'none') channelClaimId = null;
|
2018-09-20 19:34:53 +02:00
|
|
|
|
|
|
|
const chanObj = {};
|
|
|
|
if (channelName) chanObj.name = channelName;
|
|
|
|
if (channelClaimId) chanObj[(channelClaimId.length === 40 ? 'longId' : 'shortId')] = channelClaimId;
|
2018-09-21 22:16:01 +02:00
|
|
|
if (serveOnlyApproved && !isApprovedChannel(chanObj, approvedChannels)) {
|
2018-09-17 15:01:46 +02:00
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
2018-09-20 20:09:01 +02:00
|
|
|
message: 'This content is unavailable',
|
2018-09-17 15:01:46 +02:00
|
|
|
});
|
|
|
|
}
|
2018-09-20 19:34:53 +02:00
|
|
|
|
2018-05-01 00:58:04 +02:00
|
|
|
getChannelData(channelName, channelClaimId)
|
2018-03-29 20:24:52 +02:00
|
|
|
.then(data => {
|
2018-05-01 00:58:04 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2018-08-01 01:43:08 +02:00
|
|
|
data,
|
2018-05-01 00:58:04 +02:00
|
|
|
});
|
2018-03-29 20:24:52 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-05-01 00:58:04 +02:00
|
|
|
if (error === NO_CHANNEL) {
|
|
|
|
return res.status(404).json({
|
|
|
|
success: false,
|
|
|
|
message: 'No matching channel was found',
|
|
|
|
});
|
|
|
|
}
|
2018-03-29 20:24:52 +02:00
|
|
|
handleErrorResponse(originalUrl, ip, error, res);
|
|
|
|
});
|
2018-03-29 02:35:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = channelData;
|