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

@drookyn · 2018-05-21 09:59 · utopian-io

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.

#utopian-io #tutorials #steemdev #programming #javascript
Payout: 0.000 HBD
Votes: 13
More interactions (upvote, reblog, reply) coming soon.