From ecefa417926ab70b0608cd70c72564853d6b2527 Mon Sep 17 00:00:00 2001 From: Arinerron Date: Sun, 2 Sep 2018 22:08:12 -0700 Subject: [PATCH] Complete first part of work on onionrui --- onionr/api.py | 2 +- onionr/static-data/bootstrap-nodes.txt | 1 + .../www/ui/common/default-icon.html | 1 + onionr/static-data/www/ui/common/footer.html | 17 +- onionr/static-data/www/ui/dist/css/main.css | 33 +++ .../www/ui/dist/css/themes/dark.css | 29 +++ onionr/static-data/www/ui/dist/index.html | 51 ++++- onionr/static-data/www/ui/dist/js/main.js | 203 ++++++++++++++++-- onionr/static-data/www/ui/dist/js/timeline.js | 123 ++++++++--- onionr/static-data/www/ui/lang.json | 13 +- onionr/static-data/www/ui/src/css/main.css | 33 +++ .../www/ui/src/css/themes/dark.css | 29 +++ onionr/static-data/www/ui/src/index.html | 36 +++- onionr/static-data/www/ui/src/js/main.js | 202 +++++++++++++++-- onionr/static-data/www/ui/src/js/timeline.js | 127 ++++++++--- 15 files changed, 797 insertions(+), 103 deletions(-) create mode 100644 onionr/static-data/www/ui/common/default-icon.html diff --git a/onionr/api.py b/onionr/api.py index 4780034f..0f3bde41 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -250,7 +250,7 @@ class API: response = siteData.split(b'-', 2)[-1] resp = Response(response) elif action == 'info': - resp = new Response(json.dumps({'id' : 'not yet implemented'})) + resp = Response(json.dumps({'pubkey' : self._core._crypto.pubKey, 'host' : self._core.hsAdder})) elif action == "insertBlock": response = {'success' : False, 'reason' : 'An unknown error occurred'} diff --git a/onionr/static-data/bootstrap-nodes.txt b/onionr/static-data/bootstrap-nodes.txt index 5473550f..67459d90 100644 --- a/onionr/static-data/bootstrap-nodes.txt +++ b/onionr/static-data/bootstrap-nodes.txt @@ -1,2 +1,3 @@ onionragxuddecmg.onion dgyllprmtmym4gbk.onion +eczfevdpirhvbniy.onion diff --git a/onionr/static-data/www/ui/common/default-icon.html b/onionr/static-data/www/ui/common/default-icon.html new file mode 100644 index 00000000..86ccf773 --- /dev/null +++ b/onionr/static-data/www/ui/common/default-icon.html @@ -0,0 +1 @@ +/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAcFBQYFBAcGBQYIBwcIChELCgkJChUPEAwRGBUaGRgVGBcbHichGx0lHRcYIi4iJSgpKywrGiAvMy8qMicqKyr/2wBDAQcICAoJChQLCxQqHBgcKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKir/wAARCACAAIADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDrtTvrlL51jlkyGPANUZNSuvJZ2uJFYHjB6UmpTE6jcZUH5iCR0FQQLHvww3An8K8jmuz0lHQvwXV1gNLcSBmGcZqcXtwo/wBe/X1rzqw1e/stWmaTdKpcl1Le9dqmoJc2qupxnoCOauUWkOzRpnULhsATMPXmoptSuFGPPfjvms8Xew4OaY7NOSEyAT3rK9w5bFn+0rlmCrPIvqc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T3GXVHyA+dufeuedNPU6adS2hxtxFOIS3lsZZASiMvfoGqlNb31g0dtnZu+ZnH3vr9a7V7iKW6WK0ge7nkON5Xauf8BVTW7CSDT5jdkRSS5LSY5I/oPaudw5TrjUuZOnX9lt2G4leUDBO7j8RWxaX1urj/AEWE+jp6+4NcCYDcaiyWaKijptX5vwPua0H0y/gVZcXicfeLZFZSj5mySZ6OmpwiEyRLl1+9C67SP8+tYuo61a6nFJAEktpPQ9DWXpFprGqbbd/MaMcFmToPr1rpD4OijVTN50zDH3RyfxqbtbE8sYvU/9k= diff --git a/onionr/static-data/www/ui/common/footer.html b/onionr/static-data/www/ui/common/footer.html index 6b5cfb06..0143c2d8 100644 --- a/onionr/static-data/www/ui/common/footer.html +++ b/onionr/static-data/www/ui/common/footer.html @@ -1,4 +1,19 @@ - + + + + diff --git a/onionr/static-data/www/ui/dist/css/main.css b/onionr/static-data/www/ui/dist/css/main.css index dab080ef..a69216cb 100644 --- a/onionr/static-data/www/ui/dist/css/main.css +++ b/onionr/static-data/www/ui/dist/css/main.css @@ -60,6 +60,35 @@ body { width: 100%; } +.onionr-post-creator { + padding: 1rem; + margin-bottom: 1rem; + + width: 100%; +} + +.onionr-post-creator-user-name { + display: inline; +} + +.onionr-post-creator-user-id:before { content: "("; } +.onionr-post-creator-user-id:after { content: ")"; } + +.onionr-post-creator-content { + word-wrap: break-word; + width: 100%; +} + +.onionr-post-creator-user-icon { + border-radius: 100%; + width: 100%; +} + +.onionr-post-creator-create { + width: 100%; + text-align: center; +} + .h-divider { margin: 5px 15px; height: 1px; @@ -77,3 +106,7 @@ body { .onionr-profile-username { text-align: center; } + +.onionr-profile-save { + width: 100%; +} diff --git a/onionr/static-data/www/ui/dist/css/themes/dark.css b/onionr/static-data/www/ui/dist/css/themes/dark.css index b0473390..5ff6cc40 100644 --- a/onionr/static-data/www/ui/dist/css/themes/dark.css +++ b/onionr/static-data/www/ui/dist/css/themes/dark.css @@ -31,6 +31,35 @@ body { font-size: 15pt; } +.onionr-post-creator { + border: 1px solid black; + border-radius: 1rem; + + background-color: lightgray; +} + +.onionr-post-creator-user-name { + color: green; + font-weight: bold; +} + +.onionr-post-creator-user-id { + color: gray; +} + +.onionr-post-creator-date { + color: gray; +} + +.onionr-post-creator-content { + font-family: sans-serif, serif; + border-top: 1px solid black; + font-size: 15pt; + background-color: lightgray; + color: black; + border-width: 0px; +} + .h-divider { border-top:1px solid gray; } diff --git a/onionr/static-data/www/ui/dist/index.html b/onionr/static-data/www/ui/dist/index.html index 02ee47b4..ba0c31d4 100644 --- a/onionr/static-data/www/ui/dist/index.html +++ b/onionr/static-data/www/ui/dist/index.html @@ -40,10 +40,16 @@
- +
-

arinerron

+

+
+
+ +
+
+
@@ -52,6 +58,32 @@
+
+ +
+
+
+
+ +
+
+
+
+ + you +
+
+ + + + +
+
+
+
+ +
+
@@ -69,6 +101,21 @@
+ + + diff --git a/onionr/static-data/www/ui/dist/js/main.js b/onionr/static-data/www/ui/dist/js/main.js index 6bbbdb27..71c80266 100644 --- a/onionr/static-data/www/ui/dist/js/main.js +++ b/onionr/static-data/www/ui/dist/js/main.js @@ -44,16 +44,17 @@ function deserializeUser(id) { return user; } -function serializeUser(user) { - if(user !== null && user !== undefined) { - var serialized = {'name' : user.getName(), 'id' : user.getID(), 'icon' : user.getIcon()}; +function getCurrentUser() { + var user = get('currentUser', null); - usermap[user.getID()] = serialized; + if(user === null) + return null; - set('usermap', JSON.stringify(getUserMap())); + return User.getUser(user, function() {}); +} - return serialized; - } +function setCurrentUser(user) { + set('currentUser', user.getID()); } /* returns a relative date format, e.g. "5 minutes" */ @@ -131,6 +132,11 @@ class Sanitize { static url(url) { return encodeURIComponent(url); } + + /* usernames */ + static username(username) { + return String(username).replace(/[\W_]+/g, " ").substring(0, 25); + } } /* config stuff */ @@ -182,18 +188,75 @@ class User { return this.image; } + setDescription(description) { + this.description = description; + } + + getDescription() { + return this.description; + } + serialize() { return { 'name' : this.getName(), 'id' : this.getID(), - 'icon' : this.getIcon() + 'icon' : this.getIcon(), + 'description' : this.getDescription() }; } + /* save in usermap */ remember() { usermap[this.getID()] = this.serialize(); set('usermap', JSON.stringify(usermap)); } + + /* save as a block */ + save(callback) { + var block = new Block(); + + block.setType('onionr-user'); + block.setContent(JSON.stringify(this.serialize())); + + return block.save(true, callback); + } + + static getUser(id, callback) { + var user = deserializeUser(id); + if(user === null) { + Block.getBlocks({'type' : 'onionr-user-info', 'signed' : true, 'reverse' : true}, function(data) { + if(data.length !== 0) { + try { + user = new User(); + + var userInfo = JSON.parse(data[0].getContent()); + + if(userInfo['id'] === id) { + user.setName(userInfo['name']); + user.setIcon(userInfo['icon']); + user.setID(id); + + user.remember(); + + callback(user); + return user; + } + } catch(e) { + console.log(e); + + callback(null); + return null; + } + } else { + callback(null); + return null; + } + }); + } else { + callback(user); + return user; + } + } } /* post class */ @@ -278,6 +341,35 @@ class Post { getPostDate() { return this.date; } + + save(callback) { + var args = {'type' : 'onionr-post', 'sign' : true, 'content' : JSON.stringify({'content' : this.getContent()})}; + + var url = '/client/?action=insertBlock&data=' + Sanitize.url(JSON.stringify(args)) + '&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + if(callback !== undefined) { + // async + + http.addEventListener('load', function() { + callback(Block.parseBlockArray(JSON.parse(http.responseText)['hash'])); + }, false); + + http.open('GET', url, true); + http.timeout = 5000; + http.send(null); + } else { + // sync + + http.open('GET', url, false); + http.send(null); + + return Block.parseBlockArray(JSON.parse(http.responseText)['hash']); + } + } } /* block class */ @@ -380,6 +472,52 @@ class Block { return !(this.hash === null || this.hash === undefined); } + // saves the block, returns the hash + save(sign, callback) { + var type = this.getType(); + var content = this.getContent(); + var parent = this.getParent(); + + if(content !== undefined && content !== null && type !== '') { + var args = {'content' : content}; + + if(type !== undefined && type !== null && type !== '') + args['type'] = type; + if(parent !== undefined && parent !== null && parent.getHash() !== undefined && parent.getHash() !== null && parent.getHash() !== '') + args['parent'] = parent.getHash(); + if(sign !== undefined && sign !== null) + args['sign'] = String(sign) !== 'false' + + + var url = '/client/?action=insertBlock&data=' + Sanitize.url(JSON.stringify(args)) + '&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + if(callback !== undefined) { + // async + + http.addEventListener('load', function() { + callback(Block.parseBlockArray(JSON.parse(http.responseText)['hash'])); + }, false); + + http.open('GET', url, true); + http.timeout = 5000; + http.send(null); + } else { + // sync + + http.open('GET', url, false); + http.send(null); + + return Block.parseBlockArray(JSON.parse(http.responseText)['hash']); + } + } + + return false; + } + /* static functions */ // recreates a block by hash @@ -463,19 +601,56 @@ class Block { /* temporary code */ +var tt = getParameter("timingToken"); +if(tt !== null && tt !== undefined) { + setTimingToken(tt); +} + if(getWebPassword() === null) { var password = ""; while(password.length != 64) { password = prompt("Please enter the web password (run `./RUN-LINUX.sh --get-password`)"); } - setTimingToken(prompt("Please enter the timing token (optional)")); - setWebPassword(password); - window.location.reload(true); } -var tt = getParameter("timingToken"); -if(tt !== null && tt !== undefined) { - setTimingToken(tt); +if(getCurrentUser() === null) { + var url = '/client/?action=info&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + // sync + + http.open('GET', url, false); + http.send(null); + + var id = JSON.parse(http.responseText)['pubkey']; + + User.getUser(id, function(data) { + if(data === null || data === undefined) { + jQuery('#modal').modal('show'); + + var user = new User(); + + user.setName('New User'); + user.setID(id); + user.setIcon('/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAcFBQYFBAcGBQYIBwcIChELCgkJChUPEAwRGBUaGRgVGBcbHichGx0lHRcYIi4iJSgpKywrGiAvMy8qMicqKyr/2wBDAQcICAoJChQLCxQqHBgcKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKir/wAARCACAAIADASIAAhEB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FmToPr1rpD4OijVTN50zDH3RyfxqbtbE8sYvU/9k=\ +'); + user.setDescription('A new OnionrUI user'); + + user.remember(); + user.save(); + + setCurrentUser(user); + + window.location.reload(); + } else { + setCurrentUser(data); + } + }); } + +currentUser = getCurrentUser(); diff --git a/onionr/static-data/www/ui/dist/js/timeline.js b/onionr/static-data/www/ui/dist/js/timeline.js index e7a3c472..af554f80 100644 --- a/onionr/static-data/www/ui/dist/js/timeline.js +++ b/onionr/static-data/www/ui/dist/js/timeline.js @@ -1,35 +1,3 @@ -function getUserInfo(id, callback) { - var user = deserializeUser(id); - if(user === null) { - Block.getBlocks({'type' : 'onionr-user-info', 'signed' : true, 'reverse' : true}, function(data) { - if(data.length !== 0) { - try { - user = new User(); - - var userInfo = JSON.parse(data[0].getContent()); - - if(userInfo['id'] === id) { - user.setName(userInfo['name']); - user.setIcon(userInfo['icon']); - user.setID(id); - - serializeUser(user); - - return callback(user); - } - } catch(e) { - console.log(e); - return callback(null); - } - } else { - return callback(null); - } - }); - } else { - return callback(user); - } -} - /* just for testing rn */ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, function(data) { for(var i = 0; i < data.length; i++) { @@ -37,7 +5,7 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun var block = data[i]; var finished = false; - getUserInfo(new String(block.getHeader('signer', 'unknown')), function(user) { + User.getUser(new String(block.getHeader('signer', 'unknown')), function(user) { var post = new Post(); var blockContent = JSON.parse(block.getContent()); @@ -53,6 +21,7 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun while(!finished); } catch(e) { + console.log('Troublemaker block: ' + data[i].getHash()); console.log(e); } } @@ -62,11 +31,97 @@ function viewProfile(id, name) { id = decodeURIComponent(id); document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(decodeURIComponent(name)); - getUserInfo(id, function(data) { + User.getUser(id, function(data) { if(data !== null) { document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(data.getName()); document.getElementById("onionr-profile-username").title = Sanitize.html(data.getID()); document.getElementById("onionr-profile-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(data.getIcon()); + document.getElementById("onionr-profile-user-icon").b64 = Sanitize.html(data.getIcon()); } }); } + +function updateUser() { + toggleSaveButton(false); + + jQuery('#modal').modal('show'); + + var name = jQuery('#onionr-profile-username').text(); + var id = document.getElementById("onionr-profile-username").title; + var icon = document.getElementById("onionr-profile-user-icon").b64; + var description = 'todo'; + + var user = new User(); + + user.setName(name); + user.setID(id); + user.setIcon(icon); + user.setDescription(description); + + user.remember(); + user.save(); + + window.location.reload(); +} + +function cancelUpdate() { + toggleSaveButton(false); + + var name = jQuery('#onionr-profile-username').text(); + var id = document.getElementById("onionr-profile-username").title; + + viewProfile(id, name); +} + +function toggleSaveButton(show) { + document.getElementById("onionr-profile-save").style.display = (show ? 'block' : 'none'); + document.getElementById("onionr-profile-cancel").style.display = (show ? 'block' : 'none'); +} + +function makePost() { + var content = document.getElementById("onionr-post-creator-content").value; + + var post = new Post(); + + post.setUser(getCurrentUser()); + post.setContent(content); + post.setPostDate(new Date()); + + post.save(function(data) {}); // async, but no function + + document.getElementById('onionr-timeline-posts').innerHTML = post.getHTML() + document.getElementById('onionr-timeline-posts').innerHTML; + + document.getElementById("onionr-post-creator-content").value = ""; +} + +$('body').on('click', '[data-editable]', function() { + var el = jQuery(this); + var txt = el.text(); + + var input = jQuery('').val(txt); + el.replaceWith(input); + + var save = function() { + var newTxt = input.val(); + var p = el.text(Sanitize.username(newTxt)); + input.replaceWith(p); + + if(newTxt !== txt) + toggleSaveButton(true); + }; + + input.one('blur', save).focus(); +}); + +document.getElementById("onionr-post-creator-user-name").innerHTML = Sanitize.html(currentUser.getName()); +document.getElementById("onionr-post-creator-user-id").innerHTML = "you"; +document.getElementById("onionr-post-creator-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(currentUser.getIcon()); +document.getElementById("onionr-post-creator-user-id").title = currentUser.getID(); +document.getElementById("onionr-post-creator-content").placeholder = "Enter a message here..."; + +viewCurrentProfile = function() { + viewProfile(encodeURIComponent(currentUser.getID()), encodeURIComponent(currentUser.getName())); +} + +document.getElementById("onionr-post-creator-user-id").onclick = viewCurrentProfile; +document.getElementById("onionr-post-creator-user-name").onclick = viewCurrentProfile; diff --git a/onionr/static-data/www/ui/lang.json b/onionr/static-data/www/ui/lang.json index 28606a56..872a68cf 100644 --- a/onionr/static-data/www/ui/lang.json +++ b/onionr/static-data/www/ui/lang.json @@ -6,10 +6,21 @@ "NOTIFICATIONS" : "Notifications", "MESSAGES" : "Messages", + "LATEST" : "Latest...", "TRENDING" : "Trending", + "MODAL_TITLE" : "Loading...", + "MODAL_MESSAGE" : "Onionr has begun performing a CPU-intensive operation. If this operation does not complete in the next 10 seconds, try reloading the page.", + "POST_LIKE" : "like", - "POST_REPLY" : "reply" + "POST_REPLY" : "reply", + + "POST_CREATOR_YOU" : "you", + "POST_CREATOR_PLACEHOLDER" : "Enter a message here...", + "POST_CREATOR_CREATE" : "Create post", + + "PROFILE_EDIT_SAVE" : "Save", + "PROFILE_EDIT_CANCEL" : "Cancel" }, "spa" : { diff --git a/onionr/static-data/www/ui/src/css/main.css b/onionr/static-data/www/ui/src/css/main.css index dab080ef..a69216cb 100644 --- a/onionr/static-data/www/ui/src/css/main.css +++ b/onionr/static-data/www/ui/src/css/main.css @@ -60,6 +60,35 @@ body { width: 100%; } +.onionr-post-creator { + padding: 1rem; + margin-bottom: 1rem; + + width: 100%; +} + +.onionr-post-creator-user-name { + display: inline; +} + +.onionr-post-creator-user-id:before { content: "("; } +.onionr-post-creator-user-id:after { content: ")"; } + +.onionr-post-creator-content { + word-wrap: break-word; + width: 100%; +} + +.onionr-post-creator-user-icon { + border-radius: 100%; + width: 100%; +} + +.onionr-post-creator-create { + width: 100%; + text-align: center; +} + .h-divider { margin: 5px 15px; height: 1px; @@ -77,3 +106,7 @@ body { .onionr-profile-username { text-align: center; } + +.onionr-profile-save { + width: 100%; +} diff --git a/onionr/static-data/www/ui/src/css/themes/dark.css b/onionr/static-data/www/ui/src/css/themes/dark.css index b0473390..5ff6cc40 100644 --- a/onionr/static-data/www/ui/src/css/themes/dark.css +++ b/onionr/static-data/www/ui/src/css/themes/dark.css @@ -31,6 +31,35 @@ body { font-size: 15pt; } +.onionr-post-creator { + border: 1px solid black; + border-radius: 1rem; + + background-color: lightgray; +} + +.onionr-post-creator-user-name { + color: green; + font-weight: bold; +} + +.onionr-post-creator-user-id { + color: gray; +} + +.onionr-post-creator-date { + color: gray; +} + +.onionr-post-creator-content { + font-family: sans-serif, serif; + border-top: 1px solid black; + font-size: 15pt; + background-color: lightgray; + color: black; + border-width: 0px; +} + .h-divider { border-top:1px solid gray; } diff --git a/onionr/static-data/www/ui/src/index.html b/onionr/static-data/www/ui/src/index.html index f34e8605..99463d27 100644 --- a/onionr/static-data/www/ui/src/index.html +++ b/onionr/static-data/www/ui/src/index.html @@ -10,10 +10,16 @@
- +
-

arinerron

+

+
+
+ +
+
+
@@ -22,6 +28,32 @@
+
+ +
+
+
+
+ +
+
+
+
+ + you +
+
+ + + + +
+
+
+
+ +
+
diff --git a/onionr/static-data/www/ui/src/js/main.js b/onionr/static-data/www/ui/src/js/main.js index 50738052..ca2c5214 100644 --- a/onionr/static-data/www/ui/src/js/main.js +++ b/onionr/static-data/www/ui/src/js/main.js @@ -44,16 +44,17 @@ function deserializeUser(id) { return user; } -function serializeUser(user) { - if(user !== null && user !== undefined) { - var serialized = {'name' : user.getName(), 'id' : user.getID(), 'icon' : user.getIcon()}; +function getCurrentUser() { + var user = get('currentUser', null); - usermap[user.getID()] = serialized; + if(user === null) + return null; - set('usermap', JSON.stringify(getUserMap())); + return User.getUser(user, function() {}); +} - return serialized; - } +function setCurrentUser(user) { + set('currentUser', user.getID()); } /* returns a relative date format, e.g. "5 minutes" */ @@ -131,6 +132,11 @@ class Sanitize { static url(url) { return encodeURIComponent(url); } + + /* usernames */ + static username(username) { + return String(username).replace(/[\W_]+/g, " ").substring(0, 25); + } } /* config stuff */ @@ -182,18 +188,75 @@ class User { return this.image; } + setDescription(description) { + this.description = description; + } + + getDescription() { + return this.description; + } + serialize() { return { 'name' : this.getName(), 'id' : this.getID(), - 'icon' : this.getIcon() + 'icon' : this.getIcon(), + 'description' : this.getDescription() }; } + /* save in usermap */ remember() { usermap[this.getID()] = this.serialize(); set('usermap', JSON.stringify(usermap)); } + + /* save as a block */ + save(callback) { + var block = new Block(); + + block.setType('onionr-user'); + block.setContent(JSON.stringify(this.serialize())); + + return block.save(true, callback); + } + + static getUser(id, callback) { + var user = deserializeUser(id); + if(user === null) { + Block.getBlocks({'type' : 'onionr-user-info', 'signed' : true, 'reverse' : true}, function(data) { + if(data.length !== 0) { + try { + user = new User(); + + var userInfo = JSON.parse(data[0].getContent()); + + if(userInfo['id'] === id) { + user.setName(userInfo['name']); + user.setIcon(userInfo['icon']); + user.setID(id); + + user.remember(); + + callback(user); + return user; + } + } catch(e) { + console.log(e); + + callback(null); + return null; + } + } else { + callback(null); + return null; + } + }); + } else { + callback(user); + return user; + } + } } /* post class */ @@ -246,6 +309,35 @@ class Post { getPostDate() { return this.date; } + + save(callback) { + var args = {'type' : 'onionr-post', 'sign' : true, 'content' : JSON.stringify({'content' : this.getContent()})}; + + var url = '/client/?action=insertBlock&data=' + Sanitize.url(JSON.stringify(args)) + '&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + if(callback !== undefined) { + // async + + http.addEventListener('load', function() { + callback(Block.parseBlockArray(JSON.parse(http.responseText)['hash'])); + }, false); + + http.open('GET', url, true); + http.timeout = 5000; + http.send(null); + } else { + // sync + + http.open('GET', url, false); + http.send(null); + + return Block.parseBlockArray(JSON.parse(http.responseText)['hash']); + } + } } /* block class */ @@ -348,6 +440,52 @@ class Block { return !(this.hash === null || this.hash === undefined); } + // saves the block, returns the hash + save(sign, callback) { + var type = this.getType(); + var content = this.getContent(); + var parent = this.getParent(); + + if(content !== undefined && content !== null && type !== '') { + var args = {'content' : content}; + + if(type !== undefined && type !== null && type !== '') + args['type'] = type; + if(parent !== undefined && parent !== null && parent.getHash() !== undefined && parent.getHash() !== null && parent.getHash() !== '') + args['parent'] = parent.getHash(); + if(sign !== undefined && sign !== null) + args['sign'] = String(sign) !== 'false' + + + var url = '/client/?action=insertBlock&data=' + Sanitize.url(JSON.stringify(args)) + '&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + if(callback !== undefined) { + // async + + http.addEventListener('load', function() { + callback(Block.parseBlockArray(JSON.parse(http.responseText)['hash'])); + }, false); + + http.open('GET', url, true); + http.timeout = 5000; + http.send(null); + } else { + // sync + + http.open('GET', url, false); + http.send(null); + + return Block.parseBlockArray(JSON.parse(http.responseText)['hash']); + } + } + + return false; + } + /* static functions */ // recreates a block by hash @@ -431,19 +569,55 @@ class Block { /* temporary code */ +var tt = getParameter("timingToken"); +if(tt !== null && tt !== undefined) { + setTimingToken(tt); +} + if(getWebPassword() === null) { var password = ""; while(password.length != 64) { password = prompt("Please enter the web password (run `./RUN-LINUX.sh --get-password`)"); } - setTimingToken(prompt("Please enter the timing token (optional)")); - setWebPassword(password); - window.location.reload(true); } -var tt = getParameter("timingToken"); -if(tt !== null && tt !== undefined) { - setTimingToken(tt); +if(getCurrentUser() === null) { + var url = '/client/?action=info&token=' + Sanitize.url(getWebPassword()) + '&timingToken=' + Sanitize.url(getTimingToken()); + + console.log(url); + + var http = new XMLHttpRequest(); + + // sync + + http.open('GET', url, false); + http.send(null); + + var id = JSON.parse(http.responseText)['pubkey']; + + User.getUser(id, function(data) { + if(data === null || data === undefined) { + jQuery('#modal').modal('show'); + + var user = new User(); + + user.setName('New User'); + user.setID(id); + user.setIcon('<$= Template.jsTemplate("default-icon") $>'); + user.setDescription('A new OnionrUI user'); + + user.remember(); + user.save(); + + setCurrentUser(user); + + window.location.reload(); + } else { + setCurrentUser(data); + } + }); } + +currentUser = getCurrentUser(); diff --git a/onionr/static-data/www/ui/src/js/timeline.js b/onionr/static-data/www/ui/src/js/timeline.js index e7a3c472..c411f41c 100644 --- a/onionr/static-data/www/ui/src/js/timeline.js +++ b/onionr/static-data/www/ui/src/js/timeline.js @@ -1,35 +1,3 @@ -function getUserInfo(id, callback) { - var user = deserializeUser(id); - if(user === null) { - Block.getBlocks({'type' : 'onionr-user-info', 'signed' : true, 'reverse' : true}, function(data) { - if(data.length !== 0) { - try { - user = new User(); - - var userInfo = JSON.parse(data[0].getContent()); - - if(userInfo['id'] === id) { - user.setName(userInfo['name']); - user.setIcon(userInfo['icon']); - user.setID(id); - - serializeUser(user); - - return callback(user); - } - } catch(e) { - console.log(e); - return callback(null); - } - } else { - return callback(null); - } - }); - } else { - return callback(user); - } -} - /* just for testing rn */ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, function(data) { for(var i = 0; i < data.length; i++) { @@ -37,7 +5,7 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun var block = data[i]; var finished = false; - getUserInfo(new String(block.getHeader('signer', 'unknown')), function(user) { + User.getUser(new String(block.getHeader('signer', 'unknown')), function(user) { var post = new Post(); var blockContent = JSON.parse(block.getContent()); @@ -53,6 +21,7 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun while(!finished); } catch(e) { + console.log('Troublemaker block: ' + data[i].getHash()); console.log(e); } } @@ -62,11 +31,101 @@ function viewProfile(id, name) { id = decodeURIComponent(id); document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(decodeURIComponent(name)); - getUserInfo(id, function(data) { + User.getUser(id, function(data) { if(data !== null) { document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(data.getName()); document.getElementById("onionr-profile-username").title = Sanitize.html(data.getID()); document.getElementById("onionr-profile-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(data.getIcon()); + document.getElementById("onionr-profile-user-icon").b64 = Sanitize.html(data.getIcon()); } }); } + +function updateUser() { + toggleSaveButton(false); + + jQuery('#modal').modal('show'); + + var name = jQuery('#onionr-profile-username').text(); + var id = document.getElementById("onionr-profile-username").title; + var icon = document.getElementById("onionr-profile-user-icon").b64; + var description = 'todo'; + + var user = new User(); + + user.setName(name); + user.setID(id); + user.setIcon(icon); + user.setDescription(description); + + user.remember(); + user.save(); + + window.location.reload(); +} + +function cancelUpdate() { + toggleSaveButton(false); + + var name = jQuery('#onionr-profile-username').text(); + var id = document.getElementById("onionr-profile-username").title; + + viewProfile(id, name); +} + +function toggleSaveButton(show) { + document.getElementById("onionr-profile-save").style.display = (show ? 'block' : 'none'); + document.getElementById("onionr-profile-cancel").style.display = (show ? 'block' : 'none'); +} + +function makePost() { + var content = document.getElementById("onionr-post-creator-content").value; + + if(content.trim() !== '') { + var post = new Post(); + + post.setUser(getCurrentUser()); + post.setContent(content); + post.setPostDate(new Date()); + + post.save(function(data) {}); // async, but no function + + document.getElementById('onionr-timeline-posts').innerHTML = post.getHTML() + document.getElementById('onionr-timeline-posts').innerHTML; + + document.getElementById("onionr-post-creator-content").value = ""; + } else { + console.log('Not making empty post.'); + } +} + +$('body').on('click', '[data-editable]', function() { + var el = jQuery(this); + var txt = el.text(); + + var input = jQuery('').val(txt); + el.replaceWith(input); + + var save = function() { + var newTxt = input.val(); + var p = el.text(Sanitize.username(newTxt)); + input.replaceWith(p); + + if(newTxt !== txt) + toggleSaveButton(true); + }; + + input.one('blur', save).focus(); +}); + +document.getElementById("onionr-post-creator-user-name").innerHTML = Sanitize.html(currentUser.getName()); +document.getElementById("onionr-post-creator-user-id").innerHTML = "<$= LANG.POST_CREATOR_YOU $>"; +document.getElementById("onionr-post-creator-user-icon").src = "data:image/jpeg;base64," + Sanitize.html(currentUser.getIcon()); +document.getElementById("onionr-post-creator-user-id").title = currentUser.getID(); +document.getElementById("onionr-post-creator-content").placeholder = "<$= LANG.POST_CREATOR_PLACEHOLDER $>"; + +viewCurrentProfile = function() { + viewProfile(encodeURIComponent(currentUser.getID()), encodeURIComponent(currentUser.getName())); +} + +document.getElementById("onionr-post-creator-user-id").onclick = viewCurrentProfile; +document.getElementById("onionr-post-creator-user-name").onclick = viewCurrentProfile;