Merge pull request #434 from lbryio/claim-availability-check

Claim availability check
This commit is contained in:
Bill Bittner 2018-05-06 11:22:37 -07:00 committed by GitHub
commit c0838bbe54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 24 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -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 => {

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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;