function addMod(userEmail,listEmail){
$.post('/api/list/add_mod',{list:listEmail,email:userEmail},reload,'json');
}
function addPermission(userEmail,permission){
if (confirm("Really give permission to "+userEmail+"?")){
$.post('/api/user/addpermission',{email:userEmail,permissions:permission},reload,'json');
}
}
function disableList(listEmail){
$.post('/api/list/disable',{list:listEmail},showListResult,'json');
}
function dropList(listEmail){
$.post('/api/list/drop',{list:listEmail},showListResult,'json');
}
function dropMember(userEmail,listEmail){
$.post('/api/list/drop_member',{list:listEmail,email:userEmail},reload,'json');
}
function dropMod(userEmail,listEmail){
$.post('/api/list/drop_mod',{list:listEmail,email:userEmail},reload,'json');
}
function dropPermission(userEmail,permission){
if (confirm("Really withdraw permission from "+userEmail+"?")){
$.post('/api/user/droppermission',{email:userEmail,permissions:permission},reload,'json');
}
}
function enableList(listEmail){
$.post('/api/list/enable',{list:listEmail},showListResult,'json');
}
function hideList(listEmail){
$.post('/api/list/hide',{list:listEmail},showListResult,'json');
}
function loadArchive(listEmail,month){
$.get('/api/list/archive?month='+month+'&list='+listEmail,showListArchive,'json');
}
function loadArchiveSummary(listEmail){
$.get('/api/list/archive?list='+listEmail,showListArchiveSummary,'json');
}
function loadListDetail(listEmail){
$.post('/api/list/detail',{list:listEmail},showListDetail,'json');
}
function loadListOfModeratedLists(){
$.getJSON('/api/list/moderated', showListOfModeratedLists);
}
function loadListOfSubscribableLists(){
$.getJSON('/api/list/subscribable', showListList);
}
function loadMembers(listEmail){
$.post('/api/list/members',{list:listEmail},showMembers,'json');
}
function loadUserList(){
$.getJSON('/api/user/list', showUserList);
}
function reload(){
window.location.reload(true);
}
function showList(listEmail){
$.post('/api/list/show',{list:listEmail},showListResult,'json');
}
function showListArchive(data){
console.log(data);
let posts = data.archive.posts;
for (let time in posts){
let post = posts[time];
let row = $('
').html(''+post.subject+'').appendTo(row);
row.appendTo($('#archive table'));
}
}
function showListArchiveSummary(data){
console.log(data);
let summary = data.archive.summary;
for (let month in summary){
let url = 'archive?month='+month+'&list='+data.archive.list;
let row = $('
').html(''+summary[month]+'').appendTo(row);
row.appendTo($('#archive table'));
}
}
function showListDetail(data){
var options = ['forward_from','forward_attached','hide_receivers','reply_to_list','open_for_guests','open_for_subscribers','archive','edit_mods','delete_messages'];
options.forEach(function(option,index,array){
console.log(option,'→',data[option]);
if (data[option]) $('input[name="'+option+'"]').prop('checked',true);
});
if (data['delete_messages']) $('input[name="hold_time"]').val(data['delete_messages']);
}
function showListOfModeratedLists(data){
for (let i in data.lists){
let list = data.lists[i];
let row = $('
');
let addr = list.email.prefix+'@'+list.email.domain;
let inspect = 'inspect?list='+addr;
let td = $('
');
$('',{href:inspect}).text(addr).appendTo(td)
td.appendTo(row);
let states = [];
for (let state in list.state){
if (list.state[state] > 0) states.push(state);
}
$('
').text(states.toString()).appendTo(row);
if (list.last_error){
$('
').text('-').appendTo(row);
let select = $('',{name:addr}).change(function () {
let action = $(this).children("option:selected").val();
let list = $(this).attr('name');
if (confirm("This will "+action+" '"+list+"'. Are you sure?")) self[action+'List'](list);
});
$('').text('Actions').appendTo(select);
['enable','disable','drop','hide','show','test'].forEach(val => $('',{value:val}).text(val).appendTo(select));
select.appendTo($('
')).appendTo(row);
$('
').text(list.imap_host).appendTo(row);
$('
').text(list.imap_port).appendTo(row);
$('
').text(list.inbox).appendTo(row);
$('
').text(list.smtp_host).appendTo(row);
$('
').text(list.smtp_port).appendTo(row);
row.appendTo('#listlist');
}
if (data.user.permissions.includes('create lists')){
$('a[href=add_list]').show();
} else {
$('a[href=add_list]').hide();
}
}
function showListList(data){
for (let i in data.lists){
let list = data.lists[i];
let row = $('
');
let email = list.email.prefix + '@' + list.email.domain;
$('
').text(email).appendTo(row);
let states = [];
for (let state in list.state){
if (list.state[state] > 0) states.push(state);
}
$('
').text(states.toString()).appendTo(row);
let td = $('
',{class:'actions'});
$('',{onclick:"subscribeTo('"+email+"');"}).text('subscribe').appendTo(td);
$('',{onclick:"unsubscribeFrom('"+email+"');"}).text('unsubcribe').appendTo(td);
td.appendTo(row);
row.appendTo('#listlist');
}
}
function showListResult(result){
if ('error' in result){
alert(result.error);
return;
}
if ('success' in result){
alert(result.success);
reload();
return;
}
alert("Api call did not return result");
}
function showMembers(data){
var list_mail = data.list.email.prefix+'@'+data.list.email.domain;
for (let i in data.members){
let member = data.members[i];
let row = $('
');
$('
').text(member.name).appendTo(row);
$('
').text(member.email).appendTo(row);
$('
').text(member.state).appendTo(row);
let col = $('
');
console.log("data",data);
if (member.state.includes("moderator")) {
if (!member.state.includes("owner")) $('',{onclick:'dropMod("'+member.email+'","'+list_mail+'")'}).text("- moderator").appendTo(col);
} else $('',{onclick:'addMod("'+member.email+'","'+list_mail+'")'}).text("+ moderator").appendTo(col);
if (!member.state.includes("owner")) $('',{onclick:'dropMember("'+member.email+'","'+list_mail+'")'}).text("remove").appendTo(col);
col.appendTo(row);
row.appendTo('#memberlist');
}
}
function showUserList(data){
let isAdmin = data.user.permissions.includes('admin');
for (let i in data.users){
let user = data.users[i];
let row = $('
');
$('
').text(user.name).appendTo(row);
$('
').text(user.email).appendTo(row);
$('
').text(user.password).appendTo(row);
$('
').text(user.permissions).appendTo(row);
let permissions = $('');
if (isAdmin){
if (user.permissions.includes('admin')){
$('').text("- admin").click(() => dropPermission(user.email,1)).appendTo(permissions);
} else {
$('').text("+ admin").click(() => addPermission(user.email,1)).appendTo(permissions);
}
if (user.permissions.includes('create lists')){
$('').text("- create lists").click(() => dropPermission(user.email,2)).appendTo(permissions);
} else {
$('').text("+ create lists").click(() => addPermission(user.email,2)).appendTo(permissions);
}
}
permissions.appendTo(row);
row.appendTo('#userlist');
}
if (isAdmin){
$('a[href=register]').show();
} else {
$('a[href=register]').hide();
}
}
function start(){
console.log("application started");
}
function subscribeTo(listEmail){
window.location.href='subscribe?list='+listEmail;
}
function testList(listEmail){
$.post('/api/list/test',{list:listEmail},showListResult,'json');
}
function unsubscribeFrom(listEmail){
window.location.href='unsubscribe?list='+listEmail;
}
$(start); // document.on ready