diff --git a/onionr/api.py b/onionr/api.py index 3ccebff5..4780034f 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -249,6 +249,8 @@ class API: self.mimeType = 'text/html' response = siteData.split(b'-', 2)[-1] resp = Response(response) + elif action == 'info': + resp = new Response(json.dumps({'id' : 'not yet implemented'})) elif action == "insertBlock": response = {'success' : False, 'reason' : 'An unknown error occurred'} diff --git a/onionr/static-data/www/ui/dist/index.html b/onionr/static-data/www/ui/dist/index.html index 6efaac1b..02ee47b4 100644 --- a/onionr/static-data/www/ui/dist/index.html +++ b/onionr/static-data/www/ui/dist/index.html @@ -43,7 +43,7 @@
-

arinerron

+

arinerron

diff --git a/onionr/static-data/www/ui/dist/js/main.js b/onionr/static-data/www/ui/dist/js/main.js index 75cb7f81..6bbbdb27 100644 --- a/onionr/static-data/www/ui/dist/js/main.js +++ b/onionr/static-data/www/ui/dist/js/main.js @@ -17,6 +17,13 @@ function remove(key) { return localStorage.removeItem(key); } +function getParameter(name) { + var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); + return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); +} + +/* usermap localStorage stuff */ + var usermap = JSON.parse(get('usermap', '{}')); function getUserMap() { @@ -24,11 +31,29 @@ function getUserMap() { } function deserializeUser(id) { + if(!(id in getUserMap())) + return null; + var serialized = getUserMap()[id] var user = new User(); + user.setName(serialized['name']); user.setID(serialized['id']); user.setIcon(serialized['icon']); + + return user; +} + +function serializeUser(user) { + if(user !== null && user !== undefined) { + var serialized = {'name' : user.getName(), 'id' : user.getID(), 'icon' : user.getIcon()}; + + usermap[user.getID()] = serialized; + + set('usermap', JSON.stringify(getUserMap())); + + return serialized; + } } /* returns a relative date format, e.g. "5 minutes" */ @@ -219,7 +244,7 @@ class Post { // postTemplate = postTemplate.replaceAll('$user-id-truncated', Sanitize.html(this.getUser().getID().split('-').slice(0, 4).join('-'))); postTemplate = postTemplate.replaceAll('$user-id', Sanitize.html(this.getUser().getID())); - postTemplate = postTemplate.replaceAll('$user-image', Sanitize.html(this.getUser().getIcon())); + postTemplate = postTemplate.replaceAll('$user-image', "data:image/jpeg;base64," + Sanitize.html(this.getUser().getIcon())); postTemplate = postTemplate.replaceAll('$content', Sanitize.html(this.getContent())); postTemplate = postTemplate.replaceAll('$date-relative', timeSince(this.getPostDate(), device) + (device === 'desktop' ? ' ago' : '')); postTemplate = postTemplate.replaceAll('$date', this.getPostDate().toLocaleString()); @@ -449,3 +474,8 @@ if(getWebPassword() === null) { setWebPassword(password); window.location.reload(true); } + +var tt = getParameter("timingToken"); +if(tt !== null && tt !== undefined) { + setTimingToken(tt); +} diff --git a/onionr/static-data/www/ui/dist/js/timeline.js b/onionr/static-data/www/ui/dist/js/timeline.js index 0dbd634a..e7a3c472 100644 --- a/onionr/static-data/www/ui/dist/js/timeline.js +++ b/onionr/static-data/www/ui/dist/js/timeline.js @@ -1,21 +1,57 @@ +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++) { try { var block = data[i]; - var post = new Post(); - var user = new User(); + var finished = false; + getUserInfo(new String(block.getHeader('signer', 'unknown')), function(user) { + var post = new Post(); - var blockContent = JSON.parse(block.getContent()); + var blockContent = JSON.parse(block.getContent()); - user.setName('unknown'); - user.setID(new String(block.getHeader('signer', 'unknown'))); - post.setContent(blockContent['content']); - post.setPostDate(block.getDate()); - post.setUser(user); + post.setContent(blockContent['content']); + post.setPostDate(block.getDate()); + post.setUser(user); - document.getElementById('onionr-timeline-posts').innerHTML += post.getHTML(); + document.getElementById('onionr-timeline-posts').innerHTML += post.getHTML(); + + finished = true; + }); + + while(!finished); } catch(e) { console.log(e); } @@ -23,5 +59,14 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun }); function viewProfile(id, name) { + id = decodeURIComponent(id); document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(decodeURIComponent(name)); + + getUserInfo(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()); + } + }); } diff --git a/onionr/static-data/www/ui/src/index.html b/onionr/static-data/www/ui/src/index.html index e5791cb9..f34e8605 100644 --- a/onionr/static-data/www/ui/src/index.html +++ b/onionr/static-data/www/ui/src/index.html @@ -13,7 +13,7 @@
-

arinerron

+

arinerron

diff --git a/onionr/static-data/www/ui/src/js/main.js b/onionr/static-data/www/ui/src/js/main.js index a99c187b..50738052 100644 --- a/onionr/static-data/www/ui/src/js/main.js +++ b/onionr/static-data/www/ui/src/js/main.js @@ -17,6 +17,13 @@ function remove(key) { return localStorage.removeItem(key); } +function getParameter(name) { + var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search); + return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); +} + +/* usermap localStorage stuff */ + var usermap = JSON.parse(get('usermap', '{}')); function getUserMap() { @@ -24,11 +31,29 @@ function getUserMap() { } function deserializeUser(id) { + if(!(id in getUserMap())) + return null; + var serialized = getUserMap()[id] var user = new User(); + user.setName(serialized['name']); user.setID(serialized['id']); user.setIcon(serialized['icon']); + + return user; +} + +function serializeUser(user) { + if(user !== null && user !== undefined) { + var serialized = {'name' : user.getName(), 'id' : user.getID(), 'icon' : user.getIcon()}; + + usermap[user.getID()] = serialized; + + set('usermap', JSON.stringify(getUserMap())); + + return serialized; + } } /* returns a relative date format, e.g. "5 minutes" */ @@ -187,7 +212,7 @@ class Post { // postTemplate = postTemplate.replaceAll('$user-id-truncated', Sanitize.html(this.getUser().getID().split('-').slice(0, 4).join('-'))); postTemplate = postTemplate.replaceAll('$user-id', Sanitize.html(this.getUser().getID())); - postTemplate = postTemplate.replaceAll('$user-image', Sanitize.html(this.getUser().getIcon())); + postTemplate = postTemplate.replaceAll('$user-image', "data:image/jpeg;base64," + Sanitize.html(this.getUser().getIcon())); postTemplate = postTemplate.replaceAll('$content', Sanitize.html(this.getContent())); postTemplate = postTemplate.replaceAll('$date-relative', timeSince(this.getPostDate(), device) + (device === 'desktop' ? ' ago' : '')); postTemplate = postTemplate.replaceAll('$date', this.getPostDate().toLocaleString()); @@ -417,3 +442,8 @@ if(getWebPassword() === null) { setWebPassword(password); window.location.reload(true); } + +var tt = getParameter("timingToken"); +if(tt !== null && tt !== undefined) { + setTimingToken(tt); +} diff --git a/onionr/static-data/www/ui/src/js/timeline.js b/onionr/static-data/www/ui/src/js/timeline.js index 763fc7d0..e7a3c472 100644 --- a/onionr/static-data/www/ui/src/js/timeline.js +++ b/onionr/static-data/www/ui/src/js/timeline.js @@ -1,3 +1,34 @@ +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) { @@ -5,18 +36,22 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun try { var block = data[i]; - var post = new Post(); - var user = new User(); + var finished = false; + getUserInfo(new String(block.getHeader('signer', 'unknown')), function(user) { + var post = new Post(); - var blockContent = JSON.parse(block.getContent()); + var blockContent = JSON.parse(block.getContent()); - user.setName('unknown'); - user.setID(new String(block.getHeader('signer', 'unknown'))); - post.setContent(blockContent['content']); - post.setPostDate(block.getDate()); - post.setUser(user); + post.setContent(blockContent['content']); + post.setPostDate(block.getDate()); + post.setUser(user); - document.getElementById('onionr-timeline-posts').innerHTML += post.getHTML(); + document.getElementById('onionr-timeline-posts').innerHTML += post.getHTML(); + + finished = true; + }); + + while(!finished); } catch(e) { console.log(e); } @@ -24,5 +59,14 @@ Block.getBlocks({'type' : 'onionr-post', 'signed' : true, 'reverse' : true}, fun }); function viewProfile(id, name) { + id = decodeURIComponent(id); document.getElementById("onionr-profile-username").innerHTML = Sanitize.html(decodeURIComponent(name)); + + getUserInfo(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()); + } + }); }