Steem.Js for dummies #5: Getting user's last post

image.png

As usual, I'll try to keep it short and show you how to efficiently use SteemJs to get the last post (or last posts) that a given user posted on the Steem blockchain.

For this, we will need the following function:

steem.api.getDiscussionsByAuthorBeforeDate

Let's see what we can find in the Steem.js documentation:

image.png

First thing, contrarily to getAccounts function, we can notice that we can only check one account at a time here.

Therefore, if you have more than one account to check, do the following:

accounts.forEach(function (element,i,array){
    steem.api.getDiscussionsByAuthorBeforeDate(element,startPermlink,beforeDate,limit, ... );}

Let's now see about the other parameters.

  • startPermlink is the Permlink starting from which the results will be fetched. Here, we want the last post, so we won't use this option and simply input null. If you want to use it, for information, the permlink is not the whole URL but only what comes after the last '/'.

  • beforeDate cannot be null and as its name shows, the function will only return the results from before this date. Once again, since we want the last post, we will take the Date from now. It took me a while to understand the time format used there. It corresponds to the first part of an ISO String, but since I'm nice, I'm giving it for you to use directly ;)

beforeDate=new Date().toISOString().split('.')[0];
  • limit simply tells the blockchain how many results we want. Let's say we want 3 results:
accounts=["stoodkev","steem-plus"];
accounts.forEach(function (element,i,array){
    steem.api.getDiscussionsByAuthorBeforeDate(element,null, new Date().toISOString().split('.')[0],3 , function(err, result) {
        console.log(result);
    });
});

Let's take a look at the result:

image.png

We indeed got the three last posts of my two accounts.
To conclude, let's see some of the useful information found in there:

image.png

This screenshot only shows few of the data returned in the object but we can already see some useful information such as the the post's body , its list of active_votes and beneficiaries, its pending_payout_value and much more.

image.png

For some reason, replies and reblogged_by remain empty and necessitate another call to the blockchain.

Hope this helps!

@stoodkev



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
8 Comments