steem-login.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. var ACTIVE_USER = null;
  2. steem.api.setOptions({ url: STEEMIT_API_URL});
  3. function loadDataAnon (pendingLogin) {
  4. if (!pendingLogin) {
  5. populateVotes();
  6. }
  7. }
  8. function loadDataAuthenticated () {
  9. populateVotes();
  10. }
  11. $(document).ready(function () {
  12. console.log('ready');
  13. var isPendingLogin = window.sessionStorage && window.sessionStorage.getItem('steemit-active-user') != null;
  14. loadDataAnon(isPendingLogin);
  15. });
  16. sessionLogin();
  17. function sessionLogin () {
  18. if (window.sessionStorage) {
  19. var s = window.sessionStorage;
  20. var activeUser = s.getItem('steemit-active-user');
  21. if (activeUser) {
  22. activeUser = JSON.parse(activeUser);
  23. login(activeUser.username, activeUser.postingKey, function (err, account) {
  24. if (err) {
  25. return;
  26. }
  27. loadDataAuthenticated();
  28. });
  29. }
  30. }
  31. }
  32. function storeSessionLogin() {
  33. if (window.sessionStorage) {
  34. var s = window.sessionStorage;
  35. s.setItem('steemit-active-user', JSON.stringify({username: ACTIVE_USER.username, postingKey: ACTIVE_USER.postingKey}));
  36. }
  37. }
  38. function login (username, postingKey, cb) {
  39. if (!steem.auth.isWif(postingKey)) {
  40. cb('login-invalid-postingKey');
  41. return;
  42. }
  43. steem.api.getAccounts([username], function (err, accounts) {
  44. if (err) {
  45. console.log('could not login: ' + err);
  46. if (cb) {
  47. cb('login-unknown');
  48. }
  49. return;
  50. }
  51. if (accounts.length > 1) {
  52. console.log('multiple accounts are not implemented... not picking a default. bug me or send a pull request');
  53. if (cb) {
  54. cb('login-multiple-accounts-not-implemented');
  55. }
  56. return;
  57. }
  58. var account = accounts[0];
  59. var pubWif = account.posting.key_auths[0][0];
  60. var isValid = steem.auth.wifIsValid(postingKey, pubWif);
  61. if (!isValid) {
  62. cb('login-invalid');
  63. return;
  64. }
  65. ACTIVE_USER = {
  66. username: username,
  67. postingKey: postingKey,
  68. account: account
  69. };
  70. if (cb) {
  71. cb(null, ACTIVE_USER);
  72. }
  73. });
  74. }
  75. function postsByTag (tag, limit) {
  76. limit = limit || 5; // todo: is 0 valid/no limit/default limit?
  77. steem.api.getDiscussionsByBlog({tag: tag, limit: limit}, function(err, result) {
  78. console.log(err, result);
  79. });
  80. }
  81. function votes (cb) {
  82. steem.api.getActiveVotes(STEEMIT_AUTHOR, STEEMIT_PERMLINK, function (err, votes) {
  83. if (err) {
  84. console.log('error getting votes: ' + err);
  85. if (cb) {
  86. cb('votes-unknown');
  87. }
  88. return;
  89. }
  90. if (cb) {
  91. cb(null, votes);
  92. }
  93. });
  94. }
  95. function vote (weight, power, cb) {
  96. if (!ACTIVE_USER) {
  97. console.err('could not vote because not logged in');
  98. if (cb) {
  99. cb('vote-not-logged-in');
  100. }
  101. return;
  102. }
  103. weight = weight || 100;
  104. power = power || 100;
  105. var voter = ACTIVE_USER.account.name;
  106. var voteWeight = power * weight;
  107. steem.broadcast.vote(ACTIVE_USER.postingKey, voter, STEEMIT_AUTHOR, STEEMIT_PERMLINK, voteWeight, function (err, result) {
  108. if (err) {
  109. console.error('could not vote: ' + err);
  110. if (cb) {
  111. cb('vote-unknown');
  112. }
  113. return;
  114. }
  115. if (cb) {
  116. cb();
  117. }
  118. });
  119. }
  120. function logoutUser (loginFormId) {
  121. if (window.sessionStorage) {
  122. var s = window.sessionStorage;
  123. s.removeItem('steemit-active-user');
  124. }
  125. ACTIVE_USER = null;
  126. loadDataAuthenticated();
  127. cancelLoginUser(loginFormId);
  128. }
  129. function cancelLoginUser (loginFormId) {
  130. $('#' + loginFormId + '-container').hide();
  131. $('#' + loginFormId + '-button').show();
  132. }
  133. function loginUser (loginFormId) {
  134. var loginForm = $('#' + loginFormId);
  135. var username = $('.username', loginForm).val();
  136. var postingKey = $('.postingKey', loginForm).val();
  137. var errorMessageBox = $('.error-message', loginForm);
  138. if (!username || !postingKey) {
  139. errorMessageBox.text('Enter values into the fields');
  140. return;
  141. }
  142. login(username, postingKey, function (err, activeUser) {
  143. if (err) {
  144. if (err == 'login-invalid') {
  145. errorMessageBox.text('Invalid login');
  146. } else if (err == 'login-invalid-postingKey') {
  147. errorMessageBox.text('Invalid private posting key');
  148. } else {
  149. errorMessageBox.text('Unknown error');
  150. }
  151. return;
  152. }
  153. errorMessageBox.text('');
  154. storeSessionLogin();
  155. loadDataAuthenticated();
  156. cancelLoginUser(loginFormId);
  157. });
  158. }
  159. // permlink = permlink for article (add to post)
  160. // author = author for article (add to post)
  161. // wif = wif(username, postingKey)
  162. // voter = accounts(username)[0] // multiple accounts (dialog)
  163. // weight = dialog for voting weight
  164. // vote(wif, voter, author, permlink, weight)
  165. // postsByTag('{{ steemit-tag }}', 5)
  166. function voteUp () {
  167. console.log("voteUp");
  168. vote(100, null, populateVotes);
  169. }
  170. function voteDown () {
  171. console.log("voteDown");
  172. vote(-100, null, populateVotes);
  173. }
  174. function populateVotes () {
  175. votes(function(err, activeVotes) {
  176. if (err) {
  177. return;
  178. }
  179. var ups = [], downs = [], activeUserVote = null;
  180. activeVotes.forEach(function (v) {
  181. if (ACTIVE_USER && ACTIVE_USER.account.name == v.voter) {
  182. activeUserVote = v;
  183. }
  184. if (v.percent > 0) {
  185. ups.push(v);
  186. } else if (v.percent < 0) {
  187. downs.push(v);
  188. } else {
  189. console.log('0 weight vote... not counting');
  190. }
  191. });
  192. var voteEl = $('#vote-video');
  193. if (ups.length) {
  194. $('.vote-up .count', voteEl).text('(' + ups.length + ')');
  195. } else {
  196. $('.vote-up .count', voteEl).text('');
  197. }
  198. if (downs.length) {
  199. $('.vote-down .count', voteEl).text('(' + downs.length + ')');
  200. } else {
  201. $('.vote-down .count', voteEl).text('');
  202. }
  203. if (activeUserVote) {
  204. if (activeUserVote.percent > 0) {
  205. $('.vote-up .count', voteEl).css('font-weight', 'bold');
  206. $('.vote-down .count', voteEl).css('font-weight', 'normal');
  207. } else {
  208. $('.vote-up .count', voteEl).css('font-weight', 'normal');
  209. $('.vote-down .count', voteEl).css('font-weight', 'bold');
  210. }
  211. } else {
  212. $('.vote-up .count', voteEl).css('font-weight', 'normal');
  213. $('.vote-down .count', voteEl).css('font-weight', 'normal') }
  214. });
  215. }