Archived
1
0
This repository has been archived on 2023-08-12. You can view files and clone it, but cannot push or open issues or pull requests.
nntpchan/contrib/static/mod.js

281 lines
6.9 KiB
JavaScript
Raw Normal View History

2015-08-04 19:03:44 +05:00
/*
* mod.js, moderator page js stuff
*/
// TODO: implement mod panel all the way
document.onload = function(ev) {
// populate the mod page with stuff
}
function get_longhash(str) {
var idx = str.indexOf("#") + 1;
if ( idx > 0 ) {
str = str.substr(idx);
}
console.log(str);
return str;
}
2015-08-07 19:59:57 +05:00
// handle ban command
function nntpchan_ban() {
nntpchan_mod({
2015-08-07 21:36:59 +05:00
parser: get_longhash,
2015-08-07 19:59:57 +05:00
name: "ban",
handle: function(j) {
if (j.banned) {
return document.createTextNode(j.banned);
}
}
});
}
2015-08-07 21:36:59 +05:00
function nntpchan_unban() {
nntpchan_mod({
name: "unban",
handle: function(j) {
if (j.result) {
return document.createTextNode(j.result);
}
}
})
}
2015-08-07 19:59:57 +05:00
2015-09-23 17:54:49 +05:00
function get_board_target() {
var e = document.getElementById("nntpchan_board_target");
return e.value;
}
2015-10-02 20:00:26 +05:00
function get_key_target() {
var e = document.getElementById("nntpchan_key_target");
return e.value;
}
function nntpchan_key_del() {
nntpchan_admin("pubkey.del", {
pubkey: get_key_target()
});
}
function nntpchan_key_add() {
nntpchan_admin("pubkey.add", {
pubkey: get_key_target()
});
}
2016-01-12 20:23:36 +05:00
function get_nntp_username() {
var e = document.getElementById("nntpchan_nntp_username");
return e.value;
}
function get_nntp_passwd() {
var e = document.getElementById("nntpchan_nntp_passwd");
return e.value;
}
function nntpchan_admin_nntp(method) {
nntpchan_admin(method, {
username: get_nntp_username(),
passwd: get_nntp_passwd()
})
}
2015-09-23 17:54:49 +05:00
function nntpchan_admin_board(method) {
nntpchan_admin(method, {
newsgroup: get_board_target()
})
}
2016-03-07 21:42:21 +05:00
function nntpchan_admin(method, param, handler_cb) {
if (handler_cb) {
// we got a handler already set
} else {
// no handler set
var handler_cb = function(j) {
2015-09-19 18:02:08 +05:00
if (j.result) {
return document.createTextNode(j.result);
2015-09-19 17:54:54 +05:00
} else {
return "nothing happened?";
}
2016-03-07 21:42:21 +05:00
}
}
nntpchan_mod({
name:"admin",
parser: function(target) {
return method;
2015-09-23 17:59:56 +05:00
},
2016-03-07 21:42:21 +05:00
handle: handler_cb,
2015-09-23 18:04:37 +05:00
method: ( param && "POST" ) || "GET",
data: param
2015-09-19 17:54:54 +05:00
})
}
2015-08-04 19:03:44 +05:00
// handle delete command
function nntpchan_delete() {
2015-08-07 19:59:57 +05:00
nntpchan_mod({
2015-08-07 21:36:59 +05:00
parser: get_longhash,
2015-08-07 19:59:57 +05:00
name: "del",
handle: function(j) {
var elem = document.createElement("div");
if (j.deleted) {
for ( var idx = 0 ; idx < j.deleted.length ; idx ++ ) {
var msg = "deleted: " + j.deleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
if (j.notdeleted) {
for ( var idx = 0 ; idx < j.notdeleted.length ; idx ++ ) {
var msg = "not deleted: " + j.notdeleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
return elem;
}
});
}
2016-03-12 19:44:51 +05:00
function createConnectionElement(j) {
var e = document.createElement("div");
e.setAttribute("class", "connection");
2016-03-12 20:16:45 +05:00
var auth = document.createElement("div");
2016-03-12 21:24:04 +05:00
auth.appendChild(document.createTextNode("Connection: "+j.name));
2016-03-12 20:13:17 +05:00
// authentication state
if (j.authed) {
auth.setAttribute("class", "authed");
2016-03-12 21:24:04 +05:00
auth.appendChild(document.createTextNode("(authenticated)"));
2016-03-12 20:13:17 +05:00
} else {
2016-03-12 21:24:04 +05:00
auth.appendChild(document.createTextNode("(not authenticated)"));
2016-03-12 20:13:17 +05:00
}
e.appendChild(auth);
// connection mode
2016-03-12 20:16:45 +05:00
var mode = document.createElement("div");
2016-03-12 20:15:56 +05:00
mode.setAttribute("class", "mode");
mode.appendChild(document.createTextNode("mode: "+j.mode));
2016-03-12 20:13:17 +05:00
e.appendChild(mode);
var pending = document.createElement("div");
pending.setAttribute("class", "pending");
// pending articles
var articles = Object.keys(j.pending);
2016-03-12 20:15:56 +05:00
pending.appendChild(document.createTextNode("pending articles: "+articles.length));
2016-03-12 20:13:17 +05:00
for ( var idx = 0 ; idx < articles.length; idx ++ ) {
var msgid = articles[idx];
var state = j.pending[msgid];
var elem = document.createElement("div");
elem.appendChild(document.createTextNode(msgid + ": " + state));
elem.setAttribute("class", "pending_item "+state);
pending.appendChild(elem);
}
e.appendChild(pending);
// e.appendChild(document.createTextNode(JSON.stringify(j)));
2016-03-12 19:44:51 +05:00
return e;
}
2016-03-07 21:59:33 +05:00
function inject_nntp_feed_element(feed, elem) {
2016-03-12 20:18:43 +05:00
elem.appendChild(document.createElement("hr"));
var name = document.createElement("div");
2016-03-08 17:25:54 +05:00
name.setAttribute("class", "feeds_name");
2016-03-07 22:05:52 +05:00
name_elem = document.createTextNode("Name: "+feed.State.Config.Name);
2016-03-07 21:59:33 +05:00
name.appendChild(name_elem);
2016-03-07 22:03:27 +05:00
elem.appendChild(name);
var conns = document.createElement("div");
2016-03-12 19:44:51 +05:00
conns.setAttribute("class", "connections");
for ( var idx = 0 ; idx < feed.Conns.length; idx ++ ) {
conns.appendChild(createConnectionElement(feed.Conns[idx]));
}
2016-03-07 21:59:33 +05:00
elem.appendChild(conns);
}
2016-03-07 21:42:21 +05:00
function update_nntpchan_feed_ticker(elem) {
nntpchan_admin("feed.list", null, function(j) {
if (j) {
if (j.error) {
console.log("nntpchan_feed_ticker: error, "+j.error);
} else {
// remove all children
while(elem.children.length) {
elem.children[0].remove();
}
var result = j.result;
for (var idx = 0; idx < result.length; idx++) {
var item = result[idx];
var entry = document.createElement("div");
2016-03-07 21:59:33 +05:00
inject_nntp_feed_element(item, entry);
2016-03-07 21:42:21 +05:00
elem.appendChild(entry);
}
}
}
});
}
2015-08-07 19:59:57 +05:00
function nntpchan_mod(mod_action) {
2015-08-04 19:03:44 +05:00
// get the element
2015-08-07 19:59:57 +05:00
var input = document.getElementById("nntpchan_mod_target");
2015-08-07 21:36:59 +05:00
var target = input.value;
if (mod_action.parser) {
target = mod_action.parser(target);
}
2015-08-04 19:03:44 +05:00
var elem = document.getElementById("nntpchan_mod_result");
// clear old results
while( elem.firstChild ) {
elem.removeChild(elem.firstChild);
}
2015-08-07 19:59:57 +05:00
2015-08-04 19:03:44 +05:00
// fire off ajax
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if (ajax.readyState == XMLHttpRequest.DONE) {
var status = ajax.status;
// we gud?
if (status == 200) {
// yah
var txt = ajax.responseText;
var j = JSON.parse(txt);
if (j.error) {
var e = document.createTextNode(j.error);
elem.appendChild(e);
} else {
2015-08-07 19:59:57 +05:00
if (mod_action.handle) {
var result = mod_action.handle(j);
if (result) {
elem.appendChild(result);
2015-08-04 19:03:44 +05:00
}
}
}
2015-08-07 20:11:24 +05:00
} else if (status) {
2015-08-07 20:06:04 +05:00
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
2015-08-04 19:03:44 +05:00
}
2015-08-07 20:06:04 +05:00
// clear input
input.value = "";
2015-08-04 19:03:44 +05:00
}
2015-08-07 19:59:57 +05:00
}
if (mod_action.name) {
2015-08-07 21:36:59 +05:00
var url = mod_action.name + "/" + target;
2015-09-19 17:54:54 +05:00
ajax.open(mod_action.method || "GET", url);
2015-09-23 18:04:37 +05:00
var data = mod_action.data;
if (data) {
2015-09-23 18:06:44 +05:00
ajax.setRequestHeader("Content-type","text/json");
2015-09-23 18:07:53 +05:00
ajax.send(JSON.stringify(data));
2015-09-23 18:04:37 +05:00
} else {
ajax.send();
}
2015-08-07 19:59:57 +05:00
} else {
alert("mod action has no name");
2015-08-04 19:03:44 +05:00
}
}