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

239 lines
5.4 KiB
JavaScript
Raw Permalink 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;
}
2017-11-02 17:37:31 +05:00
function get_mod_board_target() {
2017-11-02 17:39:59 +05:00
var e = document.getElementById("nntpchan_mod_group_target");
2017-11-02 17:37:31 +05:00
return e.value || null;
}
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() {
2017-11-02 17:37:31 +05:00
var args = {
2015-10-02 20:00:26 +05:00
pubkey: get_key_target()
2017-11-02 17:37:31 +05:00
};
if(board) {
args.newsgroup = board;
}
nntpchan_admin("pubkey.del", args);
2015-10-02 20:00:26 +05:00
}
function nntpchan_key_add() {
2017-11-02 17:37:31 +05:00
var args = {
2015-10-02 20:00:26 +05:00
pubkey: get_key_target()
2017-11-02 17:37:31 +05:00
};
var board = get_mod_board_target();
if(board) {
args.newsgroup = board;
}
nntpchan_admin("pubkey.add", args);
2015-10-02 20:00:26 +05:00
}
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");
2017-11-02 17:37:31 +05:00
return e.value;
2016-01-12 20:23:36 +05:00
}
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()
})
}
function nntpchan_admin(method, param, handler_cb, result_elem) {
2016-03-07 21:42:21 +05:00
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
}, result_elem)
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;
}
});
}
function nntpchan_mod(mod_action, result_elem) {
2016-04-13 04:39:56 +05:00
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");
var target = null;
if (input) {
target = input.value;
}
2015-08-07 21:36:59 +05:00
if (mod_action.parser) {
target = mod_action.parser(target);
}
var elem;
if (result_elem) {
elem = result_elem;
} else {
elem = document.getElementById("nntpchan_mod_result");
}
2015-08-04 19:03:44 +05:00
// clear old results
while( elem.firstChild ) {
elem.removeChild(elem.firstChild);
}
2015-08-07 19:59:57 +05:00
2016-04-13 04:39:56 +05:00
var csrf_ajax = new XMLHttpRequest();
csrf_ajax.onreadystatechange = function() {
if (csrf_ajax.readyState == XMLHttpRequest.DONE) {
// get csrf token
var csrf = csrf_ajax.getResponseHeader("X-CSRF-Token");
// 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 {
if (mod_action.handle) {
var result = mod_action.handle(j);
if (result) {
elem.appendChild(result);
}
}
2015-08-04 19:03:44 +05:00
}
2016-04-13 04:39:56 +05:00
} else if (status) {
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
}
// clear input
if (input) {
input.value = "";
2015-08-04 19:03:44 +05:00
}
}
}
2016-04-13 04:39:56 +05:00
if (mod_action.name) {
2016-04-13 04:41:38 +05:00
2016-04-13 04:39:56 +05:00
var url = mod_action.name + "/" + target;
ajax.open(mod_action.method || "GET", url);
2016-04-13 04:41:38 +05:00
ajax.setRequestHeader("X-CSRF-Token", csrf);
2016-04-13 04:39:56 +05:00
var data = mod_action.data;
if (data) {
ajax.setRequestHeader("Content-type","text/json");
ajax.send(JSON.stringify(data));
} else {
ajax.send();
}
} else {
alert("mod action has no name");
2016-03-25 19:01:33 +05:00
}
2015-08-04 19:03:44 +05:00
}
2015-08-07 19:59:57 +05:00
}
2016-04-13 04:40:55 +05:00
csrf_ajax.open("GET", "");
2016-04-13 04:39:56 +05:00
csrf_ajax.send();
2015-08-04 19:03:44 +05:00
}