Rewrite Role Levels in Typescript | Steemit Condenser Official

in Steem Devlast year (edited)

Hello Programmers,

I hope you all are well. I am very well sure that many new developers are working on Steem and they want to build some useful tools for Steem Blockchain. Our team is working on SteemPro Mobile App for the last 6 months, During this time frame we also launch SteemPro

roles.png

To educate the new developers and also to the whole steemit users, We arrange this bi-weekly post in which we explore the code of official Steemit. You can get the GitHub repository here.

We will also modify the code to convert it into TypeScript and make the necessary changes to work with the SDS APIs provided by @steemchiller.

File Path

steemit/condenser/src/app/utils/Community.js

Class

Role

Old Code

export class Role {
    static LEVELS = ['muted', 'guest', 'member', 'mod', 'admin', 'owner'];
    static level = role => {
        if (!role) throw 'empty role provided';
        const level = Role.LEVELS.indexOf(role);
        if (level == -1) throw 'invalid role: ' + role;
        return level;
    };
    static atLeast = (role, target) => {
        return Role.level(role) >= Role.level(target);
    };
}

Working

This class helps us know if authority is valid for a specific role. We need to provide the role value in a string and the target role for comparing and we get a return value in a boolean if the role is matching with the target role or above. Or it can be a lower role and we get the false value.

Code in Typescript

export class Role {
  static LEVELS = ['muted', 'guest', 'member', 'mod', 'admin', 'owner'];

  static level = (role: string): number => {
    role = role || 'guest';
    const level = Role.LEVELS.indexOf(role);
    if (level === -1) {
      throw new Error('Invalid role: ' + role);
    }
    return level;
  };
  static atLeast = (
    role: string,
    target: 'muted' |'guest' | 'member' | 'mod' | 'admin' | 'owner',
  ): boolean => {
    const roleLevel = Role.level(role || 'guest');
    const targetLevel = Role.level(target);
    return roleLevel >= targetLevel;
  };
}

What's new

This code is optimized to use for the SDS feed_api or post_api. We can get an empty string value of author_role for a community which is why we need to use the 'guest' value for this empty string.

SteemPro Official

@steempro.com
https://www.steempro.com

▀▄▀▄▀▄ T̳̿͟͞h̳̿͟͞a̳̿͟͞n̳̿͟͞k̳̿͟͞s̳̿͟͞ ̳̿͟͞f̳̿͟͞o̳̿͟͞r̳̿͟͞ ̳̿͟͞R̳̿͟͞e̳̿͟͞a̳̿͟͞d̳̿͟͞i̳̿͟͞n̳̿͟͞g̳̿͟͞ ▄▀▄▀▄▀

Cc: @blacks
Cc: @rme
Cc: @hungry-griffin
Cc: @steemchiller
Cc: @steemcurator01


Best Regards @faisalamin

Sort:  
 last year 

We can get an empty string value of author_title for a community which is why we need to use the 'guest' value for this empty string.

I think you mean the field author_role.

Absolutely,
author_role thanks for the correction.

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 59588.19
ETH 2572.25
USDT 1.00
SBD 2.50