diff --git a/server/controllers/api/channel/availability/checkChannelAvailability.js b/server/controllers/api/channel/availability/checkChannelAvailability.js index 3c5d78b5..3261c63f 100644 --- a/server/controllers/api/channel/availability/checkChannelAvailability.js +++ b/server/controllers/api/channel/availability/checkChannelAvailability.js @@ -3,13 +3,12 @@ const db = require('../../../../models'); const checkChannelAvailability = (name) => { return db.Channel .findAll({ - where: { channelName: name }, + where: { + channelName: name, + }, }) .then(result => { - if (result.length >= 1) { - throw new Error('That channel has already been claimed'); - } - return name; + return (result.length <= 0); }) .catch(error => { throw error; diff --git a/server/controllers/api/channel/availability/index.js b/server/controllers/api/channel/availability/index.js index ae2a3807..dace8e01 100644 --- a/server/controllers/api/channel/availability/index.js +++ b/server/controllers/api/channel/availability/index.js @@ -8,12 +8,29 @@ const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); */ +function addAtSymbolIfNecessary (name) { + if (name.substring(0,1) !== '@') { + return `@${name}`; + } + return name; +} + const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => { const gaStartTime = Date.now(); + name = addAtSymbolIfNecessary(name); checkChannelAvailability(name) - .then(availableName => { - res.status(200).json(availableName); - sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); + .then(isAvailable => { + let responseObject = { + success: true, + data: isAvailable, + }; + if (isAvailable) { + responseObject['message'] = `${name} is available` + } else { + responseObject['message'] = `${name} is already in use` + } + res.status(200).json(responseObject); + sendGATimingEvent('end-to-end', 'channel name availability', name, gaStartTime, Date.now()); }) .catch(error => { handleErrorResponse(originalUrl, ip, error, res); diff --git a/server/controllers/api/claim/availability/checkClaimAvailability.js b/server/controllers/api/claim/availability/checkClaimAvailability.js index c4f4501a..68c31056 100644 --- a/server/controllers/api/claim/availability/checkClaimAvailability.js +++ b/server/controllers/api/claim/availability/checkClaimAvailability.js @@ -1,4 +1,4 @@ -const db = require('../../../../models/index'); +const db = require('../../../../models'); const { publishing: { primaryClaimAddress, additionalClaimAddresses } } = require('../../../../../config/siteConfig.js'); const Sequelize = require('sequelize'); const Op = Sequelize.Op; @@ -18,10 +18,7 @@ const claimAvailability = (name) => { }, }) .then(result => { - if (result.length >= 1) { - throw new Error('That claim is already in use'); - } - return name; + return (result.length <= 0); }) .catch(error => { throw error; diff --git a/server/controllers/api/claim/availability/index.js b/server/controllers/api/claim/availability/index.js index 1583cb26..da982e8f 100644 --- a/server/controllers/api/claim/availability/index.js +++ b/server/controllers/api/claim/availability/index.js @@ -11,8 +11,17 @@ const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => { const gaStartTime = Date.now(); checkClaimAvailability(name) - .then(result => { - res.status(200).json(result); + .then(isAvailable => { + let responseObject = { + success: true, + data: isAvailable, + }; + if (isAvailable) { + responseObject['message'] = `That claim name is available` + } else { + responseObject['message'] = `That url is already in use` + } + res.status(200).json(responseObject); sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); }) .catch(error => { diff --git a/server/controllers/api/claim/data/index.js b/server/controllers/api/claim/data/index.js index 49361d71..250e9849 100644 --- a/server/controllers/api/claim/data/index.js +++ b/server/controllers/api/claim/data/index.js @@ -14,9 +14,15 @@ const claimData = ({ ip, originalUrl, body, params }, res) => { db.Claim.resolveClaim(claimName, claimId) .then(claimInfo => { if (!claimInfo) { - return res.status(404).json({success: false, message: 'No claim could be found'}); + return res.status(404).json({ + success: false, + message: 'No claim could be found' + }); } - res.status(200).json({success: true, data: claimInfo}); + res.status(200).json({ + success: true, + data: claimInfo + }); }) .catch(error => { handleErrorResponse(originalUrl, ip, error, res); diff --git a/server/controllers/assets/utils/serveFile.js b/server/controllers/assets/utils/serveFile.js index ab3c167a..aa0f2e34 100644 --- a/server/controllers/assets/utils/serveFile.js +++ b/server/controllers/assets/utils/serveFile.js @@ -8,7 +8,12 @@ const serveFile = ({ filePath, fileType }, res) => { 'Content-Type' : fileType || 'image/jpeg', }, }; - res.status(200).sendFile(filePath, sendFileOptions); + res.status(200).sendFile(filePath, sendFileOptions, (error) => { + if (error) { + logger.warn(error); + res.status(404).send(); + } + }); }; module.exports = serveFile; diff --git a/server/controllers/auth/logout/index.js b/server/controllers/auth/logout/index.js index 13d945b1..e79ee7c0 100644 --- a/server/controllers/auth/logout/index.js +++ b/server/controllers/auth/logout/index.js @@ -1,6 +1,10 @@ const logout = (req, res) => { req.logout(); - res.status(200).json({success: true, message: 'you successfully logged out'}); + const responseObject = { + success: true, + message: 'you successfully logged out' + }; + res.status(200).json(responseObject); }; module.exports = logout; diff --git a/server/controllers/auth/user/index.js b/server/controllers/auth/user/index.js index 9e776bbd..7cb1bdd0 100644 --- a/server/controllers/auth/user/index.js +++ b/server/controllers/auth/user/index.js @@ -1,9 +1,9 @@ const user = (req, res) => { - if (req.user) { - res.status(200).json({success: true, data: req.user}); - } else { - res.status(401).json({success: false, message: 'user is not logged in'}); - } + const responseObject = { + success: true, + data: req.user, + }; + res.status(200).json(responseObject); }; module.exports = user;