Skip to content
Snippets Groups Projects
Commit 85a89e84 authored by inertia's avatar inertia
Browse files

#36 switched tutorial to use bridge method

parent b32e4d74
No related branches found
No related tags found
2 merge requests!113Deploy to production,!112Fast biscuit
......@@ -15,10 +15,7 @@ We are using the `get_state` function with `dhive` that returns the current stat
Also see:
* [get discussions]({{ '/search/?q=get discussions' | relative_url }})
* [tags_api.get_discussions_by_comments]({{ '/apidefinitions/#tags_api.get_discussions_by_comments' | relative_url }})
* [condenser_api.get_discussions_by_comments]({{ '/apidefinitions/#condenser_api.get_discussions_by_comments' | relative_url }})
* [tags_api.get_discussions_by_created]({{ '/apidefinitions/#tags_api.get_discussions_by_created' | relative_url }})
* [condenser_api.get_discussions_by_created]({{ '/apidefinitions/#condenser_api.get_discussions_by_created' | relative_url }})
* [bridge.get_account_posts]({{ '/apidefinitions/#bridge.get_account_posts' | relative_url }})
## Steps
......@@ -52,10 +49,10 @@ const md = new Remarkable({ html: true, linkify: true });
Next, we have the `main` function which runs when the page is loaded.
```javascript
// query string, fetching comments made by @hiveio account
const query = '/@hiveio/comments';
// fetching comments made by author
const author = prompt('Account?', 'hiveio');
client.database.call('get_state', [query]).then(result => {
client.hivemind.call('get_account_posts', {sort: 'comments', account: author, limit: 100})
// work with state object
});
```
......@@ -65,166 +62,54 @@ client.database.call('get_state', [query]).then(result => {
The following is an example of the returned object:
```json
{
"current_route":"/@hiveio/comments",
"props":{
"head_block_number":22307429,
"head_block_id":"01546265c9dc3e761add4c4b652743e3c640fa19",
"time":"2018-05-10T12:15:30",
"current_witness":"smooth.witness",
"total_pow":514415,
"num_pow_witnesses":172,
"virtual_supply":"271970374.699 HIVE",
"current_supply":"268140818.508 HIVE",
"confidential_supply":"0.000 HIVE",
"current_hbd_supply":"13342173.771 HBD",
"confidential_hbd_supply":"0.000 HBD",
"total_vesting_fund_hive":"191002132.498 HIVE",
"total_vesting_shares":"388786707656.308148 VESTS",
"total_reward_fund_hive":"0.000 HIVE",
"total_reward_shares2":"0",
"pending_rewarded_vesting_shares":"366359809.533218 VESTS",
"pending_rewarded_vesting_hive":"178575.754 HIVE",
"hbd_interest_rate":0,
"hbd_print_rate":10000,
"maximum_block_size":65536,
"current_aslot":22373110,
"recent_slots_filled":"340282366920938463463374607431768211455",
"participation_count":128,
"last_irreversible_block_num":22307411,
"vote_power_reserve_rate":10,
"average_block_size":14881,
"current_reserve_ratio":200000000,
"max_virtual_bandwidth":"264241152000000000000"
},
"tag_idx":{
"trending":["","life","photography","hiveio","kr","introduceyourself","bitcoin","art","travel","cryptocurrency","spanish","food","hive","blog","funny","news","nature","colorchallenge","dtube","indonesia","story","cn","money","music","writing","crypto","contest","busy","health","poetry","meme","video","utopian-io","photo","new","love","blockchain","deutsch","dmania","science","technology","aceh","entertainment","gaming","politics","myanmar","esteem","sports","fun","tr"]
},
"tags":{},
"content":{
"hiveio/afm007-re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t050215510z":{
"id":47669989,
"author":"hiveio",
"permlink":"afm007-re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t050215510z",
"category":"hive",
"parent_author":"afm007",
"parent_permlink":"devportal-update-3-ux-improvements-more-javascript-tutorials-and-more","title":"","body":"I want to learn the Python language.",
"json_metadata":"{''}",
"last_update":"2018-05-09T05:02:15",
"created":"2018-05-09T05:02:15",
"active":"2018-05-09T05:02:15",
"last_payout":"1970-01-01T00:00:00",
"depth":1,
"children":0,
"net_rshares":1057692008,
"abs_rshares":1057692008,
"vote_rshares":1057692008,
"children_abs_rshares":0,
"cashout_time":"2018-05-16T05:02:15",
"max_cashout_time":"1969-12-31T23:59:59",
"total_vote_weight":32523,
"reward_weight":10000,
"total_payout_value":"0.000 HBD",
"curator_payout_value":"0.000 HBD",
"author_rewards":0,
"net_votes":1,
"root_author":"hiveio",
"root_permlink":"devportal-update-3-ux-improvements-more-javascript-tutorials-and-more",
"max_accepted_payout":"1000000.000 HBD",
"percent_hbd":10000,
"allow_replies":true,
"allow_votes":true,
"allow_curation_rewards":true,
"beneficiaries":[],
"url":"/hive/@hiveio/devportal-update-3-ux-improvements-more-javascript-tutorials-and-more#@afm007/afm007-re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t050215510z",
"root_title":"DevPortal Update #3: UX Improvements, More Javascript Tutorials and More!",
"pending_payout_value":"0.005 HBD",
"total_pending_payout_value":"0.000 HIVE",
"active_votes":[{"voter":"afm007","weight":17182,"rshares":1057692008,"percent":10000,"reputation":"855556264424","time":"2018-05-09T05:18:06"}],
"replies":[],
"author_reputation":"855556264424",
"promoted":"0.000 HBD",
"body_length":0,
"reblogged_by":[]
},
"hiveio/re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t045305223z":{
"id":47669080,
"author":"hiveio",
"permlink":"re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t045305223z",
"category":"hive",
"parent_author":"andreina89",
"parent_permlink":"devportal-update-3-ux-improvements-more-javascript-tutorials-and-more",
"title":"",
"body":"Excellent post very interesting friend, thanks",
"json_metadata":"{\"tags\":[\"hive\"],\"app\":\"hiveblog/0.1\"}",
"last_update":"2018-05-09T04:53:21",
"created":"2018-05-09T04:53:21",
"active":"2018-05-09T04:53:27",
"last_payout":"1970-01-01T00:00:00",
"depth":1,
"children":1,
"net_rshares":0,
"abs_rshares":0,
"vote_rshares":0,
"children_abs_rshares":0,
"cashout_time":"2018-05-16T04:53:21",
"max_cashout_time":"1969-12-31T23:59:59",
"total_vote_weight":0,
"reward_weight":10000,
"total_payout_value":"0.000 HBD",
"curator_payout_value":"0.000 HBD",
"author_rewards":0,
"net_votes":0,
"root_author":"hiveio",
"root_permlink":"devportal-update-3-ux-improvements-more-javascript-tutorials-and-more",
"max_accepted_payout":"1000000.000 HBD",
"percent_hbd":10000,
"allow_replies":true,
"allow_votes":true,
"allow_curation_rewards":true,
"beneficiaries":[],
"url":"/hive/@hiveio/devportal-update-3-ux-improvements-more-javascript-tutorials-and-more#@andreina89/re-hiveio-devportal-update-3-ux-improvements-more-javascript-tutorials-and-more-20180509t045305223z",
"root_title":"DevPortal Update #3: UX Improvements, More Javascript Tutorials and More!",
"pending_payout_value":"0.000 HBD",
"total_pending_payout_value":"0.000 HIVE",
"active_votes":[],
"replies":[],
"author_reputation":"174938588721",
"promoted":"0.000 HBD",
"body_length":0,"reblogged_by":[]
}
},
"accounts":{
"hiveio":{},
},
"witnesses":{},
"discussion_idx":{},
"witness_schedule":{
"id":0,
"current_virtual_time":"326078326927286190874576091",
"next_shuffle_block_num":22307439,
"current_shuffled_witnesses":["furion","someguy123","good-karma","blocktrades","smooth.witness"],
"num_scheduled_witnesses":21,
"top19_weight":1,
"timeshare_weight":5,
"miner_weight":1,
"witness_pay_normalization_factor":25,
"median_props":{
"account_creation_fee":"0.100 HIVE",
"maximum_block_size":65536,
"hbd_interest_rate":0
},
"majority_version":"0.19.3",
"max_voted_witnesses":20,
"max_miner_witnesses":0,
"max_runner_witnesses":1,
"hardfork_required_witnesses":17
},
"feed_price":{
"base":"3.484 HBD",
"quote":"1.000 HIVE"
}
}
[
{
"post_id":103793175,
"author":"blocktrades",
"permlink":"qtbpx4",
"category":"hive-139531",
"title":"RE: 12th update of 2021 on BlockTrades work on Hive software",
"body":"Yes, I think the above is quite probable.",
"json_metadata":{"app":"hiveblog\/0.1"},
"created":"2021-05-18T22:17:30",
"updated":"2021-05-18T22:17:30",
"depth":4,
"children":0,
"net_rshares":45572072190,
"is_paidout":false,
"payout_at":"2021-05-25T22:17:30",
"payout":0.021,
"pending_payout_value":"0.021 HBD",
"author_payout_value":"0.000 HBD",
"curator_payout_value":"0.000 HBD",
"promoted":"0.000 HBD",
"replies":[],
"author_reputation":77.33,
"stats":{
"hide":false,
"gray":false,
"total_votes":1,
"flag_weight":0.0
},
"url":"\/hive-139531\/@blocktrades\/12th-update-of-2021-on-blocktrades-work-on-hive-software#@blocktrades\/qtbpx4",
"beneficiaries":[],
"max_accepted_payout":"1000000.000 HBD",
"percent_hbd":10000,
"parent_author":"urun",
"parent_permlink":"re-blocktrades-qtblai",
"active_votes":[
{
"rshares":45572072190,
"voter":"urun"
}
],
"blacklists":[],
"community":"hive-139531",
"community_title":"HiveDevs",
"author_role":"mod",
"author_title":""
}
]
```
#### 3. Formatting<a name="formatting"></a>
......@@ -234,18 +119,18 @@ Next we will format the above object properly to view in a simple user interface
```javascript
if (
!(
Object.keys(result.content).length === 0 &&
result.content.constructor === Object
Object.keys(result).length === 0 &&
result.constructor === Object
)
) {
var comments = [];
Object.keys(result.content).forEach(key => {
const comment = result.content[key];
const comment = result[key];
const parent_author = comment.parent_author;
const parent_permlink = comment.parent_permlink;
const created = new Date(comment.created).toDateString();
const body = md.render(comment.body);
const netvotes = comment.net_votes;
const totalVotes = comment.stats.total_votes;
comments.push(
`<div class="list-group-item list-group-item-action flex-column align-items-start">\
<div class="d-flex w-100 justify-content-between">\
......@@ -253,22 +138,23 @@ if (
<small class="text-muted">${created}</small>\
</div>\
<p class="mb-1">${body}</p>\
<small class="text-muted">&#9650; ${netvotes}, Replied to: @${parent_author}/${parent_permlink}</small>\
<small class="text-muted">&#9650; ${totalVotes}, Replied to: @${parent_author}/${parent_permlink}</small>\
</div>`
);
});
document.getElementById('author').innerHTML = author;
document.getElementById('comments').style.display = 'block';
document.getElementById('comments').innerHTML = comments.join('');
}
```
We first check if `content` is not an empty object. We then iterate through each object in `content` and extract:
We first check if `result` is not an empty object. We then iterate through each object in `result` and extract:
* `parent_author`
* `parent_permlink`
* and the post or comment the `@hiveio` account is replying to
* and the post or comment the author is replying to
We format `created` date and time, parse `body` markdown and get `net_votes` on that comment.
We format `created` date and time, parse `body` markdown and get `totalVotes` on that comment.
Each line is then pushed and displayed separately.
### To Run the tutorial
......
......@@ -12,25 +12,26 @@ const md = new Remarkable({ html: true, linkify: true });
//fetch list of comments for certain account
async function main() {
const query = '/@hiveio/comments';
client.database
.call('get_state', [query])
const author = prompt('Account?', 'hiveio');
client.hivemind
.call('get_account_posts', {sort: 'comments', account: author, limit: 100})
.then(result => {
console.log(result);
if (
!(
Object.keys(result.content).length === 0 &&
result.content.constructor === Object
Object.keys(result).length === 0 &&
result.constructor === Object
)
) {
var comments = [];
Object.keys(result.content).forEach(key => {
const comment = result.content[key];
Object.keys(result).forEach(key => {
const comment = result[key];
const parent_author = comment.parent_author;
const parent_permlink = comment.parent_permlink;
const created = new Date(comment.created).toDateString();
const body = md.render(comment.body);
const netvotes = comment.net_votes;
const totalVotes = comment.stats.total_votes;
comments.push(
`<div class="list-group-item list-group-item-action flex-column align-items-start">\
<div class="d-flex w-100 justify-content-between">\
......@@ -38,10 +39,11 @@ async function main() {
<small class="text-muted">${created}</small>\
</div>\
<p class="mb-1">${body}</p>\
<small class="text-muted">&#9650; ${netvotes}, Replied to: @${parent_author}/${parent_permlink}</small>\
<small class="text-muted">&#9650; ${totalVotes}, Replied to: @${parent_author}/${parent_permlink}</small>\
</div>`
);
});
document.getElementById('author').innerHTML = author;
document.getElementById('comments').style.display = 'block';
document.getElementById('comments').innerHTML = comments.join(
''
......
......@@ -5,7 +5,7 @@
</head>
<body>
<div class="container" id="content"><br/>
<h4>Comments by @hiveio</h4>
<h4>Comments by @<span id="author"></span></h4>
<p>
<div id="comments" style="display: none;" class="list-group">
</div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment