How to get Details of All the Posts a Person Upvoted in Steemit
Repository
https://github.com/steemit/steem-js
What Will I Learn?
- You will learn How to get Votes Made by User
- You will learn How to get Details about the Posts where Upvote is Made
Requirements
- Basic Understanding of Promises in JavaScript
Difficulty
- Basic
Tutorial Contents
This will be a very short tutorial where we will learn about getting all the posts you voted on whether it's a self-post or others posts. For this, you need to know two basic functions of Steem.js and they are getAccountVotes() and getContent().
getAccountVotes() gives you details about all the posts you have upvoted, where voter is the username without @. It will give you an array of objects. The object has authorperm which is a permalink of the post you voted, weight of the upvoted, reshares, percentage and the time as shown below. As you can see the time goes back to 2017, which means it gives me the upvotes from the start.
steem.api.getAccountVotes('codingdefined', function(err, result) {
console.log(err, result);
});
Since I do not want to know the upvotes made by in 2017, so what I am gonna do is to reverse the array based on time so that I will get the latest upvote as shown below.
function compare(a,b) {
if (a.time < b.time)
return -1;
if (a.time > b.time)
return 1;
return 0;
}
steem.api.getAccountVotes('codingdefined', function (err, resp) {
if (err) console.log(err)
resp.sort(compare);
resp.reverse();
console.log(resp);
});
I have written a compare method, Sorted by Votes array and then reversed it. The sorting algorithm is for ascending order though I can change the logic, I just kept if anyone needs the array as an ascending order. Now I have the array which is sorted based on the most recent upvote.
Next is to get the Content of the Post which I have upvoted, for that we will be using Promises as we have to call the Steemit Functions Asynchrously inside the loop of the array. Once done we will be combining all the promises and then create the list. Since there are quite a few data, so we will be taking only last 10 upvotes and then get the details.
The Code of the same is, where we are first taking the 10 objects from the array and then looping it through the map function. Inside the loop, we are splittling the authorperm on '/' to get the author and permalink and then calling getContent asynchronously. At last we are combining all the promises and then displaying the result.
function compare(a,b) {
if (a.time < b.time)
return -1;
if (a.time > b.time)
return 1;
return 0;
}
steem.api.getAccountVotes('codingdefined', function (err, resp) {
if (err) console.log(err)
resp.sort(compare);
resp.reverse();
let promises = resp.slice(0, 10).map(r => {
const autper = r.authorperm.split('/', 2);
return steem.api.getContentAsync(autper[0], autper[1])
.then((result) => {
return result
});
})
Promise.all(promises)
.then(results => {
console.log(results);
})
.catch(e => {
console.error(e);
})
})
As you can see below my last upvote was on https://steemit.com/utopian-io/@amosbastian/introducing-football-a-python-wrapper-for-the-football-data-api.
Thus this simple code gives you the understanding about how to combine two functions and get the required results.
Resources
https://github.com/steemit/steem-js/tree/master/doc
Proof of Work Done
https://github.com/codingdefined