onionr/static-data/www/board/board.js

219 lines
6.7 KiB
JavaScript
Raw Normal View History

/*
Onionr - Private P2P Communication
This file handles the boards/circles interface
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/>.
*/
2019-02-02 03:49:11 +00:00
requested = []
var windowHeight = window.innerHeight;
2019-04-16 05:02:09 +00:00
webpassword = webpass
newPostForm = document.getElementById('addMsg')
firstLoad = true
2019-07-29 03:00:38 +00:00
lastLoadedBoard = 'global'
2019-09-06 05:18:25 +00:00
loadingMessage = document.getElementById('loadingBoard')
let toggleLoadingMessage = function(){
switch (loadingMessage.style.display){
case "block":
case "inline":
case "inline-block":
loadingMessage.style.display = "none"
break;
default:
loadingMessage.style.display = "initial"
break;
}
}
2019-07-29 03:00:38 +00:00
2019-09-06 05:18:25 +00:00
fetch('/flow/version', {
method: 'GET',
headers: {
"token": webpass
}})
.then((resp) => resp.text()) // Transform the data into json
.then(function(data) {
document.getElementById('circlesVersion').innerText = data
})
function appendMessages(msg, blockHash, beforeHash){
var humanDate = new Date(0)
if (msg.length == 0){
return
}
2019-09-06 05:18:25 +00:00
//var msg = JSON.parse(msg)
var el = document.createElement('div')
var msgDate = msg['meta']['time']
2019-09-06 05:18:25 +00:00
var feed = document.getElementById("feed")
var beforeEl = null
if (msgDate === undefined){
msgDate = 'unknown'
2019-02-02 03:49:11 +00:00
}
else{
humanDate.setUTCSeconds(msgDate)
msgDate = humanDate.toLocaleTimeString() + ' ' + humanDate.toLocaleDateString()
2019-02-02 03:49:11 +00:00
}
el.className = 'entry'
el.innerText = msg['content']
2019-09-06 05:18:25 +00:00
if (beforeHash !== null){
for (x = 0; x < feed.children.length; x++){
if (feed.children[x].getAttribute('data-bl') === beforeHash){
beforeEl = feed.children[x]
}
}
}
/* Template Test */
// Test to see if the browser supports the HTML template element by checking
// for the presence of the template element's content attribute.
if ('content' in document.createElement('template')) {
// Instantiate the table with the existing HTML tbody
// and the row with the template
var template = document.getElementById('cMsgTemplate')
// Clone the new row and insert it into the table
2019-09-06 05:18:25 +00:00
var clone = document.importNode(template.content, true)
var div = clone.querySelectorAll("div")
2019-09-21 05:06:49 +00:00
var identicon = clone.querySelectorAll("img")
2019-09-06 05:18:25 +00:00
div[0].setAttribute('data-bl', blockHash)
div[2].textContent = msg['content']
if (typeof msg['meta']['signer'] != 'undefined' && msg['meta']['signer'].length > 0){
div[3].textContent = msg['meta']['signer'].substr(0, 5)
setHumanReadableIDOnPost(div[3], msg['meta']['signer'])
div[3].title = msg['meta']['signer']
2019-09-21 05:06:49 +00:00
identicon[0].src = "data:image/svg+xml;base64," + userIcon(msg['meta']['signer'])
}
else{
identicon[0].remove()
}
div[4].textContent = msgDate
2019-09-06 05:18:25 +00:00
loadingMessage.style.display = "none"
if (firstLoad){
2019-09-06 05:18:25 +00:00
//feed.appendChild(clone)
feed.prepend(clone)
firstLoad = false
}
else{
2019-09-06 05:18:25 +00:00
if (beforeEl === null){
feed.prepend(clone)
}
else{
beforeEl.insertAdjacentElement("beforebegin", clone.children[0])
2019-09-06 05:18:25 +00:00
}
}
}
2019-02-02 03:49:11 +00:00
}
function getBlocks(){
2019-07-29 03:00:38 +00:00
var feed = document.getElementById("feed")
var ch = document.getElementById('feedIDInput').value
if (lastLoadedBoard !== ch){
2019-09-06 05:18:25 +00:00
toggleLoadingMessage()
2019-07-29 03:00:38 +00:00
while (feed.firstChild) {
feed.removeChild(feed.firstChild);
}
requested = [] // reset requested list
}
lastLoadedBoard = ch
2019-02-02 03:49:11 +00:00
if (document.getElementById('none') !== null){
document.getElementById('none').remove();
}
2019-09-06 05:18:25 +00:00
2019-07-29 03:00:38 +00:00
var feedText = httpGet('/flow/getpostsbyboard/' + ch)
2019-09-06 05:18:25 +00:00
var blockList = feedText.split(',')
2019-09-04 06:20:11 +00:00
2019-02-02 03:49:11 +00:00
for (i = 0; i < blockList.length; i++){
2019-09-06 05:18:25 +00:00
while (blockList[i].length < 64) { blockList[i] = "0" + blockList[i] }
2019-02-02 03:49:11 +00:00
if (! requested.includes(blockList[i])){
2019-07-29 03:00:38 +00:00
if (blockList[i].length == 0){
continue
}
2019-02-02 03:49:11 +00:00
requested.push(blockList[i])
2019-09-06 05:18:25 +00:00
loadMessage(blockList[i], blockList, i)
}
}
2019-02-02 03:49:11 +00:00
}
2019-09-06 05:18:25 +00:00
function loadMessage(blockHash, blockList, count){
fetch('/getblockdata/' + blockHash, {
method: 'GET',
headers: {
"token": webpass
}})
.then((resp) => resp.json())
.then(function(data) {
let before = blockList[count - 1]
let delay = 2000
if (typeof before == "undefined"){
before = null
}
else{
let existing = document.getElementsByClassName('cMsgBox')
for (x = 0; x < existing.length; x++){
if (existing[x].getAttribute('data-bl') === before){
delay = 0
}
}
}
setTimeout(function(){appendMessages(data, blockHash, before)}, delay)
//appendMessages(data, blockHash, before)
})
}
2019-02-02 03:49:11 +00:00
document.getElementById('refreshFeed').onclick = function(){
getBlocks()
}
newPostForm.onsubmit = function(){
var message = document.getElementById('newMsgText').value
2019-07-29 03:00:38 +00:00
var channel = document.getElementById('feedIDInput').value
var meta = {'ch': channel}
let doSign = document.getElementById('postAnon').checked
var postData = {'message': message, 'sign': doSign, 'type': 'brd', 'encrypt': false, 'meta': JSON.stringify(meta)}
postData = JSON.stringify(postData)
newPostForm.style.display = 'none'
fetch('/insertblock', {
method: 'POST',
body: postData,
headers: {
"content-type": "application/json",
"token": webpass
}})
.then((resp) => resp.text()) // Transform the data into json
.then(function(data) {
newPostForm.style.display = 'block'
if (data == 'failure due to duplicate insert'){
PNotify.error({
text: "This message is already queued"
})
return
}
PNotify.success({
text: "Message queued for posting"
})
2019-09-06 05:18:25 +00:00
setTimeout(function(){getBlocks()}, 500)
})
return false
2019-02-02 03:49:11 +00:00
}