const utils = require('../utils');
const api = require('../utils/api');
const ui = require('../ui/common');
const logger_commands = require('../utils/logger');
const $ = require('jquery');
const pretty_bytes = require('pretty-bytes');
const list_row = require('../ui/list_row');
const modal = require('../utils/modal');
populate_linked_accounts_table();
async function populate_linked_accounts_table() {
$("#linked-accounts-list").empty();
const {
linked_accounts
} = await api.get("/account/linked_accounts");
let any_accounts_linked = false;
for (const [linked_account_name, linked_account] of Object.entries(linked_accounts)) {
if (linked_account.linked_account_username) {
any_accounts_linked = true;
list_row.create("linked-accounts", {
title: utils.capitalize(linked_account_name),
...(linked_account.needs_new_auth ? {
icon: 'warning',
text: `Your ${linked_account_name} account link requires re-authentication for user ${linked_account.linked_account_username}`
} : {
text: `Your ${linked_account_name} account is linked with user ${linked_account.linked_account_username}`
})
}).on('click', function () {
if (linked_account.needs_new_auth) {
$('#existing-linked-account-message').text('This account was previously linked but the authentication has expired. Would you like to relink the account or unlink it fully?')
$("#relink-account").show();
} else {
$('#existing-linked-account-message').text(`Are you sure you want to unlink your ${linked_account_name} account?`);
$("#relink-account").hide().off('click').on("click", async function () {
await api.post('/account/linked_accounts/link');
ui.show_banner({
message: `Account ${linked_account_name} Unlinked`
});
await populate_linked_account_dropdown();
await populate_linked_accounts_table();
modal.close();
});
}
$("#unlink-account").off('click').on("click", async function () {
await api.delete('/account/linked_accounts/unlink/' + linked_account_name);
ui.show_banner({
message: `Account ${linked_account_name} Unlinked`
});
await populate_linked_account_dropdown();
await populate_linked_accounts_table();
modal.close();
});
ui.prefill_inputs("#existing-linked-account", {
linked_account_name
});
modal.create('existing-linked-account-modal');
});
}
}
if (!any_accounts_linked) {
list_row.create("linked-accounts", {
title: 'No accounts linked'
});
}
}
populate_linked_account_dropdown();
async function populate_linked_account_dropdown() {
const [$input] = ui.get_inputs(['#new-linked-account'], 'linked_account_name');
$input.empty();
const {
available_accounts
} = await api.get("/account/linked_accounts/available");
if (available_accounts.length) {
$input.prop('disabled', false);
$('#new-linked-account').find('button').prop('disabled', false);
} else {
$input.append($('<option>', {
text: 'All possible accounts are linked'
}));
$input.prop('disabled', true);
$('#new-linked-account').find('button').prop('disabled', true);
return;
}
for (const available_account of available_accounts) {
$input.append($('<option>', {
value: available_account,
text: utils.capitalize(available_account)
}));
}
}
api.on_submit("/account/linked_accounts/link", [
$("#new-linked-account"), $("#existing-linked-account")
], {
/* no value conversions */
}, [
["linked_account_name", "Linked account name must be provided"]
], async function () {
await populate_linked_account_dropdown();
await populate_linked_accounts_table();
});
ui.init_new_password();
$("#settings").on("click", function () {
$("#logs_size").text(pretty_bytes(logger_commands.logs_memory_usage()));
});
$("#save_display_name").on("click", function () {
utils.validate.keys({
display_name: $("#display_name").val().trim()
}, [
["display_name", "Username cannot be empty"]
]).then(function (data) {
return api.post("/account/update/display-name", data);
}).then(function (data) {
ui.show_banner(data);
}).catch(api.handle_error_message);
});
$("#save_new_password").on("click", function () {
utils.validate.keys({
current_password: $("#current_password").val(),
new_password: $("#new_password").val()
}, [
["current_password", "Current password required"],
["new_password", "Passwords don't match", password => password === $("#confirm_password").val()],
["new_password", "You must appease the password strength meter", password => utils.validate.password(password).score]
]).then(function (data) {
return api.post("/account/update/password", data);
}).then(function (data) {
ui.show_banner(data);
}).catch(api.handle_error_message);
});
$("#export-button").on("click", async function () {
await api.get('/export');
});
toggle_logger_settings_state(
logger_commands.logs_enabled(),
false
);
$("#toggle_logs").on("click", function () {
toggle_logger_settings_state(
logger_commands.toggle_logs(), true
);
});
function toggle_logger_settings_state(enable, show_banner) {
if (enable) {
$("#logs_size").text(pretty_bytes(logger_commands.logs_memory_usage()));
$("#logs_enabled_message").show();
$("#logs_disabled_message").hide();
$("#toggle_logs").text("Disable Logs");
$("#export_logs").prop('disabled', false).on("click", function () {
logger_commands.export_logs().then(function () {
ui.show_banner({
message: "Logs Exported"
});
$("#logs_size").text(pretty_bytes(logger_commands.logs_memory_usage()));
}).catch(api.handle_error_message);
});
if (show_banner) {
ui.show_banner({
message: "Logs Enabled"
});
}
} else {
$("#logs_enabled_message").hide();
$("#logs_disabled_message").show();
$("#toggle_logs").text("Enable Logs");
$("#export_logs").prop('disabled', true).off("click");
if (show_banner) {
ui.show_banner({
message: "Logs Disabled"
});
}
}
}