work on UI friends manager

This commit is contained in:
Kevin Froman 2019-02-22 15:04:03 -06:00
parent 30a2ae8d06
commit c61c833658
9 changed files with 100 additions and 40 deletions

View File

@ -290,9 +290,11 @@ class API:
return
try:
if not hmac.compare_digest(request.headers['token'], self.clientToken):
abort(403)
if not hmac.compare_digest(request.form['token'], self.clientToken):
abort(403)
except KeyError:
abort(403)
if not hmac.compare_digest(request.form['token'], self.clientToken):
abort(403)
@app.after_request
def afterReq(resp):
@ -417,14 +419,16 @@ class API:
if self._core._utils.validateHash(bHash):
try:
resp = Block(bHash).bcontent
except onionrexceptions.NoDataAvailable:
abort(404)
except TypeError:
pass
try:
resp = base64.b64decode(resp)
except:
pass
if resp == 'Not Found':
abourt(404)
if resp == 'Not Found' or not resp:
abort(404)
return Response(resp)
@app.route('/waitforshare/<name>', methods=['post'])

View File

@ -19,7 +19,7 @@
'''
import core, json
from onionrusers import contactmanager
from flask import Blueprint, Response, request, abort
from flask import Blueprint, Response, request, abort, redirect
friends = Blueprint('friends', __name__)
@ -45,7 +45,7 @@ def remove_friend(pubkey):
def set_info(pubkey, key):
data = request.form['data']
contactmanager.ContactManager(core.Core(), pubkey).set_info(key, data)
return 'success'
return redirect(request.referrer + '#' + request.form['token'])
@friends.route('/friends/getinfo/<pubkey>/<key>')
def get_info(pubkey, key):

View File

@ -139,32 +139,10 @@ class Block:
# import from file
if blockdata is None:
blockdata = onionrstorage.getData(self.core, self.getHash()).decode()
'''
filelocation = file
readfile = True
if filelocation is None:
if self.getHash() is None:
return False
elif self.getHash() in Block.getCache():
# get the block from cache, if it's in it
blockdata = Block.getCache(self.getHash())
readfile = False
# read from file if it's still None
if blockdata is None:
filelocation = self.core.dataDir + 'blocks/%s.dat' % self.getHash()
if readfile:
try:
blockdata = onionrstorage.getData(self.core, self.getHash()).decode()
#with open(filelocation, 'rb') as f:
#blockdata = f.read().decode()
self.blockFile = filelocation
'''
except AttributeError:
raise onionrexceptions.NoDataAvailable('Block does not exist')
else:
self.blockFile = None
# parse block
@ -200,11 +178,11 @@ class Block:
return True
except Exception as e:
logger.error('Failed to parse block %s.' % self.getHash(), error = e, timestamp = False)
logger.warn('Failed to parse block %s.' % self.getHash(), error = e, timestamp = False)
# if block can't be parsed, it's a waste of precious space. Throw it away.
if not self.delete():
logger.error('Failed to delete invalid block %s.' % self.getHash(), error = e)
logger.warn('Failed to delete invalid block %s.' % self.getHash(), error = e)
else:
logger.debug('Deleted invalid block %s.' % self.getHash(), timestamp = False)

View File

@ -89,7 +89,8 @@ class DaemonTools:
'''Check if we are connected to the internet or not when we can't connect to any peers'''
if len(self.daemon.onlinePeers) == 0:
if not netutils.checkNetwork(self.daemon._core._utils, torPort=self.daemon.proxyPort):
logger.warn('Network check failed, are you connected to the internet?')
if not self.daemon.shutdown:
logger.warn('Network check failed, are you connected to the internet?')
self.daemon.isOnline = False
else:
self.daemon.isOnline = True

View File

@ -18,6 +18,24 @@
*/
friendListDisplay = document.getElementById('friendList')
addForm = document.getElementById('addFriend')
addForm.onsubmit = function(){
var friend = document.getElementsByName('addKey')[0]
var alias = document.getElementsByName('data')[0]
fetch('/friends/add/' + friend.value, {
method: 'POST',
headers: {
"token": webpass
}}).then(function(data) {
if (alias.value.trim().length > 0){
post_to_url('/friends/setinfo/' + friend.value + '/name', {'data': alias.value, 'token': webpass})
}
})
return false
}
fetch('/friends/list', {
headers: {
@ -28,16 +46,26 @@ fetch('/friends/list', {
var keys = [];
for(var k in resp) keys.push(k);
console.log(keys)
friendListDisplay.innerHTML = 'Click name to view info<br><br>'
for (var i = 0; i < keys.length; i++){
friendListDisplay.innerText = ''
var peer = keys[i]
var name = resp[keys[i]]['name']
if (name === null || name === ''){
name = 'Anonymous'
name = peer
}
var entry = document.createElement('div')
var nameText = document.createElement('input')
removeButton = document.createElement('button')
removeButton.classList.add('friendRemove')
removeButton.classList.add('dangerBtn')
entry.setAttribute('data-pubkey', peer)
removeButton.innerText = 'X'
nameText.value = name
nameText.readOnly = true
nameText.style.fontStyle = "italic"
entry.style.paddingTop = '8px'
entry.innerText = name + ' - ' + peer
entry.appendChild(removeButton)
entry.appendChild(nameText)
friendListDisplay.appendChild(entry)
}
})

View File

@ -16,8 +16,8 @@
<h2>Friend Manager</h2>
<form id='addFriend' action='/' method='POST'>
<label>Friend ID: <input type='text' name='addKey' placeholder='public key/ID' required></label>
<label>Set Alias: <input type='text' name='addAlias' placeholder='what to call them' required></label>
<input type='submit' value='Add Friend'>
<label>Set Alias: <input type='text' name='data' placeholder='what to call them'></label>
<input type='submit' value='Add Friend' class='successBtn'>
</form>
<h2>Friend List:</h2>
<div id='friendList'>None Yet :(</div>

View File

@ -12,4 +12,15 @@ form label{
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
}
#friendList{
display: inline;
}
#friendList span{
text-align: center;
}
#friendList button{
display: inline;
margin-right: 10px;
}

View File

@ -150,4 +150,22 @@ body{
.closeOverlay:after{
content: '❌';
padding: 5px;
}
}
.btn, .warnBtn, .dangerBtn, .successBtn{
padding: 5px;
border-radius: 5px;
border: 2px solid black;
}
.warnBtn{
background-color: orange;
color: black;
}
.dangerBtn{
background-color: #f44336;
color: black;
}
.successBtn{
background-color: #4CAF50;
color: black;
}

View File

@ -20,6 +20,25 @@
webpass = document.location.hash.replace('#', '')
nowebpass = false
function post_to_url(path, params) {
var form = document.createElement("form")
form.setAttribute("method", "POST")
form.setAttribute("action", path)
for(var key in params) {
var hiddenField = document.createElement("input")
hiddenField.setAttribute("type", "hidden")
hiddenField.setAttribute("name", key)
hiddenField.setAttribute("value", params[key])
form.appendChild(hiddenField)
}
document.body.appendChild(form)
form.submit()
}
if (typeof webpass == "undefined"){
webpass = localStorage['webpass']
}
@ -67,3 +86,4 @@ for(var i = 0; i < refreshLinks.length; i++) {
location.reload()
}
}