Write a Steemit Web App: Part 6 - Follow/Unfollow SomebodysteemCreated with Sketch.

in #steemdev7 years ago (edited)

(Previous Post: Part 5)

Up to this point, this series has concentrated on reading data from the API. Now it's time to start writing back to the blockchain!

In Part 2, we retrieved the list of followers for an account. What if we wanted to follow (or unfollow) somebody? How can that be done programmatically?

Follows are saved in the blockchain as Custom JSON data. For example, in Block 13770966, we find this in txid 243c092653d8bd6e9098d653dbed8f2d7784f7d1:

["follow",{"follower":"euni","following":"kebi4all","what":["blog"]}]


Here, the account @euni had followed the account @kebi4all. (And in case it wasn't obvious, the record of your follows and unfollows are permanently stored in the blockchain for people to query until the end of time)

Broadcast Custom JSON

Let's suppose that you want to add a "follow" button next to an account in your web app. When that button is clicked, the event handler must execute a call to steem.broadcast.customJson() in order to announce the follow request to the network for inclusion in the blockchain:

let followReq = ["follow"]
followReq.push({follower: "you", following: "someone_else", what: ["blog"]})

const customJson = JSON.stringify(followReq)

steem.broadcast.customJsonAsync("5K...wif_here...", [], ["you"], "follow", customJson)
  .then(console.log)
  .catch(console.log)


The documentation for this function call can be found here:

steem.broadcast.customJson(wif, requiredAuths, requiredPostingAuths, id, json, function(err, result) {
  console.log(err, result);
});


The first argument is the WIF for posting, otherwise known as your Private Posting Key (to find it, go to your Wallet in Steemit.com => Permissions tab => click "Show Private Key" for the Posting key). This is something that your user will need to provide to your app, but treat it like a password - it should never be transmitted to nor stored on your server unless you absolutely need it while they are offline, and probably should be deleted after the browser session closes.

The second argument is just an empty array.

The third argument should have the account name as the only member of an array (i.e., your web app's user).

The fourth argument is a string simply containing "follow".

The fifth argument is the custom JSON string, as constructed above. The "what" property in the payload is an array that contains a single string: "blog" to follow the person, "ignore" to mute the person, and an empty string ("") to unfollow the person.

Note: If you are using node-style callbacks, then your callback function would be the sixth argument. Otherwise, if you use promises, then add Async to the function name and use .then() and .catch() in place of the callback function.

Throttling

Just a little warning: an account cannot broadcast to the blockchain more than once every three seconds. So, if you're trying to build a script to follow a massive amount of people, then you will need to throttle the broadcasts to wait three seconds between each.

javascriptlogo.png

(Next Post: Part 7)

Sort:  

Nice, I'm working on a steemit app myself and this is very useful. I went with the steemconnect route for authentication, but if I'd found this post beforehand I might not have needed that.

By the way, your link to post 2 (second paragraph) is broken

Thanks for the heads up about that paragraph 2 link. Missed a slash.

Marvelously succinct yet detailed. I didn't even suspect that un/follows were part of the blockchain data, I bet that over time they will account or a significant t amount of data as users come, go, or change their preferences or allegiances.

What other minor aspects of user/steemit interaction usually handled server/DB side are committed to the blockchain?

Everything is committed to the blockchain. Think of it as a series of database scripts that you can use to restore a production database from scratch - all that you need to do is "play" the blockchain forward in time and apply each transaction in the blockchain to your database (i.e., if you're building out a database, like http://steemsql.com/)