soselo/views/instance.php

321 lines
11 KiB
PHP
Raw Normal View History

2022-11-27 20:20:18 +00:00
<div class="hmax">
<div id="tabs"></div>
<div id="tabs-item" data-id="{id}">
2022-11-28 14:21:46 +00:00
<a href="javascript:window.view.instance.load_content('{id}',true)">
2022-11-27 20:20:18 +00:00
<div class="tab-content">
<i class="fa fa-{icon} fa-fw"></i>
2022-11-27 20:20:18 +00:00
<span>{title}</span>
</div>
</a>
</div>
<div id="content">
<div id="container"></div>
</div>
</div>
<script>
window.view.instance = {
show_config: function() {
window.location.hash = window.location.hash.replace('#instance/','#instance_config/');
},
do: {
new_accounts: {
approve: function(_id) {
window.view.instance.do.new_accounts._perform('approve', _id);
},
reject: function(_id) {
window.view.instance.do.new_accounts._perform('reject', _id);
},
_perform: function(action, _id) {
const hargs = get_hash_arguments();
http.get(`api/v1/http/${hargs.software}/accounts/${action}/?instance=${hargs.instance}&id=${_id}`,
{}, function(js) {
window.view.instance.load_content('new-accounts');
});
},
},
filter_users: {
get_current_filter: function() {
var data = {};
if (E.element('#filters-current input[name=preset_id]').value !== '-1')
data.id = E.element('#filters-current input[name=preset_id]').value;
var preset = E.element('#filters-current input[name="preset_name"]').value.trim();
if (preset !== '' && preset.toLowerCase() !== 'undefined')
data.preset_name = E.element('#filters-current input[name="preset_name"]').value;
data.user_filter = E.element('#filters-current input[name=user_filter]:checked').value;
var prof_stype = E.element("#filters-current input[name=profile_search_type]:checked").value;
var profile = E.element("#filters-current textarea[name=profile]").value;
if (prof_stype === 'empty')
profile = '<empty>';
else if (prof_stype === 'expr')
profile = 'expr: '+profile;
data.profile = btoa(profile);
const obj_noavatar = E.element('*[name=no_avatar]:checked');
if (obj_noavatar !== null)
data.no_avatar = '';
const obj_nostatuses = E.element('*[name=no_statuses]:checked');
if (obj_nostatuses !== null)
data.no_statuses = '';
const obj_nofollows = E.element('*[name=no_follows]:checked');
if (obj_nofollows !== null)
data.no_follows = '';
return data;
},
set_current_filter: function(data) {
const _process = function(_d) {
const hargs = get_hash_arguments();
var d = JSON.parse(JSON.stringify(_d));
if (d.id === undefined)
E.element('#filters-current input[name=preset_id]').value = -1;
else E.element('#filters-current input[name=preset_id]').value = d.id;
if (d.preset_name === undefined) {
d.preset_name = 'Undefined';
E.elemid('filter-current-name').innerText = '*New filter';
} else E.elemid('filter-current-name').innerText = d.preset_name;
E.element('#filters-current input[name="preset_name"]').value = d.preset_name;
if (d.user_filter === undefined) d.user_filter = 'local';
E.element('#filters-current input[name=user_filter][value='+d.user_filter+']').click();
if (d.profile === undefined) d.profile = '';
d.profile = atob(d.profile);
if (d.profile === '<empty>')
E.element('#filters-current #profile-search-type-empty').click();
else if (d.profile.startsWith('expr:')) {
d.profile = d.profile.replace(/^expr:/,'').trim();
E.element('#filters-current #profile-search-type-expr').click();
E.element("#filters-current textarea[name=profile]").value = d.profile;
}
else {
E.element('#filters-current #profile-search-type-simple').click();
E.element("#filters-current textarea[name=profile]").value = d.profile;
}
E.elements('#filters-current input[type=checkbox]').forEach(function(it){ it.checked = false });
if (d.no_avatar !== undefined) E.element('#filters-current input[name=no_avatar]').click();
if (d.no_statuses !== undefined) E.element('#filters-current input[name=no_statuses]').click();
if (d.no_follows !== undefined) E.element('#filters-current input[name=no_follows]').click();
};
if (typeof data === 'object')
_process(data);
else if (typeof data === 'string') {
for (var i = 0; i < window.vars.user_filters.length; i++) {
if (window.vars.user_filters[i].id === data)
_process(window.vars.user_filters[i]);
}
}
//else // TODO: load the filter by API
},
execute: function() {
const btn = E.elemid('btncollapse-filters-current');
if (btn.innerText.trim() === 'hide')
btn.click();
const data = window.view.instance.do.filter_users.get_current_filter();
const hargs = get_hash_arguments();
var payload = data;
payload.instance = hargs.instance;
payload = JSON_to_URLEncoded(payload);
http.get(`api/v1/database/${hargs.software}/accounts/search?${payload}`, data, function(js) {
console.log(js);
});
},
save_filter: function() {
const data = window.view.instance.do.filter_users.get_current_filter();
if (data.preset_name === undefined)
// TODO: add a toast with the error
return alert("You must define a preset_name other than 'Undefined' or empty");
if (data.id === undefined)
data.id = uuidv4();
window.view.instance.do.filter_users.set_current_filter(data);
http.post('api/v1/config/filters/put?type=users', data, function(js) {
if (js.ok !== undefined)
window.view.instance.load.filter_users();
});
},
},
},
load: {
new_accounts: function() {
const hargs = get_hash_arguments();
E.http_template('instance/new-account', function(html) {
E.element('#window-instance #container').innerHTML = html;
http.get(`api/v1/database/${hargs.software}/get-pending-users/?instance=${hargs.instance}`,
{}, function(js) {
E.template('new-accounts', function(TPL) {
if ((js.count === undefined || js.account === null)
|| js.count === 0)
return 'There is not any pending accounts';
var tpl = TPL;
tpl = tpl.replaceAll('{count}', js.count);
tpl = tpl.replaceAll('{id}', js.account.id);
tpl = tpl.replaceAll('{text}', html2text(js.account.text));
tpl = tpl.replaceAll('{email}', html2text(js.account.email));
tpl = tpl.replaceAll('{username}', html2text(js.account.username));
tpl = tpl.replaceAll('{ip_address}', html2text(js.account.sign_up_ip));
return tpl;
});
console.log(js);
});
});
},
2022-12-03 17:54:18 +00:00
filter_users: function() {
const hargs = get_hash_arguments();
2022-12-03 17:54:18 +00:00
E.http_template('instance/filter-users', function(html) {
html = html.replaceAll('{view.js}', 'window.view.instance.do.filter_users');
2022-12-03 17:54:18 +00:00
E.element('#window-instance #container').innerHTML = html;
E.elemid('profile-search-type-simple').onchange = function(e) {
if (e.target.checked) {
E.element('#filters-current textarea[name=profile]').setAttribute('placeholder', 'Input your text here');
E.element('#filters-current textarea[name=profile]').removeAttribute('disabled');
}
};
E.elemid('profile-search-type-expr').onchange = function(e) {
if (e.target.checked) {
E.element('#filters-current textarea[name=profile]').setAttribute('placeholder',
'(words "apple,banana" AND contains "i like apples") OR words "pineapple,strawberry"');
E.element('#filters-current textarea[name=profile]').removeAttribute('disabled');
}
};
E.elemid('profile-search-type-empty').onchange = function(e) {
if (e.target.checked) {
E.element('#filters-current textarea[name=profile]').setAttribute('placeholder', '<Profile content is empty>');
E.element('#filters-current textarea[name=profile]').setAttribute('disabled', 'disabled');
E.element("#filters-current textarea[name=profile]").value = '';
}
}
2022-12-03 17:54:18 +00:00
E.custom.btncollapse_render();
http.get('api/v1/config/filters/get?type=users', {}, function(js) {
window.vars.user_filters = js;
E.template('filters-all', function(TPL) {
var html = '';
for (var i = 0; i < js.length; i++) {
var tpl = TPL;
tpl = tpl.replaceAll('{id}', js[i].id);
tpl = tpl.replaceAll('{name}', js[i].preset_name);
html += tpl;
}
return html;
});
if (hargs.filter_id !== undefined)
window.view.instance.do.filter_users.set_current_filter(hargs.filter_id);
});
2022-12-03 17:54:18 +00:00
});
},
},
load_content: function(content, click) {
click = click === undefined ? false : click;
const hargs = get_hash_arguments();
2022-11-28 15:52:57 +00:00
const _empty_content = function() {
E.element('#window-instance #container').innerHTML = '';
};
if (click) {
delete window.hash_argument_nofirechange;
del_hash_argument('filter_id', false);
}
switch (content) {
case 'home':
2022-11-28 15:52:57 +00:00
_empty_content();
window.location.hash = '#home';
return;
case 'settings':
2022-11-28 15:52:57 +00:00
_empty_content();
return window.view.instance.show_config();
default:
2022-11-28 15:52:57 +00:00
_empty_content();
if (hargs.content === undefined)
set_hash_argument('content', content, false);
return window.view.instance
.load[content.replaceAll('-','_')]();
}
},
};
function title__instance() {}
function load__instance(args) {
const hargs = get_hash_arguments();
const _error400 = function(msg) {
msg = msg || 'Parameters are not correct';
console.log('TODO: handle error correctly');
};
const _paint = function() {
2022-11-27 20:20:18 +00:00
const menu = [
{
id: 'home',
title: '',
icon: 'home',
},
{
id: 'filter-users',
title: 'Users filter',
icon: 'user',
2022-11-27 20:20:18 +00:00
},
{
id: 'filter-posts',
title: 'Posts filter',
icon: 'file-text',
2022-11-27 20:20:18 +00:00
},
{
id: 'new-accounts',
2022-11-27 20:20:18 +00:00
title: 'New accounts',
icon: 'user-plus',
},
{
id: 'settings',
title: hargs['instance'],
icon: 'gear',
2022-11-27 20:20:18 +00:00
},
];
E.template('tabs', function(TPL) {
var html = '';
for (var i = 0; i < menu.length; i++) {
var tpl = TPL;
tpl = tpl.replaceAll('{id}', menu[i]['id']);
2022-11-27 20:20:18 +00:00
tpl = tpl.replaceAll('{title}', menu[i]['title']);
tpl = tpl.replaceAll('{icon}', menu[i]['icon']);
2022-11-27 20:20:18 +00:00
html += tpl;
}
return html;
});
if (hargs['content'] !== undefined)
window.view.instance.load_content(hargs['content']);
};
const _main = function() {
if (hargs['software'] === undefined ||
!window.vars['instance_config']['supported_ap_software']
.includes(hargs['software']))
return _error400();
if (hargs['instance'] === undefined)
return _error400();
var found = false;
for (var i = 0; i < window.vars['instance_config']['hosts'][hargs['software']].length; i++)
{
const item = window.vars['instance_config']['hosts'][hargs['software']][i];
if (item['instance'] === hargs['instance']) {
window.vars['current_instance'] = item;
found = true;
break;
}
}
if (!found)
return _error400();
apcontrol_title(`Instance (${hargs['instance']})`);
const config = parse_ini_config(window.vars['current_instance']['config']);
2022-11-27 20:20:18 +00:00
const musthave = window.consts['instance_config'][hargs['software']]['musthave'];
for (var i = 0; i < musthave.length; i++) {
if (config[musthave[i]] === undefined) {
window.view.instance.show_config();
2022-11-27 20:20:18 +00:00
return false;
}
}
return _paint();
};
if (window.vars['instance_config'] === undefined) {
http.get('api/v1/config/get',{},function(data) {
window.vars['instance_config'] = JSON.parse(data);
_main();
});
} else { _main() }
}
</script>