Onionr/static-data/www/friends/friends.js

96 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

2019-02-21 20:25:45 +00:00
/*
Onionr - Private P2P Communication
2019-02-21 20:25:45 +00:00
This file handles the UI for managing friends/contacts
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
friendListDisplay = document.getElementById('friendList')
2019-02-22 21:04:03 +00:00
addForm = document.getElementById('addFriend')
function removeFriend(pubkey){
post_to_url('/friends/remove/' + pubkey, {'token': webpass})
}
2019-02-22 21:04:03 +00:00
addForm.onsubmit = function(){
var friend = document.getElementsByName('addKey')[0]
var alias = document.getElementsByName('data')[0]
2019-09-04 06:20:11 +00:00
if (alias.value.toLowerCase().trim() == 'anonymous'){
PNotify.error({
text: "Anonymous is a reserved alias name"
})
return false
}
2019-02-22 21:04:03 +00:00
fetch('/friends/add/' + friend.value, {
method: 'POST',
headers: {
"token": webpass
}}).then(function(data) {
if (alias.value.trim().length > 0){
2019-03-04 19:03:35 +00:00
post_to_url('/friends/setinfo/' + friend.value + '/name', {'data': alias.value, 'token': webpass})
2019-02-22 21:04:03 +00:00
}
})
return false
}
2019-02-21 20:25:45 +00:00
2019-02-22 01:55:13 +00:00
fetch('/friends/list', {
headers: {
"token": webpass
}})
.then((resp) => resp.json()) // Transform the data into json
.then(function(resp) {
var keys = [];
for(var k in resp) keys.push(k);
console.log(keys)
if (keys.length == 0){
friendListDisplay.innerText = "None yet :("
}
2019-02-22 01:55:13 +00:00
for (var i = 0; i < keys.length; i++){
var peer = keys[i]
var name = resp[keys[i]]['name']
if (name === null || name === ''){
2019-02-22 21:04:03 +00:00
name = peer
2019-02-22 01:55:13 +00:00
}
var entry = document.createElement('div')
2019-02-22 21:04:03 +00:00
var nameText = document.createElement('input')
removeButton = document.createElement('button')
removeButton.classList.add('friendRemove')
2019-07-14 00:29:33 +00:00
removeButton.classList.add('button', 'is-danger')
2019-02-22 21:04:03 +00:00
entry.setAttribute('data-pubkey', peer)
removeButton.innerText = 'X'
nameText.value = name
nameText.readOnly = true
nameText.style.fontStyle = "italic"
2019-02-22 01:55:13 +00:00
entry.style.paddingTop = '8px'
2019-02-22 21:04:03 +00:00
entry.appendChild(removeButton)
entry.appendChild(nameText)
2019-02-22 01:55:13 +00:00
friendListDisplay.appendChild(entry)
}
// If friend delete buttons are pressed
var friendRemoveBtns = document.getElementsByClassName('friendRemove')
for (var x = 0; x < friendRemoveBtns.length; x++){
var friendKey = friendRemoveBtns[x].parentElement.getAttribute('data-pubkey')
friendRemoveBtns[x].onclick = function(){
removeFriend(friendKey)
}
}
2019-07-13 00:03:06 +00:00
})