create account

[KnackSteem API] - More features added by jaysermendez

View this thread on: hive.blogpeakd.comecency.com
· @jaysermendez ·
$56.97
[KnackSteem API] - More features added
#### Repository & Pull Request
https://github.com/knacksteem/knacksteem-api
https://github.com/knacksteem/knacksteem-api/pull/5

#### What is KnackSteem?
"Do you have any talent? If yes! then KnackSteem is for you."
"Rewards people with talents, it can be any talent, anything you know how to do best is highly welcome on the platform. "
Source: Discord Channel :D

---
#### Changes Made
---

##### Middleware to check if a user is a moderator
Since some endpoints will be exclusively for moderators, a middleware to check if the current user is a moderator is needed to avoid unauthorized access. Basically, this middleware will check the username saved from the last middleware in the chain and will ask the database if this user is a moderator. If so, it will move to the next middleware. Otherwise, will tell the client that this user is not authorized to perform such action.

Related code:

https://github.com/knacksteem/knacksteem-api/blob/master/src/api/middlewares/is_moderator.js

##### Middleware to check if a user is a supervisor
Same as the middleware mentioned above but in this case, for supervisors. The process is exactly the same but, of course, for supervisors.

Related code:

https://github.com/knacksteem/knacksteem-api/blob/master/src/api/middlewares/is_supervisor.js

##### Fetch all posts from Steem API using the permlinks from the database
As discussed with other developers in this project, we had to somehow query only our posts from the blockchain but having all the data updated. Also, we wanted to add custom filters like not-moderated posts, moderated posts, and so on. To achieve this, we've decided to only store author, permlink, and category of the post in the database. First, we grab all the permlinks and authors from the query and we make calls to the Steem API to complete the missing data of the posts. Thanks to libraries like async.js, we were able to make concurrent HTTP calls and join the results at the end.

Related code:

https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/posts.controller.js#L35


##### Query posts by author
The method above allowed us to re-use this same code to perform a query by author in the database and complete the data using the same process mentioned above.

Related code:

https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/posts.controller.js#L117

##### Documentation for endpoints
One of the things I really like about apidocs is that you can generate a documentation based on the comments of the routes in the API. For instance, look at the following:

```
/**
 * @api {post} v1/posts/create Create Post
 * @apiDescription Insert a post into the database
 * @apiVersion 1.0.0
 * @apiName createPost
 * @apiGroup Posts
 * @apiPermission user
 *
 * @apiHeader {String}   Authorization     SC2 User's access token
 *
 * @apiParam  {String}   permlink          Permlink of the post
 * TODO: Add validation to the parameters.
 *
 * @apiSuccess {Number}  status            http status response
 * @apiSuccess {String}  message           http return message
 *
 * @apiError (Unauthorized 401) Unauthorized Only authenticated users can create a post
 */
router.route('/create').post(sc2Middleware, checkUserMiddleware, controller.createPost);
```

Will generate the following page:

![Screen Shot 2018-06-22 at 9.26.23 PM.png](https://cdn.steemitimages.com/DQmYkpmRog53cpi5CVPKpWWC2Hj66sKQS6hA55pEKqREiMB/Screen%20Shot%202018-06-22%20at%209.26.23%20PM.png)

##### Function to moderate a post
This is a moderation tool. Basically, it allows moderators and supervisors to edit the moderation object of any post. So, they can decide if the post is approved or not.

Related Code:

https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/moderator.controller.js#L4

---

##### Commits Overview
---

- https://github.com/knacksteem/knacksteem-api/pull/5/commits/b8fafa67b7fda38f05b6f9fd64418b85a5d53330
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/2d54636c18dee3d23dc90a5a38c9a5f58d60ed1a
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/45b6daff9e186d22a1ac15a9f481085f83b30f0a
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/8b1d3ab3f6021788ce25bb4b27cea76510feb761
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/92c7f85decdf63dded74674fc545f27251634af9
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/61c9ea373d2267bffe738228019082911bff429e
- https://github.com/knacksteem/knacksteem-api/pull/5/commits/cce2275910c078994ef981b1a2897b0d7585d5a5
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 81 others
properties (23)
authorjaysermendez
permlinkknacksteem-api-more-features-added
categoryutopian-io
json_metadata{"tags":["utopian-io","development","knacksteem","steemdev","steemapps"],"image":["https://cdn.steemitimages.com/DQmYkpmRog53cpi5CVPKpWWC2Hj66sKQS6hA55pEKqREiMB/Screen%20Shot%202018-06-22%20at%209.26.23%20PM.png"],"links":["https://github.com/knacksteem/knacksteem-api","https://github.com/knacksteem/knacksteem-api/pull/5","https://github.com/knacksteem/knacksteem-api/blob/master/src/api/middlewares/is_moderator.js","https://github.com/knacksteem/knacksteem-api/blob/master/src/api/middlewares/is_supervisor.js","https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/posts.controller.js#L35","https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/posts.controller.js#L117","https://github.com/knacksteem/knacksteem-api/blob/master/src/api/controllers/moderator.controller.js#L4","https://github.com/knacksteem/knacksteem-api/pull/5/commits/b8fafa67b7fda38f05b6f9fd64418b85a5d53330","https://github.com/knacksteem/knacksteem-api/pull/5/commits/2d54636c18dee3d23dc90a5a38c9a5f58d60ed1a","https://github.com/knacksteem/knacksteem-api/pull/5/commits/45b6daff9e186d22a1ac15a9f481085f83b30f0a","https://github.com/knacksteem/knacksteem-api/pull/5/commits/8b1d3ab3f6021788ce25bb4b27cea76510feb761","https://github.com/knacksteem/knacksteem-api/pull/5/commits/92c7f85decdf63dded74674fc545f27251634af9","https://github.com/knacksteem/knacksteem-api/pull/5/commits/61c9ea373d2267bffe738228019082911bff429e","https://github.com/knacksteem/knacksteem-api/pull/5/commits/cce2275910c078994ef981b1a2897b0d7585d5a5"],"app":"steemit/0.1","format":"markdown"}
created2018-06-23 01:30:15
last_update2018-06-23 01:30:15
depth0
children6
last_payout2018-06-30 01:30:15
cashout_time1969-12-31 23:59:59
total_payout_value43.313 HBD
curator_payout_value13.654 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,566
author_reputation20,790,862,502,465
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,845,210
net_rshares28,752,074,541,171
author_curate_reward""
vote details (145)
@amosbastian ·
Thanks for the contribution, @jaysermendez! Since the `isModerator` and `isSupervisor` functions are identical apart from one line, wouldn't it be better to combine them into one function?

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/3/2332211).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authoramosbastian
permlinkre-jaysermendez-knacksteem-api-more-features-added-20180623t213326032z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["jaysermendez"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/3/2332211","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-23 21:33:24
last_update2018-06-23 21:33:24
depth1
children3
last_payout2018-06-30 21:33:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length679
author_reputation174,473,586,900,705
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,963,920
net_rshares0
@jaysermendez ·
Even they are identical, if I combine them, mods will have supervisors rights and it is something we don't want :D. While supervisors can do everyrhing, mods cannot. So, if I combine them, mods will have access to supervisors endpoints as well
properties (22)
authorjaysermendez
permlinkre-amosbastian-re-jaysermendez-knacksteem-api-more-features-added-20180623t214635886z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-23 21:46:36
last_update2018-06-23 21:46:36
depth2
children2
last_payout2018-06-30 21:46:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length243
author_reputation20,790,862,502,465
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,965,035
net_rshares0
@amosbastian ·
Yes I know, but surely there is a better way of checking a user's role instead of duplicating all the code?
properties (22)
authoramosbastian
permlinkre-jaysermendez-re-amosbastian-re-jaysermendez-knacksteem-api-more-features-added-20180623t214810804z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-23 21:48:12
last_update2018-06-23 21:48:12
depth3
children1
last_payout2018-06-30 21:48:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length107
author_reputation174,473,586,900,705
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,965,151
net_rshares0
@steemitboard ·
Congratulations @jaysermendez! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/payout.png)](http://steemitboard.com/@jaysermendez) Award for the total payout received

<sub>_Click on the badge to view your Board of Honor._</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>


To support your work, I also upvoted your post!


**Do not miss the [last post](https://steemit.com/steemitboard/@steemitboard/steemitboard-world-cup-contest-england-vs-panama) from @steemitboard!**

---
**Participate in the [SteemitBoard World Cup Contest](https://steemit.com/steemitboard/@steemitboard/steemitboard-world-cup-contest-collect-badges-and-win-free-sbd)!**
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: [@good-karma](https://v2.steemconnect.com/sign/account-witness-vote?witness=good-karma&approve=1) and [@lukestokes](https://v2.steemconnect.com/sign/account-witness-vote?witness=lukestokes.mhth&approve=1)

---

> Do you like [SteemitBoard's project](https://steemit.com/@steemitboard)? Then **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-jaysermendez-20180623t124914000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2018-06-23 12:49:12
last_update2018-06-23 12:49:12
depth1
children0
last_payout2018-06-30 12:49:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,340
author_reputation38,975,615,169,260
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,906,486
net_rshares0
@utopian-io ·
Hey @jaysermendez
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Contributing on Utopian**
Learn how to contribute on <a href='https://join.utopian.io'>our website</a> or by watching <a href='https://www.youtube.com/watch?v=8S1AtrzYY1Q'>this tutorial</a> on Youtube.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-knacksteem-api-more-features-added-20180624t075007z
categoryutopian-io
json_metadata"{"app": "beem/0.19.29"}"
created2018-06-24 07:50:09
last_update2018-06-24 07:50:09
depth1
children0
last_payout2018-07-01 07:50:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length509
author_reputation152,955,367,999,756
root_title"[KnackSteem API] - More features added"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id62,013,859
net_rshares0