|
| 1 | +# Get follower and following list |
| 2 | + |
| 3 | +_By the end of this tutorial you should know how to create a list of followers and users that you are following._ |
| 4 | + |
| 5 | +This tutorial will take you through the process of calling both the `follower` and `following` functions from the STEEM API. |
| 6 | + |
| 7 | +## Intro |
| 8 | + |
| 9 | +We are using the `call` operation provided by the `dsteem` library to pull the follow information for a specified user account. There are 4 variables required to execute this operation: |
| 10 | + |
| 11 | +1. _username_ - The specific user for which the follower(ing) list will be retrieved. |
| 12 | +2. _startFollower(ing)_ - The starting letter(s) or name for the search query. |
| 13 | +3. _followType_ - This value is set to `blog` and includes all users following or being followed by the `user`. |
| 14 | +4. _limit_ - The maximum number of lines to be returned by the query. |
| 15 | + |
| 16 | +A simple HTML interface is used to capture the required information after which the function is executed. |
| 17 | + |
| 18 | +## Steps |
| 19 | + |
| 20 | +1. [**Configure connection**](#connection) Configuration of `dsteem` to communicate with the Steem blockchain |
| 21 | +2. [**Input variables**](#input) Collecting the required inputs via an HTML UI |
| 22 | +3. [**Get followers/following**](#query) Get the followers or users being followed |
| 23 | +4. [**Display**](#display) Display the array of results on the UI |
| 24 | + |
| 25 | +#### 1. Configure connection<a name="connection"></a> |
| 26 | + |
| 27 | +As usual, we have a `public/app.js` file which holds the Javascript segment of the tutorial. In the first few lines we define the configured library and packages: |
| 28 | + |
| 29 | +```javascript |
| 30 | +const dsteem = require('dsteem'); |
| 31 | +let opts = {}; |
| 32 | +//define network parameters |
| 33 | +opts.addressPrefix = 'STM'; |
| 34 | +opts.chainId = |
| 35 | + '0000000000000000000000000000000000000000000000000000000000000000'; |
| 36 | +//connect to a steem node, production in this case |
| 37 | +const client = new dsteem.Client('https://api.steemit.com'); |
| 38 | +``` |
| 39 | + |
| 40 | +Above, we have `dsteem` pointing to the production network with the proper chainId, addressPrefix, and endpoint. |
| 41 | + |
| 42 | +#### 2. Input variables<a name="input"></a> |
| 43 | + |
| 44 | +The required parameters for the follow operation is recorded via an HTML UI that can be found in the `public/index.html` file. The values have been pre-populated for ease of use but are editable. |
| 45 | + |
| 46 | +The parameter values are allocated as seen below once the user clicks on the "Get Followers" or "Get Following" button. |
| 47 | +The two queries are very similar and run from two different functions activated from a button on the UI. The first line of both functions is used to clear the display before new information is queried. |
| 48 | + |
| 49 | +```javascript |
| 50 | +//Followers function |
| 51 | +window.submitFollower = async () => { |
| 52 | + //clear list |
| 53 | + document.getElementById('followList').innerHTML = ''; |
| 54 | + |
| 55 | + //get user name |
| 56 | + const username = document.getElementById('username').value; |
| 57 | + //get starting letters / word |
| 58 | + const startFollow = document.getElementById('startFollow').value; |
| 59 | + //get limit |
| 60 | + var limit = document.getElementById('limit').value; |
| 61 | +``` |
| 62 | +
|
| 63 | +#### 3. Get followers/following<a name="query"></a> |
| 64 | +
|
| 65 | +A list of followers or users being followed is called from the database with the `follow_api` available in the `SteemJS` library. |
| 66 | +
|
| 67 | +```javascript |
| 68 | +//get list of followers |
| 69 | +//getFollowers(following, startFollower, followType, limit) |
| 70 | + let followlist = await client.call('follow_api', 'get_followers', [ |
| 71 | + username, |
| 72 | + startFollow, |
| 73 | + 'blog', |
| 74 | + limit, |
| 75 | + ]); |
| 76 | + |
| 77 | + document.getElementById('followResultContainer').style.display = 'flex'; |
| 78 | + document.getElementById('followResult').className = 'form-control-plaintext alert alert-success'; |
| 79 | + document.getElementById('followResult').innerHTML = 'Followers'; |
| 80 | + |
| 81 | + |
| 82 | +//get list of authors you are following |
| 83 | +//getFollowing(follower, startFollowing, followType, limit) |
| 84 | + let followlist = await client.call('follow_api', 'get_following', [ |
| 85 | + username, |
| 86 | + startFollow, |
| 87 | + 'blog', |
| 88 | + limit, |
| 89 | + ]); |
| 90 | + |
| 91 | + document.getElementById('followResultContainer').style.display = 'flex'; |
| 92 | + document.getElementById('followResult').className = 'form-control-plaintext alert alert-success'; |
| 93 | + document.getElementById('followResult').innerHTML = 'Following'; |
| 94 | + |
| 95 | +``` |
| 96 | +
|
| 97 | +#### 4. Display<a name="display"></a> |
| 98 | +
|
| 99 | +The result returned from the query is an array of objects. The follower(ing) value from that array is displayed on both the UI and the console via a simple `forEach` array method. |
| 100 | +
|
| 101 | +```javascript |
| 102 | + followlist.forEach((newObj) => { |
| 103 | + name = newObj.follower; |
| 104 | + document.getElementById('followList').innerHTML += name + '<br>'; |
| 105 | + console.log(name); |
| 106 | + }); |
| 107 | +``` |
| 108 | +
|
| 109 | +### To run this tutorial |
| 110 | +
|
| 111 | + 1. clone this repo |
| 112 | + 2. `cd tutorials/19_get_follower_and_following_list` |
| 113 | + 3. `npm i` |
| 114 | + 4. `npm run dev-server` or `npm run start` |
| 115 | + 5. After a few moments, the server should be running at http://localhost:3000/ |
0 commit comments