How to - steem.js - All post details by upvotes

in utopian-io •  7 years ago  (edited)

Repository

https://github.com/steemit/steem-js

What Will I Learn?

  1. How to retrieve any vote a user took
  2. How to retrieve details from a post a user voted on

Requirements

  • JavaScript (ES6 / ES7) knowledge
  • Promises

Difficulty

Intermediate


In this tutorial I will show you how to get details of posts you upvoted using the steem.js API. Instead of callbacks I will use promises.


steem.js functions

To retrieve our votes and the corresponding posts we need the following API functions:

  • steem.api.getAccountVotes
  • steem.api.getContent

These two functions using callbacks to handle asynchrony. As we dont want to end in the "callback-hell" we are going to use promises:

function getAccountVotes(author) {
  return new Promise((resolve, reject) => {
    // get all votes for specific author
    steem.api.getAccountVotes(author, (error, result) => {
      // reject if error
      if (error) {
        reject(error);
      // return votes if success
      } else {
        resolve(result);
      }
    });
  });
}

function getContent(author, permlink) {
  return new Promise((resolve, reject) => {
    // get all details for specific authors post
    steem.api.getContent(author, permlink, (error, result) => {
      if (error) {
        // reject if error
        reject(error);
      } else {
        // return post details if success
        resolve(result);
      }
    });
  });
}

Main code

The previous code retrieves the last ten votes and creates an array of content requests to retrieve all details.

// get all votes for myself
getAccountVotes('drookyn')
  .then((votes) => {
    // get last 10 votes
    const lastTenVotes = votes.reverse().slice(0, 10);
    // return new promise with all detail promises
    return Promise.all(lastTenVotes.map((vote) => {
      const authorPerm = vote.authorperm.split('/');
      return getContent(authorPerm[0], authorPerm[1]);
    }));
  })
  .then(posts => console.log(posts.map(p => p.title))) // log all titles 
  .catch(err => console.log(err));

Other details

We just printed the title of each post in the posts array above. But what else do these objects contain?

  • id
  • author
  • permlink
  • category
  • parent_author
  • parent_permlink
  • title
  • body
  • json_metadata
  • last_update
  • created
  • active
  • last_payout
  • depth
  • children
  • net_rshares
  • abs_rshares
  • vote_rshares
  • children_abs_rshares
  • cashout_time
  • max_cashout_time
  • total_vote_weight
  • reward_weight
  • total_payout_values
  • curator_payout_value
  • author_rewards
  • net_votes
  • root_author
  • root_permlink
  • max_accepted_payout
  • percent_steem_dollars
  • allow_replies
  • allow_votes
  • allow_curation_rewards
  • beneficiaries
  • url
  • root_title
  • pending_payout_value
  • total_pending_payout_value
  • active_votes
  • replies
  • author_reputation
  • promoted
  • body_length
  • reblogged_by

All the code can be found in this paste.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Steemit api uses bluebird, so you don't need to wrap with promises. You can just do:

steem.api.getAccountVotesAsync(author)
    .then((votes) => {
        // do stuff
    })

or

steem.api.getContentAsync(author, permlink)
    .then((content) => {
        // do stuf
    })

Thank you for your contribution.
While I enjoyed your work, I would suggest some further improvements to your future contributions, as follows:

  • Provide much more visual representations in shape of screenshots, output,...
  • When you mentioned the different object attributes being returned, a short explanation would have been helpful
  • Try to come up with more innovative and useful ways to utilize steemjs functions, as there has been a decent amount of relevant tutorials.
  • Expand the content of your tutorial, it is basically a bit too short.

Your contribution has been evaluated according to Utopian rules 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 for your time.
I will work on that in the next contributions.

Hey @drookyn
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!