Greenshift 018: Resteems and JSON
Today, we begin tackling our fourth relationship. For this relationship, we want to get the most recently resteemed author's most recent post. The relationship is a little more robust than simply getting the most recently resteemed post in that we are more likely to fall within the seven day window especially if a resteem is older. But because we need to find a second author, this relationship is more complex than our previous three.
First off, we need to filter posts by resteem. Unfortunately, there is no resteem operation as part of the blockchain. Instead, resteeming is defined as a custom JSON operation containing the type of the custom JSON operation ("reblog") and the information associated with that information. It is essentially the same structure as an operation but within an operation itself.
JSON stands for JavaScript Object Notation. It is the de facto way of transporting data across the internet. There are hundreds of web servers out there whose only job is to give callers JSON in return. Let's just say it is a very popular pattern that one will come across in web development. We'll only briefly cover it in how it relates to this project but you should really take a look at the standard if you are interested in web development.
We can quickly program a function to grab this JSON information to see how it is formatted. Below is the shell of our previous relationships, except this time we filter for custom JSON.
In order to see what this JSON looks like, we'll simply add the function to the controller with the others and add this information to the view. Since, I haven't resteemed in a really long time, I have provided an example with a different account that has recent advertised for their resteeming services:
We can now dissect this JSON message. We have an array of length 2, with the first item being the type of custom operation and the second part containing the author, permlink, and resteeming account. We simply want the author at this moment in time since we want their most recent content. We also need to verify that the type is indeed "reblog" to differentiate it from other custom JSON operations. These changes are implemented in the code below:
First, we parse the JSON by using Ruby's built-in library for JSON. Then, we check the type before grabbing our author. By quickly refreshing the browser, we now get the author that was most recently resteemed instead of the JSON object. The refreshed view can be seen below:
But we want the permlink of the new content, not the author. Greenshift is supposed to give users new pieces of content, not authors to check out. Which is all true. But in order to get that content, we'll have to query the blockchain a second time with the new account that we obtained. And we'll do that next time.
This post has been just added as new item to timeline of Q-Filter on Steem Projects.
If you want to be notified about new updates from this project, register on Steem Projects and add Q-Filter to your favorite projects.