Commit 7dcf359b authored by Martin Lees's avatar Martin Lees
Browse files

Update to use a remote server, cleanup is due

parent 7c051b94
......@@ -304,9 +304,9 @@ class Steempress_sp_Admin {
$data = $result['body'];
$primary_tag = explode("/",$data)[0];
$permlink= explode("/",$data)[1];
add_post_meta($id, "steempress_sp_username", $username);
add_post_meta($id, "steempress_sp_permlink", $permlink);
add_post_meta($id, "steempress_sp_tag", $primary_tag);
add_post_meta($id, "steempress_sp_username", $username, true);
add_post_meta($id, "steempress_sp_permlink", $permlink, true);
add_post_meta($id, "steempress_sp_tag", $primary_tag, true);
}
}
......
......@@ -107,11 +107,12 @@ class Steempress_sp_Public {
$options = get_option($this->plugin_name);
if (!isset($options["twoway"]))
$options["twoway"] = "off";
if ($options['twoway'] == "on") {
$id = get_the_ID();
$username = get_post_meta($id, "steempress_sp_username");
......
......@@ -2,190 +2,64 @@
(function( $ ) {
'use strict';
function compare_posts(a,b) {
if (a.payout < b.payout)
return 1;
if (a.payout > b.payout)
return -1;
return 0;
}
function get_whole_values(base_value, time_fractions) {
var time_data = [base_value];
for (var i = 0; i < time_fractions.length; i++) {
time_data.push(parseInt(time_data[i]/time_fractions[i]));
time_data[i] = time_data[i] % time_fractions[i];
}; return time_data;
};
function datestr(elapsed)
{
const whole = get_whole_values(elapsed, [60,60,24]);
var display_date = "";
if (whole[3] !== 0) {
if (whole[3] === 1)
display_date += whole[3]+" day ";
else
display_date += whole[3]+" days ";
if (whole[2] !== 0) {
if (whole[2] === 1)
display_date += whole[2]+ " hour ago";
else
display_date += whole[2]+ " hours ago";
}
}
else
{
if (whole[2] !== 0) {
if (whole[2] === 1)
display_date += whole[2]+" hour ";
else
display_date += whole[2]+" hours ";
if (whole[1] !== 0) {
if (whole[1] === 1)
display_date += whole[1]+ " minute ago";
else
display_date += whole[1]+ " minutes ago";
}
}
else if (whole[1] !== 0) {
if (whole[1] === 1)
display_date += whole[1]+" minute ";
else
display_date += whole[1]+" minutes ";
if (whole[0] !== 0) {
if (whole[0] === 1)
display_date += whole[0]+ " second ago";
else
display_date += whole[0]+ " seconds ago";
}
}
else {
if (whole[0] === 1)
display_date += whole[0] + " second ago";
else
display_date += whole[0] + " seconds ago";
}
}
return display_date;
}
function get_all_comments(author, permlink, tag, callback)
{
steem.api.getState(tag+"/@"+author+"/"+permlink, function (err, post) {
if (err)
return resolve({error:err});
if (post['root_permlink'] === "" && post['root_author'] === "" )
return resolve({error:"content not found"});
var comment_list = post.content;
var comments_ordered = [];
var author_list = new Set([]); // will be used later to query the blockchain for user avatars
for (const comment_id in comment_list) {
function load_steem_capabilities() {
var comment = comment_list[comment_id];
const username = $("#steempress_sp_username")[0].innerText;
const permlink = $("#steempress_sp_permlink")[0].innerText;
const tag = $("#steempress_sp_tag")[0].innerText;
if (comment.depth !== 0)
continue;
comments_ordered.push(get_replies(comment, comment_list, author_list));
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return callback(comments_ordered);
});
}
function get_replies(comment, comment_list, author_list)
{
author_list.add(comment.author);
comment.date = datestr(Math.floor(new Date().getTime() / 1000) - (new Date(comment.created).getTime()/1000));
for (var i = 0; i < comment.replies.length; i++) {
var reply = comment_list[comment.replies[i]];
if (reply.total_payout_value !== "0.000 SBD")
reply.payout = reply.total_payout_value.replace("SBD", "");
else
reply.payout = reply.pending_payout_value.replace("SBD", "");
reply.payout = (Math.floor(parseFloat(reply.payout)*100)/100);
comment.replies[i] = get_replies(reply, comment_list, author_list);
return xhr;
}
comment.replies.sort(compare_posts);
return comment;
}
// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
var url = "http://localhost:8002";
function generate_comment_string(comments)
{
var str = "";
for (var i = 0; i < comments.length; i++)
{
str += "<li class=\"steempress_sp_cmmnt\">\n" +
" <div class=\"avatar\"><img src=\"https://steemitimages.com/u/"+comments[i].author+"/avatar\" style='height: 55px; width: 55px' alt=\""+comments[i].author+"'s avatar\"></div>\n" +
" <div class=\"steempress_sp_cmmnt-content\">\n" +
" <header><a href=\"https://steemit.com/@"+comments[i].author+"\" class=\"userlink\">"+comments[i].author+"</a> - <span class=\"pubdate\">"+comments[i].date+"</span> </header>\n" +
" <p class=\"steempress_sp_comment_text\">"+comments[i].body+"</p>\n" + comments[i].payout.toString()+" $"+
" </div>\n"
if (comments[i].replies.length !== 0) {
str += "<ul class=\"replies\">";
str += generate_comment_string(comments[i].replies);
str += "</ul>";
var xhr = createCORSRequest('POST', url);
var params = "username="+username+"&permlink="+permlink+"&tag="+tag;
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
if (!xhr) {
alert('CORS not supported');
return;
}
str += " </li>"
}
return str;
}
// Response handlers.
xhr.onload = function() {
var data = JSON.parse(xhr.responseText);
function load_steem_capabilities() {
$("#steempress_sp_price")[0].innerHTML = data.payout;
const username = $("#steempress_sp_username")[0].innerText;
const permlink = $("#steempress_sp_permlink")[0].innerText;
const tag = $("#steempress_sp_tag")[0].innerText;
$("#steempress_sp_comments")[0].innerHTML = data.comments;
get_all_comments(username, permlink, tag, function (result) {
console.log(data);
// TODO : Test this correctly
if (result.error)
return load_steem_capabilities();
};
var payout = "";
if (result[0].total_payout_value !== "0.000 SBD")
payout = result[0].total_payout_value.replace("SBD", "");
else
payout = result[0].pending_payout_value.replace("SBD", "");
payout = (Math.floor(parseFloat(payout) * 100) / 100);
payout = payout.toString() + " $";
$("#steempress_sp_price")[0].innerHTML = payout;
var comment_str = "<p>Steem comments <a href=\"https://wordpress.org/plugins/steempress/\">powered by SteemPress</a> : </p> <div id=\"steempress_sp_comment_container\"><ul id=\"steempress_sp_comments\">";
comment_str += generate_comment_string(result[0].replies);
comment_str += "</ul></div>";
$("#steempress_sp_comments")[0].innerHTML = comment_str;
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send(params);
}
});
makeCorsRequest();
}
$(window).load(load_steem_capabilities)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment