Today, we are going to tackle our third relationship. This relationship takes the author of a piece of content and looks for the highest percentage upvote in the past seven days and given a tie, the most recent upvote. Fortunately for us, we have already developed the most recent upvote in our first relationship and have easy access to the information that we will require to finish this third relationship.
First, however, we need to define the concept of weight. Weight simply denotes the amount of respective voting power that you are utilizing at any one point in time. Weight is an integer value from -10,000 to 10,000. A vote on the maximum side of that range would indicate an 100% vote. A vote on the lower side of that range indicates a -100% vote or what we would normally consider a flag. Negative weights take away and positive weights give.
Now knowing this, developing code for our new relationship should be straight-forward. We simply find the vote with the highest weight and obtain the permlink from the vote. And that is basically correct. But first, we should now recognize that our first relationship actually contains a potential flaw!
In our old code, we simply verify that the operation type is a vote, but never specify whether that vote needs to be positive or negative. We could be getting a piece of content that the author has flagged and that probably wouldn't be the best idea if are trying to find similar interesting content. Luckily, we can quickly fix this using the weight:
We simply add a single line to our most recent upvote relationship where we make sure that we skip the content in the case that the weight of the post is equal to or less than 0. This ensures that we will only ever grab content that has some positive sentiment from the author we are reading.
Next, we will tackle the third relationship, which we will start off using this updated recent upvote code. But in order to keep track of the vote which the highest upvote at any given moment, we'll have to add an additional variable to keep track of the weight at any given moment. The final result can be seen below:
By storing this weight, we can now compare every newer piece of content against this weight. Only pieces of content with greater or equivalent weight will be allowed through to the point where we store the permlink. Note that we allow equivalent weight content through since we want the most recent upvote in the case of a tie and the content we pull is ordered from oldest to most recent. We account for tiebreakers since a lot of smaller users use 100% of their vote.
Now that this relationship is completed, we can add this relationship to the controller and add the data to the view to display a third piece of content to our users. The final result is shown below:
So, now we are more than halfway through our relationships. But there's still a ton of work left to do. Our webpage is still completely static and we have yet to grab the content information from the blockchain. But by taking things in bite size pieces, large projects become more manageable and programming becomes easier and less stressful. So coming up, we'll finish off our last two relationships and then begin taking a look at the bigger picture so we can begin developing a better user interface and ultimately a usable product.