mirror of
https://github.com/Qortal/qortal-mobile.git
synced 2025-05-13 21:27:51 +00:00
updated admin_action
This commit is contained in:
parent
f97c91fe35
commit
bd19a0b6d2
@ -3048,46 +3048,102 @@ export const cancelSellOrder = async (data, isFromExtension) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const adminAction = async (data, isFromExtension) => {
|
export const adminAction = async (data, isFromExtension) => {
|
||||||
const requiredFields = [
|
const requiredFields = ["type"];
|
||||||
"type",
|
|
||||||
];
|
|
||||||
const missingFields: string[] = [];
|
const missingFields: string[] = [];
|
||||||
requiredFields.forEach((field) => {
|
requiredFields.forEach((field) => {
|
||||||
if (!data[field]) {
|
if (!data[field]) {
|
||||||
missingFields.push(field);
|
missingFields.push(field);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// For actions that require a value, check for 'value' field
|
||||||
|
const actionsRequiringValue = [
|
||||||
|
"addpeer",
|
||||||
|
"removepeer",
|
||||||
|
"forcesync",
|
||||||
|
"addmintingaccount",
|
||||||
|
"removemintingaccount",
|
||||||
|
];
|
||||||
|
if (
|
||||||
|
actionsRequiringValue.includes(data.type.toLowerCase()) &&
|
||||||
|
!data.value
|
||||||
|
) {
|
||||||
|
missingFields.push("value");
|
||||||
|
}
|
||||||
if (missingFields.length > 0) {
|
if (missingFields.length > 0) {
|
||||||
const missingFieldsString = missingFields.join(", ");
|
const missingFieldsString = missingFields.join(", ");
|
||||||
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
const errorMsg = `Missing fields: ${missingFieldsString}`;
|
||||||
throw new Error(errorMsg);
|
throw new Error(errorMsg);
|
||||||
}
|
}
|
||||||
const isGateway = await isRunningGateway()
|
const isGateway = await isRunningGateway();
|
||||||
if(isGateway){
|
if (isGateway) {
|
||||||
throw new Error('This action cannot be done through a gateway')
|
throw new Error("This action cannot be done through a gateway");
|
||||||
}
|
}
|
||||||
|
|
||||||
let apiEndpoint = '';
|
let apiEndpoint = "";
|
||||||
switch (data.type.toLowerCase()) {
|
let method = "GET"; // Default method
|
||||||
case 'stop':
|
let includeValueInBody = false;
|
||||||
apiEndpoint = await createEndpoint('/admin/stop');
|
switch (data.type.toLowerCase()) {
|
||||||
break;
|
case "stop":
|
||||||
case 'restart':
|
apiEndpoint = await createEndpoint("/admin/stop");
|
||||||
apiEndpoint = await createEndpoint('/admin/restart');
|
break;
|
||||||
break;
|
case "restart":
|
||||||
case 'bootstrap':
|
apiEndpoint = await createEndpoint("/admin/restart");
|
||||||
apiEndpoint = await createEndpoint('/admin/bootstrap');
|
break;
|
||||||
break;
|
case "bootstrap":
|
||||||
default:
|
apiEndpoint = await createEndpoint("/admin/bootstrap");
|
||||||
throw new Error(`Unknown admin action type: ${data.type}`);
|
break;
|
||||||
}
|
case "addmintingaccount":
|
||||||
|
apiEndpoint = await createEndpoint("/admin/mintingaccounts");
|
||||||
|
method = "POST";
|
||||||
|
includeValueInBody = true;
|
||||||
|
break;
|
||||||
|
case "removemintingaccount":
|
||||||
|
apiEndpoint = await createEndpoint("/admin/mintingaccounts");
|
||||||
|
method = "DELETE";
|
||||||
|
includeValueInBody = true;
|
||||||
|
break;
|
||||||
|
case "forcesync":
|
||||||
|
apiEndpoint = await createEndpoint("/admin/forcesync");
|
||||||
|
method = "POST";
|
||||||
|
includeValueInBody = true;
|
||||||
|
break;
|
||||||
|
case "addpeer":
|
||||||
|
apiEndpoint = await createEndpoint("/peers");
|
||||||
|
method = "POST";
|
||||||
|
includeValueInBody = true;
|
||||||
|
break;
|
||||||
|
case "removepeer":
|
||||||
|
apiEndpoint = await createEndpoint("/peers");
|
||||||
|
method = "DELETE";
|
||||||
|
includeValueInBody = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new Error(`Unknown admin action type: ${data.type}`);
|
||||||
|
}
|
||||||
|
// Prepare the permission prompt text
|
||||||
|
let permissionText = `Do you give this application permission to perform the admin action: ${data.type}`;
|
||||||
|
if (data.value) {
|
||||||
|
permissionText += ` with value: ${data.value}`;
|
||||||
|
}
|
||||||
|
|
||||||
const resPermission = await getUserPermission({
|
const resPermission = await getUserPermission(
|
||||||
text1: `Do you give this application permission to perform a node ${data.type}?`,
|
{
|
||||||
}, isFromExtension);
|
text1: permissionText,
|
||||||
const { accepted } = resPermission;
|
},
|
||||||
if (accepted) {
|
isFromExtension
|
||||||
const response = await fetch(apiEndpoint);
|
);
|
||||||
|
const { accepted } = resPermission;
|
||||||
|
if (accepted) {
|
||||||
|
// Set up options for the API call
|
||||||
|
const options: RequestInit = {
|
||||||
|
method: method,
|
||||||
|
headers: {},
|
||||||
|
};
|
||||||
|
if (includeValueInBody) {
|
||||||
|
options.headers["Content-Type"] = "text/plain";
|
||||||
|
options.body = data.value;
|
||||||
|
}
|
||||||
|
const response = await fetch(apiEndpoint, options);
|
||||||
if (!response.ok) throw new Error("Failed to perform request");
|
if (!response.ok) throw new Error("Failed to perform request");
|
||||||
|
|
||||||
let res;
|
let res;
|
||||||
@ -3100,7 +3156,6 @@ export const adminAction = async (data, isFromExtension) => {
|
|||||||
} else {
|
} else {
|
||||||
throw new Error("User declined request");
|
throw new Error("User declined request");
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const openNewTab = async (data, isFromExtension) => {
|
export const openNewTab = async (data, isFromExtension) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user