Browse Source

added steemit login for video voting

Harlan Iverson 7 years ago
parent
commit
5afcc24949

File diff suppressed because it is too large
+ 0 - 0
ispooge.com-v2/app/resources/templates/assets/steem.js


+ 3 - 0
ispooge.com-v2/app/resources/templates/config.edn

@@ -29,6 +29,9 @@
  :keep-files           [".git"]
  :disqus?              false
  :disqus-shortname     ""
+ :steemit?             true
+ :steemit-api-url      "https://api.steemit.com"
+ :steemit-tag          "ispooge"
  :ignored-files        [#"\.#.*" #".*\.swp$"]
  :posts-per-page       8
  :blocks-per-preview   2

+ 4 - 4
ispooge.com-v2/app/resources/templates/themes/ispooge/html/base.html

@@ -10,6 +10,9 @@
     <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
     <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
     <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/default.min.css">
+    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
+<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
+    <script src="/assets/steem.js"></script>
     {% style "css/screen.css" %}
 
     {% block head %}
@@ -84,8 +87,6 @@
 
 
 <div class="container">
-
-
     <div class="row">
         <div class="col-lg-12">
             <div id="content">
@@ -97,8 +98,7 @@
     <footer>Copyright &copy; {{today|date:yyyy}} {{author}}
         <p style="text-align: center;">Powered by <a href="http://cryogenweb.org">Cryogen</a></p></footer>
 </div>
-<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
-<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
+
 {% script "js/highlight.pack.js" %}
 <script>hljs.initHighlightingOnLoad();</script>
 {% if post.klipse %} {{post.klipse|safe}} {% endif %}

+ 23 - 0
ispooge.com-v2/app/resources/templates/themes/ispooge/html/partials/steem-login.html

@@ -0,0 +1,23 @@
+<div id="{{form-id}}-container" style="text-align: center; width: 100%">
+  <form id="{{form-id}}" style="display: table; margin: auto; text-align: left; border: 1px solid black; padding: 8px; width: fit-content;">
+    <h2>STEEM Login</h2>
+    <p class="error-message"></p>
+    <p>
+        <input class="username" name="username" type="text" placeholder="Username">
+    </p>
+    <p>
+      <input class="postingKey" name="postingKey" type="password" placeholder="Private Posting Key">
+      <a href="javascript:void(0)" title="SteemIt.com &gt; Wallet &gt; Permissions &gt; Posting Key (read description) &gt; Private Key">?</a>
+    </p>
+    <p style="text-align: center;">
+      Valid until the tab is closed
+    </p>
+    <p style="text-align: center">
+      <input type="button" value="Login" onclick="{{login-function}}('{{form-id}}');">
+      <input type="button" value="Cancel" onclick="{{cancel-function}}('{{form-id}}');">
+    </p>
+    <p style="text-align: center;">
+      <a href="javascript:{{logout-function}}('{{form-id}}');">Logout</a>
+    </p>
+  </form>
+</div>

+ 33 - 0
ispooge.com-v2/app/resources/templates/themes/ispooge/html/video.html

@@ -6,6 +6,10 @@
 {% include "/html/partials/video-head.html" %}
 
 
+<style type="text/css">
+  #steemit-login-container {display: none;}
+</style>
+
 {% endblock %}
 
 {% block content %}
@@ -18,6 +22,35 @@
 
     {% include "/html/partials/video-player.html" %}
 
+
+{% if all steemit? post.steemit-permlink %}
+
+<button id="steemit-login-button" onclick="$('#steemit-login-container').show(); $(this).hide();" title="We encourage you to VIEW SOURCE, or vote on SteemIt">STEEM Login</button> 
+<span class="view-on-steemit"><a href="https://steemit.com/@{{ post.steemit-author }}/{{ post.steemit-permlink }}">View on SteemIt</a></span>
+{% include "/html/partials/steem-login.html" with form-id="steemit-login" login-function="loginUser" cancel-function="cancelLoginUser" logout-function="logoutUser" %}
+
+
+<script>
+
+var STEEMIT_API_URL = '{{ steemit-api-url }}';
+var STEEMIT_AUTHOR = '{{ post.steemit-author }}';
+var STEEMIT_PERMLINK = '{{ post.steemit-permlink }}';
+
+</script>
+
+{% script "js/steem-login.js" %}
+
+    
+    <div id="vote-video">
+      STEEM Vote: 
+      <button class="vote-up" onclick="voteUp()">UP <span class="count"></span></button>
+      <button class="vote-down" onclick="voteDown()">DOWN <span class="count"></span></button>
+    </div>
+
+
+{% endif %}
+
+
     {{post.content|safe}}
 
     {% include "/html/partials/post-pagination.html" %}

+ 270 - 0
ispooge.com-v2/app/resources/templates/themes/ispooge/js/steem-login.js

@@ -0,0 +1,270 @@
+
+var ACTIVE_USER = null;
+
+steem.api.setOptions({ url: STEEMIT_API_URL});
+
+function loadDataAnon (pendingLogin) {
+  if (!pendingLogin) {
+    populateVotes();
+  }
+}
+
+function loadDataAuthenticated () {
+  populateVotes();
+}
+
+
+
+
+$(document).ready(function () {
+  console.log('ready');
+  var isPendingLogin = window.sessionStorage && window.sessionStorage.getItem('steemit-active-user') != null;
+  
+  loadDataAnon(isPendingLogin);
+});
+
+sessionLogin();
+
+function sessionLogin () {
+  if (window.sessionStorage) {
+    var s = window.sessionStorage;
+    var activeUser = s.getItem('steemit-active-user');
+  
+    if (activeUser) {
+      activeUser = JSON.parse(activeUser);
+      login(activeUser.username, activeUser.postingKey, function (err, account) {
+        if (err) {
+          return;
+        }
+        loadDataAuthenticated();
+      });
+    }
+  }
+}
+
+function storeSessionLogin() {
+  if (window.sessionStorage) {
+    var s = window.sessionStorage;
+    s.setItem('steemit-active-user', JSON.stringify({username: ACTIVE_USER.username, postingKey: ACTIVE_USER.postingKey}));
+  }
+}
+
+function login (username, postingKey, cb) {
+
+  if (!steem.auth.isWif(postingKey)) {
+    cb('login-invalid-postingKey');
+    return;
+  }
+
+  steem.api.getAccounts([username], function (err, accounts) {
+    if (err) {
+      console.log('could not login: ' + err);
+      if (cb) {
+        cb('login-unknown');
+      }
+      return;
+    }
+
+    if (accounts.length > 1) {
+      console.log('multiple accounts are not implemented... not picking a default. bug me or send a pull request');
+      if (cb) {
+        cb('login-multiple-accounts-not-implemented');
+      }
+      return;
+    }
+
+    var account = accounts[0];
+    
+    
+    var pubWif = account.posting.key_auths[0][0];
+    var isValid = steem.auth.wifIsValid(postingKey, pubWif);
+    
+    if (!isValid) {
+      cb('login-invalid');
+      return;
+    }
+    
+    ACTIVE_USER = {
+      username: username,
+      postingKey: postingKey,
+      account: account
+    };
+
+    if (cb) {
+      cb(null, ACTIVE_USER);
+    }
+  });
+}
+
+function postsByTag (tag, limit) {
+  limit = limit || 5; // todo: is 0 valid/no limit/default limit?
+  steem.api.getDiscussionsByBlog({tag: tag, limit: limit}, function(err, result) {
+    console.log(err, result);
+  });
+}
+
+function votes (cb) {
+  steem.api.getActiveVotes(STEEMIT_AUTHOR, STEEMIT_PERMLINK, function (err, votes) {
+    if (err) {
+      console.log('error getting votes: ' + err);
+      if (cb) {
+        cb('votes-unknown');
+      }
+      return;
+    }
+
+    if (cb) {
+      cb(null, votes);
+    }
+  });
+}
+
+function vote (weight, power, cb) {
+  if (!ACTIVE_USER) {
+    console.err('could not vote because not logged in');
+    if (cb) {
+      cb('vote-not-logged-in');
+    }
+    return;
+  }
+  
+  weight = weight || 100;
+  power = power || 100;
+
+  var voter = ACTIVE_USER.account.name;
+  var voteWeight = power * weight;
+  
+  steem.broadcast.vote(ACTIVE_USER.postingKey, voter, STEEMIT_AUTHOR, STEEMIT_PERMLINK, voteWeight, function (err, result) {
+    if (err) {
+      console.error('could not vote: ' + err);
+      if (cb) {
+        cb('vote-unknown');
+      }
+      return;
+    }
+    
+    if (cb) {
+      cb();
+    }
+  });
+}
+
+function logoutUser (loginFormId) {
+  if (window.sessionStorage) {
+    var s = window.sessionStorage;
+    s.removeItem('steemit-active-user');
+  }
+
+  ACTIVE_USER = null;
+
+  loadDataAuthenticated();
+  cancelLoginUser(loginFormId);
+}
+
+function cancelLoginUser (loginFormId) {
+  $('#' + loginFormId + '-container').hide();
+  $('#' + loginFormId + '-button').show();
+}
+
+
+function loginUser (loginFormId) {
+  var loginForm = $('#' + loginFormId);
+  var username = $('.username', loginForm).val();
+  var postingKey = $('.postingKey', loginForm).val();
+
+  var errorMessageBox = $('.error-message', loginForm);
+  
+  if (!username || !postingKey) {
+    errorMessageBox.text('Enter values into the fields');
+    return;  
+  }
+
+  login(username, postingKey, function (err, activeUser) {
+    if (err) {
+      if (err == 'login-invalid') {
+        errorMessageBox.text('Invalid login');
+      } else if (err == 'login-invalid-postingKey') {
+        errorMessageBox.text('Invalid private posting key');
+      } else {
+        errorMessageBox.text('Unknown error');
+      }
+      return;
+    }
+
+    errorMessageBox.text('');
+    storeSessionLogin();
+    loadDataAuthenticated();
+
+    cancelLoginUser(loginFormId);
+  });
+}
+
+// permlink = permlink for article (add to post)
+// author = author for article (add to post)
+
+// wif = wif(username, postingKey)
+// voter = accounts(username)[0] // multiple accounts (dialog)
+// weight = dialog for voting weight
+
+// vote(wif, voter, author, permlink, weight)
+
+// postsByTag('{{ steemit-tag }}', 5)
+
+function voteUp () {
+  console.log("voteUp");
+  vote(100, null, populateVotes);
+}
+
+function voteDown () {
+  console.log("voteDown");
+  vote(-100, null, populateVotes);
+
+}
+
+function populateVotes () {
+  votes(function(err, activeVotes) {
+    if (err) {
+      return;
+    }
+    
+    var ups = [], downs = [], activeUserVote = null;
+    activeVotes.forEach(function (v) {
+      if (ACTIVE_USER && ACTIVE_USER.account.name == v.voter) {
+        activeUserVote = v;
+      }
+      if (v.percent > 0) {
+        ups.push(v);
+      } else if (v.percent < 0) {
+        downs.push(v);
+      } else {
+        console.log('0 weight vote... not counting');
+      }
+    });
+
+    var voteEl = $('#vote-video');
+
+    if (ups.length) {
+      $('.vote-up .count', voteEl).text('(' + ups.length + ')');
+    } else {
+      $('.vote-up .count', voteEl).text('');
+    }
+
+    if (downs.length) {
+      $('.vote-down .count', voteEl).text('(' + downs.length + ')');
+    } else {
+      $('.vote-down .count', voteEl).text('');
+    }
+
+    if (activeUserVote) {
+      if (activeUserVote.percent > 0) {
+        $('.vote-up .count', voteEl).css('font-weight', 'bold');
+        $('.vote-down .count', voteEl).css('font-weight', 'normal');
+      } else {
+        $('.vote-up .count', voteEl).css('font-weight', 'normal');
+        $('.vote-down .count', voteEl).css('font-weight', 'bold');
+      }
+    } else {
+      $('.vote-up .count', voteEl).css('font-weight', 'normal');
+      $('.vote-down .count', voteEl).css('font-weight', 'normal')    }
+  });
+}

Some files were not shown because too many files changed in this diff