KnackSteem API Updates
Repository
https://github.com/knacksteem/knacksteem-api
Pull Request
https://github.com/knacksteem/knacksteem-api/pull/4
What is the KnackSteem?
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 :)
New Features
- Steem API
I used the light-rpc library for the Steem API because I used it before and it's really useful, fast and light :) We used it for backend of Steemia app before.
I created a route to query posts from blockchain. This route takes the author and permalink parameters, request queries the post from blockchain with controller and returns the response with post.
- Single Post Route
const controller = require('../../controllers/posts.controller');
router.route('/:author/:permlink').get(controller.getSinglePost)
- Post Controller
The Post Controller queries the post from the Steem API with the parameters. If one of the parameters is missing or incorrect, it returns an error. In addition, if an error is received while querying the post, the API returns an error indicating that the parameters are incorrect.
If the post is successfully queried, the API performs all parsing operations to make implemetation easier on the frontend.
- json_metadata is parsed in JSON format.
- exact author reputation is calculated.
- vote values are calculated in SBD.
- votes are sorted by SBD value.
/**
* 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));
};
- Helper Util Class
I've created a utility class to improve code usability, and keep code quality high. Currently there are only 2 functions: isVoted() and ReturnError()
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
}
}
What is next?
In my next contribution; I will create /new /hot and /trending endpoints to list the posts under the #knacksteem tag.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]
Thank you @helo,
Frontend is also in development :)
Hey @hsynterkr
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!