https://github.com/knacksteem/knacksteem-api
https://github.com/knacksteem/knacksteem-api/pull/4
As mentioned in the KnackSteem repository on Github, This project is meant for people with great skills to showcase them and earn reward while doing so.
When the frontend is completed as well, the KnackSteem platform will become clearer :)
const controller = require('../../controllers/posts.controller');
router.route('/:author/:permlink').get(controller.getSinglePost)
If the post is successfully queried, the API performs all parsing operations to make implemetation easier on the frontend.
/**
* Method to get a single post from Steem Blockchain
* @param {*} req
* @param {*} res
* @author Huseyin Terkir (hsynterkr)
* @returns an object with the post from Steem Blockchain
* @public
*/
exports.getSinglePost = (req, res, next) => {
let author = req.params.author;
let permlink = req.params.permlink;
if (author === null || author === undefined || author === '') {
return next(helper.ReturnError(500, 'Required parameter "author" is missing.', 'Internal'));
} else if (permlink === null || permlink === undefined || permlink === '') {
return next(helper.ReturnError(500, 'Required parameter "permlink" is missing.', 'Internal'));
}
client.sendAsync('get_content', [author, permlink]).then(post => {
if (!post.author || !post.permlink) {
return next(helper.ReturnError(404, 'Required parameter "permlink" or "author" is wrong!', 'Not Found'));
}
post.json_metadata = JSON.parse(post.json_metadata);
// Get body image of the post.
post.image = post.json_metadata.image[0];
// Use steem formatter to format reputation
post.author_reputation = steem.formatter.reputation(post.author_reputation);
// Calculate total payout for vote values
let totalPayout =
parseFloat(post.pending_payout_value) +
parseFloat(post.total_payout_value) +
parseFloat(post.curator_payout_value);
for(i in post.beneficiaries) {
post.beneficiaries[i].weight = (post.beneficiaries[i].weight)/100
}
// Calculate recent voteRshares and ratio values.
let voteRshares = post.active_votes.reduce((a, b) => a + parseFloat(b.rshares), 0);
let ratio = totalPayout / voteRshares;
// Calculate exact values of votes
for(i in post.active_votes) {
post.active_votes[i].value = (post.active_votes[i].rshares * ratio).toFixed(2);
post.active_votes[i].reputation = steem.formatter.reputation(post.active_votes[i].reputation);
post.active_votes[i].percent = post.active_votes[i].percent / 100;
post.active_votes[i].profile_image = 'https://steemitimages.com/u/' + post.active_votes[i].voter + '/avatar/small'
}
// Sort votes by vote value
let active_votes = post.active_votes.slice(0);
active_votes.sort((a,b) => {
return b.value - a.value
})
return res.json(post);
}).catch(err => console.log(err));
};
isVoted() function is used to determine whether a user votes for a particular post. isVoted() function gets 2 parameters, vote array and username.
The for loop checks the array and determines whether the given user has voted the post.
/**
* Method to validate vote.
* @param {Number} array
* @param {String} user
* @description It will return true if the user has voted, otherwise it will return false.
* @returns boolean (is voted or not)
*/
exports.isVoted = (array, user) => {
for (let vote of array) {
if (vote.voter === user) {
return true;
}
}
return false;
}
ReturnError() is a function that will be called when an error occurs.
It takes 3 parameters. Error status, Error Message and Error Type
/**
* Method to generate JSON for error
* @param {Number} code
* @param {String} msg
* @param {String} type
*/
exports.ReturnError = (code, msg, type) => {
return {
status: code,
message: msg,
type: type
}
}
In my next contribution; I will create /new /hot and /trending endpoints to list the posts under the #knacksteem tag.