How to clear your feed, coding an unfollow script

in #utopian-io6 years ago (edited)

hexagon-2307350_1280.png

Motivation

Our steemit feed can get really crowded, even with just a few hundred accounts we follow. When we create a new account, we may not be aware of this fact and we just hit that follow button. But, in order to be able to have a meaningful interaction with the content we follow, we need to keep that feed under control. What happens if you have a large number of people you follow and want to unfollow? Do we just go on a case by case basis and hit the unfollow button? Of course not, that would be silly and take a really long time. We need to code our way out of trouble. If you wish to skip this tutorial, please visit Code

What will I learn

You will learn about steem blockchain transactions, how to build your personalized transaction and broadcast it.
By the end of this tutorial you will be able to:

  • Unfollow one, two or however many accounts you wish, at once
  • Create a personalized transaction and broadcast it
  • Get a list of accounts you follow

Requirements

The requirements are mostly the same as in previous tutorials, you will need:

  • Plain text editor (I use Atom)
  • NodeJS installed on your machine ( Node )
  • Javascript at a beginner to intermediate level

Difficulty

The tutorial has intermediate difficulty, but can, in my opinion, be followed by anyone with a basic understanding of programming and JavaScript.

What does my project actually do

My script is able to unfollow a given list of friends. Also, it can programmatically extract the list, up to a number you specify and than unfollow this list.

Step1 - Workspace Setup

You will need to install Node on your machine and clone my repository. The code is located in the unfollow.js script. Conversely, you can start a new node project and try to code your own script by following this tutorial.

Step2 - Dependencies, setup your parameters

This project has one dependency, namely the steem-js library. Import it by adding this to the first line of the script: var steem = require('steem');. As it was the case with my previous tutorial, you need your account name and your private posting key. You can specify as a parameter a list, if you have some specific accounts on hand you wish to unfollow.

Step3 - Design

I have encapsulated the unfollow feature in its own class:

class Unfollow {
  constructor(user, listToUnfollow, password) {
    this.user = user;
    this.listToUnfollow = listToUnfollow;
    this.password = password;
  }
}

This class needs a user (that is you), a list of accounts to unfollow and a password (private posting key). Putting this feature under a class can make the code a bit easier to follow, compered to my previous tutorials. The steemit library does not provide a method to directly unfollow folks, or to follow them for that matter. This is why, in order to achieve our goal, we need to create a custom transaction and broadcast it via the steem.broadcast.send() method. How to create such a custom transaction? Well, it's easier than it sounds.

Step4 - Unfollow transaction model

There are two methods we can chose in order to discover this model. We can manually unfollow someone, using the steemit interface at steemit.com/@..., than go and invoke the steem.getAccountHistory() and see the transaction JSON. Based on that, we can than make our own JSON. Alternatively, we can use steemd. Again, we must unfollow someone, and go to steemd/@[your account] and browse the unfollow tranzaction that was made by you. It should look like this:

2018-02-08 (3).png

This is basically a pretty print of the unfollow transaction we need to construct. In code, the hardcoded transaction looks like this:

const my_json = '["follow",{"follower":"prometheus21","following":"allnatural","what":[]}]'

var follow_trx =  [
  'custom_json',
  {
    required_auths: [],
    required_posting_auths: ['prometheus21'],
    id: 'follow',
    json: my_json
  }
];

We identify 2 key components here, namely my account name, prometheus21 and the account name I unfollowed, allnatural, under the following key in the my_json constant. Our task is therefore as follows: parse the list of friends we wish to unfollow, create the JSON transaction with each account that is to be unfollowed as the value to the 'following' key.

Step5 - Building our transactions

Now that we know what needs coding, our task just got a lot easier. We break it down into two parts, namely building the custon json, the one containing the name of the account we wish to unfollow, than we move up to buiding the actual unfollow transaction. Building the json looks like this:

 makeCustomJson(friend) {
    var result = []
    result.push('follow');
    const custom_json = {
      follower: this.user,
      following : friend,
      what: []
    }
    result.push(custom_json);
    return JSON.stringify(result);
  }

All the fields are taken from the model we observed earlier. I just added the custom value, under 'following' of the account I wish to unfollow. Next, we move up to building the whole transaction:

makeTranzaction(friend) {
    var tranzaction = ['custom_json'];
    var json = {
      required_auths: [],
      required_posting_auths: [this.user],
      id: 'follow',
      json: this.makeCustomJson(friend)
    }
    tranzaction.push(json);
    return tranzaction;
  }

The json field is created using the previous function and at the * required_posting_auths:* field we just add your account. Next, we broadcast our newly forged transaction!

Step6 - Brodcast

As I have mentioned, I will be using the steem.broadcast.send() method. This method, takes as parameter our operations (transactions as I have them named here) and our posting key. All of the parameters this function takes are lists, hence we can broadcast multiple transactions at one, using different types of passwords. The code looks like this:

  unfollowFriends() {
    var tranzactions = []
    for(var i=0; i< this.listToUnfollow.length; i++) {
        tranzactions.push(this.makeTranzaction(this.listToUnfollow[i]));
    }

    return steem.broadcast.sendAsync(
      {
        extensions: [],
        operations: tranzactions
      }, [this.password]);
  }
}

I looped through the list of unfollow accounts and created a custom transaction for each one, using the function from the previous step. I used the newly minted transactions and the broadcast method to achieve the desired behavior.

Step7 - Get a list of accounts you follow

What happens if we don't have lying around a list of accounts and we just wish to unfollow a number of folks? We just invoke the API and retrieve the list ourselves:

steem.api.getFollowingAsync('prometheus21', '', 'blog', LIMIT);

We process the result and get our list:

function getListOfFriends(apiFollowingResponse) {
  var res = []
  for(var i=0; i<apiFollowingResponse.length; i++) {
    res.push(apiFollowingResponse[i]['following']);
  }
  return res;
}

Step8 - Putting it all toghether

We now have all the pieces. We just need to instantiate our class and call the right methods!

async function main() {

  const LIMIT = 10;
  const NAME = ''

  var unf = new Unfollow(NAME, [], '');

  const apiListOfFollowing = await steem.api.getFollowingAsync(NAME, '', 'blog', LIMIT);

  unf.setListToUnfollow(getListOfFriends(apiListOfFollowing));

  const unfollowRes = await unf.unfollowFriends();

  // console.log(unfollowRes);

}

main()

There is plenty of customization here. We can instantiate with a list of accounts we have in hand or we can call the API with the LIMIT we want. This limit will be the number of accounts that be unfollowed!

Conclusion

In this tutorial I have furthered my knowledge of steemit-js and I hope I have managed to do the same for you. Also, I hope this can help some people who want a more orderly feed. I believe it's important to be able to interact with our feed and that is almost impossible if you are following a large amount of people. I hope that in the future, steemit will improve how the feed is organised.

Curriculum

Understending this tutorial is not dependent on the previous ones I made, but they all relate to the same library and are useful in their own way. For example, I have used the async await mechanism in this tutorial. If you are not familiar with it, I suggest you go through my last tutorial!

Sources

Pixabay Thumbnail



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @prometheus21 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

followed

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly � Featured Posts are voted every 2.4hrs
Join the Curation Team Here | Vote Resteemable for Witness

You got a 0.68% upvote from @postpromoter courtesy of @prometheus21!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.029
BTC 67831.26
ETH 3460.55
USDT 1.00
SBD 2.72