Skip to content

Commit

Permalink
v0.0.9, add node list, stats tab
Browse files Browse the repository at this point in the history
  • Loading branch information
SteakOverCooked authored and SteakOverCooked committed Feb 8, 2018
1 parent 9f04c3d commit 268451a
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 45 deletions.
182 changes: 140 additions & 42 deletions js/utopian.js
@@ -1,38 +1,70 @@
'use strict';

steem.api.setOptions({ url: 'https://api.steemit.com' });
// default node
const default_node = "https://api.steemit.com";
steem.api.setOptions({ url: default_node });

// get Node
const getNode = () => {
return $('select#nodes').val();
}

// check if valid steem id
const validId = (id) => {
id = id.trim();
let pat = /^[a-z0-9\-\.]+$/g;
return id && pat.test(id);
}

// dots can't be used as a valid HTML div identifier
const getIdForDiv = (id) => {
return id.replace(".", "");
}

// try best to return a valid steem id
const prepareId = (id) => {
return id.replace("@", "").trim().toLowerCase();
}

// button click when press enter in text
const textPressEnterButtonClick = (text, button) => {
text.keydown(function(e) {
if (e.keyCode == 13) {
button.click();
}
});
}

// get steem profile url given id
const getSteemUrl = (id) => {
return "<a target=_blank href='https://steemit.com/@" + id + "'>@" + id + "</a>";
}

// get chrome version
const getChromeVersion = () => {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
}

// read as text
const readResponseAsText = (response) => {
return response.text();
}

// read as json
const readResponseAsJSON = (response) => {
return response.json();
}

// check if valid response
const validateResponse = (response) => {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}

// write msg in the log
const logit = (msg) => {
let d = new Date();
let n = d.toLocaleTimeString();
Expand Down Expand Up @@ -168,6 +200,60 @@ function updateStats(api) {
});
}

function getStats(api, dom) {
logit("calling " + api);
$.ajax({
type: "GET",
url: api,
success: function(result) {
let s = "<ul>";
let stats = result.stats;
s += "<li>Bot is Voting: <B>" + stats['bot_is_voting'] + "</B></li>";
s += "<li>Total Paid Rewards: " + stats['total_paid_rewards'] + " STEEM</li>";
s += "<li>Total Pending Rewards: " + stats['total_pending_rewards'] + " STEEM</li>";
s += "<li>Total Paid Authors: " + stats['total_paid_authors'] + " STEEM</li>";
s += "<li>Total Paid Curators: " + stats['total_paid_curators'] + " STEEM</li>";
s += "<li>Last Limit Comment Benefactor: " + stats['last_limit_comment_benefactor'] + "</li>";
s += "<li>Total Pending Last Post Date: " + stats['stats_total_pending_last_post_date'] + "</li>";
s += "<li>Total Paid Last Post Date: " + stats['stats_total_paid_last_post_date'] + "</li>";
s += "<li>Status Total Moderated: " + stats['stats_total_moderated'] + "</li>";
s += "</ul>";
s += "<h4>Categories</h4>";
s += "<ul>";
let cats = stats.categories;
let keys = Object.keys(cats);
let keylen = keys.length;
for (let i = 0; i < keylen; i ++) {
s += "<li>";
s += "<h5>" + keys[i] + "</h5>";
let cat = cats[keys[i]];
let catkeys = Object.keys(cat);
let catkeylen = catkeys.length;
s += "<ul>";
for (let j = 0; j < catkeylen; j ++) {
let att = cat[catkeys[j]];
s += "<li><i>" + catkeys[j] + "</i>: " + att + "</li>";
}
s += "</ul>";
s +="</li>";
}
s += "</ul>";
dom.html(s);
},
error: function(request, status, error) {
logit('Response: ' + request.responseText);
logit('Error: ' + error );
logit('Status: ' + status);
},
complete: function(data) {
logit("API Finished: Moderators.");
$('img#loading-chart').hide();
$('img#loading-moderators').hide();
}
});
}


function updateModerators(api) {
logit("calling " + api);
$.ajax({
Expand Down Expand Up @@ -351,11 +437,13 @@ function getModeratorStats(api, dom_approved, dom_rejected, dom_stats, div_of_ch
});
}

function getVP(id, dom) {
function getVP(id, dom, server) {
server = server || default_node;
steem.api.setOptions({ url: server });

steem.api.getAccounts([id], function(err, response) {
if(!err)
{
let result = (response[0].voting_power)/100;
if (!err) {
let result = (response[0].voting_power) / 100;
dom.html("<i>@" + id + "'s Voting Power is</i> <B>" + result + "%</B>");
if (result < 30) {
dom.css("background-color", "red");
Expand All @@ -366,24 +454,28 @@ function getVP(id, dom) {
}
dom.css("color", "white");
dom.css("width", result + "%");
logit("Data fetched from Steeem API");
}
else
{
logit(err);
logit("API Finished: VP- " + id);
} else {
logit("API error: " + err);
}
});
}

function getRep(id, dom) {
function getRep(id, dom, server) {
server = server || default_node;
steem.api.setOptions({ url: server });

steem.api.getAccounts([id], function(err, response) {
if(!err)
{
var result = steem.formatter.reputation(response[0].reputation);
var steemPower = steem.formatter.estimateAccountValue(response[0]);
steemPower.then(value => dom.html("<i>@" + id + "'s Reputation is</i> <B>" + result + "</B><br><i>@" + id + "'s Total Account Value is</i> <B>$" + value + "</B>"));
logit("API Finished: Reputation - " + id);
}
if (!err){
let result = steem.formatter.reputation(response[0].reputation);
let av = steem.formatter.estimateAccountValue(response[0]);
av.then(value => {
dom.html("<i>@" + id + "'s Reputation is</i> <B>" + result + "</B><br><i>@" + id + "'s Total Account Value is</i> <B>$" + value + "</B>");
});
logit("API Finished: Reputation/Account Value - " + id);
} else {
logit("API error: " + err);
}
});
}

Expand Down Expand Up @@ -522,8 +614,9 @@ function getTeamMembers(id, api, dom, div_of_chart) {
});
}

const getPosts = (id, dom, num = 10) => {
steem.api.setOptions({ url: 'https://api.steemit.com' });
const getPosts = (id, dom, server, num = 10) => {
server = server || default_node;
steem.api.setOptions({ url: server });

var query = {
tag: id,
Expand All @@ -542,8 +635,9 @@ const getPosts = (id, dom, num = 10) => {
});
}

const getData = (id, dom, item) => {
steem.api.setOptions({ url: 'https://api.steemit.com' });
const getData = (id, dom, item, server) => {
server = server || default_node;
steem.api.setOptions({ url: server });

steem.api.getAccounts([id], function(err, result) {
let s = "<ul>";
Expand All @@ -561,17 +655,19 @@ document.addEventListener('DOMContentLoaded', function() {
$(function() {
$( "#tabs" ).tabs();
});
// set default node
$('select#nodes').val(default_node);
// load steem id
chrome.storage.sync.get('utopian_settings', function(data) {
if (data && data.utopian_settings) {
let utopian = data.utopian_settings;
if (utopian["steemit_id"]) {
let id = utopian["steemit_id"].trim();
let id = prepareId(utopian["steemit_id"]);
$('input#steemit_id').val(id);
if (validId(id)) {
$('input#contributor_id').val(id);
getVP(id, $("div#account_vp"));
getRep(id, $("div#account_rep"));
getVP(id, $("div#account_vp"), utopian['nodes']);
getRep(id, $("div#account_rep"), utopian['nodes']);
getModeratorStats("https://api.utopian.io/api/posts?moderator=" + id + "&skip=0&limit=8", $("div#moderators_approved"), $("div#moderators_rejected"), $('div#moderators_stats'), "chart_moderators");
getTeamMembers(id, "https://api.utopian.io/api/moderators", $("div#search_team_result"), search_result_chart_members);
}
Expand All @@ -586,33 +682,38 @@ document.addEventListener('DOMContentLoaded', function() {
friends = friends.split("\n");
let len = friends.length;
for (let i = 0; i < len; ++ i) {
let id = friends[i];
let id = prepareId(friends[i]);
if (validId(id)) {
$("div#friends_vp_rep").append("<div id='account_vp_100_" + id + "' class='vpbar'><div id='account_vp_" + id + "' class='vp'> </div> </div><div id='account_rep_" + id + "'> </div>");
getRep(id, $('div#account_rep_' + id));
getVP(id, $('div#account_vp_' + id));
let tid = getIdForDiv(id);
$("div#friends_vp_rep").append("<div id='account_vp_100_" + tid + "' class='vpbar'><div id='account_vp_" + tid + "' class='vp'> </div> </div><div id='account_rep_" + tid + "'> </div>");
getRep(id, $('div#account_rep_' + tid), utopian['nodes']);
getVP(id, $('div#account_vp_' + tid), utopian['nodes']);
}
}
}
// get node infor
$('select#nodes').val(utopian['nodes']);
}
});
$('button#save_id_btn').click(function() {
let id = $('input#steemit_id').val().trim();
let website = $('select#steemit_website').val();
let friends = $('textarea#friends').val();
let nodes = $('select#nodes').val();
let utopian = {};
utopian['steemit_id'] = id;
utopian['steemit_website'] = website;
utopian['friends'] = friends;
utopian['nodes'] = nodes;
chrome.storage.sync.set({
utopian_settings: utopian
}, function() {
alert('Settings Saved (Required: Reload Extension)');
});
});
// utopian-io
getVP("utopian-io", $("div#account_utopian_vp"));
getRep("utopian-io", $("div#account_utopian_rep"));
getVP("utopian-io", $("div#account_utopian_vp"), getNode());
getRep("utopian-io", $("div#account_utopian_rep"), getNode());
// about
let manifest = chrome.runtime.getManifest();
let app_name = manifest.name + " v" + manifest.version;
Expand All @@ -624,26 +725,22 @@ document.addEventListener('DOMContentLoaded', function() {
// load unreviewed contributions
updateUnreviewed("https://utopian.plus/unreviewedPosts.json");
// search a id when press Enter
$('input#mod_id').keydown(function(e) {
if (e.keyCode == 13) {
$('button#search_id').click();
}
});
textPressEnterButtonClick($('input#mod_id'), $('button#search_id'));
// find user's details.
$('button#search_id').click(function() {
let id = $('input#mod_id').val().trim();
let id = prepareId($('input#mod_id').val());
if (!validId(id)) {
alert("Doesn't seem a valid Steem ID.");
} else {
$("div#search_result_rep").html("<img id='loading' src='images/loading.gif' />");
getVP(id, $("div#search_result_vp"));
getRep(id, $("div#search_result_rep"));
getVP(id, $("div#search_result_vp"), getNode());
getRep(id, $("div#search_result_rep"), getNode());
getModeratorStats("https://api.utopian.io/api/posts?moderator=" + id + "&skip=0&limit=8", $("div#search_result_approved"), $("div#search_result_rejected"), $('div#search_result_stats'), "search_result_chart");
updateModeratorsById(id, "https://api.utopian.io/api/moderators", $("div#search_result_stats2"));
}
});
// get latest utopian posts
getPosts("utopian-io", $("ul#posts"), 20);
getPosts("utopian-io", $("ul#posts"), getNode(), 20);
// get basic information
getData("utopian-io", $("div#info"), [
"name",
Expand All @@ -654,11 +751,12 @@ document.addEventListener('DOMContentLoaded', function() {
"delegated_vesting_shares",
"voting_power",
"reputation"
]);
], getNode());
// rep calculator
$('button#btn_rep').click(function() {
let rep = parseInt($('input#steemit_reputation').val());
let reputation = steem.formatter.reputation(rep);
$('div#rep_result').html("Reputation of " + rep + " = <B>" + reputation + "</B>");
})
});
getStats("https://api.utopian.io/api/stats", $("div#stats"));
}, false);
2 changes: 1 addition & 1 deletion manifest.json
Expand Up @@ -3,7 +3,7 @@
"name": "Utopian Moderator & Supervisor",
"short_name": "Utopian-IO",
"default_locale": "en",
"version": "0.0.8",
"version": "0.0.9",
"browser_action": {
"default_icon": "icon.png",
"default_title": "Utopian Moderator & Supervisor",
Expand Down
25 changes: 23 additions & 2 deletions utopian-ext-main.html
Expand Up @@ -20,7 +20,7 @@
<script src="js/steem.min.js"></script>
<script src="js/utopian.js"></script>
<style>
.ext-window {width: 770px;}
.ext-window {width: 850px;}
textarea {width: 100%;}
img {max-width: 100%;}
.chart {width: 100%; height: 570px;}
Expand All @@ -37,6 +37,7 @@
<li><a href="#tabs-supervisor">Supervisor</a></li>
<li><a href="#tabs-chart">Chart</a></li>
<li><a href="#tabs-posts">Posts</a></li>
<li><a href="#tabs-stats">Stats</a></li>
<li><a href="#tabs-setting">Setting</a></li>
<li><a href="#tabs-tools">Tools</a></li>
<li><a href="#tabs-log">Log</a></li>
Expand Down Expand Up @@ -93,7 +94,11 @@ <h4>@utopian-io</h4>
<div id="info"> </div>
<h4>Latest Posts from @utopian-io</h4>
<ul id="posts"> </ul>
</div>
</div>
<div id="tabs-stats">
<h4>Statistics</h4>
<div id="stats"> </div>
</div>
<div id="tabs-setting">
<p><h4>Your SteemId ID: </h4><input type='text' class='form-control' id='steemit_id' name='steemit_id' placeholder="Your Steem ID without @" value=''></p>
<p>
Expand All @@ -103,6 +108,22 @@ <h4>Alt + S: (Switch)</h4>
<option value='busy.org'>busy.org</option>
</select>
</p>
<p>
<h4>Nodes</h4>
<select id='nodes' class='form-control'>
<option value='https://api.steemit.com'>https://api.steemit.com</option>
<option value='https://rpc.buildteam.io'>https://rpc.buildteam.io</option>
<option value='https://gtg.steem.house:8090'>https://gtg.steem.house:8090</option>
<option value='https://steemd.minnowsupportproject.org'>https://steemd.minnowsupportproject.org</option>
<option value='https://rpc.steemliberator.com'>https://rpc.steemliberator.com</option>
<option value='https://seed.bitcoiner.me'>https://seed.bitcoiner.me</option>
<option value='https://steemd.pevo.science'>https://steemd.pevo.science</option>
<option value='https://steemd.privex.io'>https://steemd.privex.io</option>
<option value='https://seed.bitcoiner.me'>https://seed.bitcoiner.me</option>
<option value='https://steemd.steemitdev.com'>https://steemd.steemitdev.com</option>
<option value='https://steemd.steemitstage.com'>https://steemd.steemitstage.com</option>
</select>
</p>
<p>
<h4>Additional ID's (Tab General)</h4>
<textarea class="form-control" id="friends" placeholder="Enter Your Friends' IDs one ID per line" rows=10></textarea>
Expand Down

0 comments on commit 268451a

Please sign in to comment.