mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-06-04 09:03:50 -04:00
migrate to deno 2.0 serve
This commit is contained in:
@@ -10,13 +10,7 @@ import { generateApiDeleteEmail } from '../../commandUtils.ts';
|
||||
import utils from '../../utils.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
export const apiKeyDelete = async (
|
||||
requestEvent: Deno.RequestEvent,
|
||||
query: Map<string, string>,
|
||||
apiUserid: BigInt,
|
||||
apiUserEmail: string,
|
||||
apiUserDelCode: string,
|
||||
) => {
|
||||
export const apiKeyDelete = async (query: Map<string, string>, apiUserid: bigint, apiUserEmail: string, apiUserDelCode: string): Promise<Response> => {
|
||||
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('email') && (query.get('email') || '').length > 0) {
|
||||
if (apiUserid === BigInt(query.get('user') || '0') && apiUserEmail === query.get('email')) {
|
||||
if (query.has('code') && (query.get('code') || '').length > 0) {
|
||||
@@ -26,28 +20,25 @@ export const apiKeyDelete = async (
|
||||
|
||||
await dbClient.execute('DELETE FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKeyDelete.ts:25', 'insert into', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Channel Clean Failed.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Channel Clean Failed.');
|
||||
}
|
||||
|
||||
await dbClient.execute('DELETE FROM all_keys WHERE userid = ?', [apiUserid]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKeyDelete.ts:34', 'delete from', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Delete Key Failed.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Delete Key Failed.');
|
||||
} else {
|
||||
// Send OK as response to indicate key deletion was successful
|
||||
requestEvent.respondWith(stdResp.OK('You have been removed from the DB, Goodbye.'));
|
||||
return;
|
||||
return stdResp.OK('You have been removed from the DB, Goodbye.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('Invalid Delete Code.'));
|
||||
return stdResp.Forbidden('Invalid Delete Code.');
|
||||
}
|
||||
} else {
|
||||
// User does not have their delete code yet, so we need to generate one and email it to them
|
||||
@@ -58,32 +49,29 @@ export const apiKeyDelete = async (
|
||||
// Execute the DB modification
|
||||
await dbClient.execute('UPDATE all_keys SET deleteCode = ? WHERE userid = ?', [deleteCode, apiUserid]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKeyDelete.ts:57', 'update', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Delete Code Failed'));
|
||||
erroredOut = true;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Delete Code Failed');
|
||||
}
|
||||
|
||||
// "Send" the email
|
||||
await sendMessage(config.api.email, generateApiDeleteEmail(apiUserEmail, deleteCode)).catch(() => {
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to send email.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to send email.');
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(stdResp.FailedDependency('Please look for an email containing a Delete Key and run this query again with said key.'));
|
||||
return;
|
||||
return stdResp.FailedDependency('Please look for an email containing a Delete Key and run this query again with said key.');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('You can only delete your own key.'));
|
||||
return stdResp.Forbidden('You can only delete your own key.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import dbClient from '../../db/client.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
|
||||
export const apiChannel = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
|
||||
if (query.has('user') && (query.get('user') || '').length > 0) {
|
||||
if (apiUserid === BigInt(query.get('user') || '0')) {
|
||||
// Flag to see if there is an error inside the catch
|
||||
@@ -11,25 +11,23 @@ export const apiChannel = async (requestEvent: Deno.RequestEvent, query: Map<str
|
||||
// Get all channels userid has authorized
|
||||
const dbAllowedChannelQuery = await dbClient.query('SELECT * FROM allowed_channels WHERE userid = ?', [apiUserid]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiChannel.ts', 'query', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to get channels.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to get channels.');
|
||||
} else {
|
||||
// Customized strinification to handle BigInts correctly
|
||||
const returnChannels = JSON.stringify(dbAllowedChannelQuery, (_key, value) => (typeof value === 'bigint' ? value.toString() : value));
|
||||
// Send channel list as response
|
||||
requestEvent.respondWith(stdResp.OK(returnChannels));
|
||||
return;
|
||||
return stdResp.OK(returnChannels);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('You can only view your own channels.'));
|
||||
return stdResp.Forbidden('You can only view your own channels.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ import { generateApiKeyEmail } from '../../commandUtils.ts';
|
||||
import utils from '../../utils.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
|
||||
export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string, string>) => {
|
||||
export const apiKey = async (query: Map<string, string>): Promise<Response> => {
|
||||
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('email') && (query.get('email') || '').length > 0) {
|
||||
// Generate new secure key
|
||||
const newKey = await nanoid(25);
|
||||
@@ -23,30 +23,27 @@ export const apiKey = async (requestEvent: Deno.RequestEvent, query: Map<string,
|
||||
.execute('INSERT INTO all_keys(userid,apiKey,email) values(?,?,?)', [BigInt(query.get('user') || '0'), newKey, (query.get('email') || '').toLowerCase()])
|
||||
.catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKey.ts:27', 'insert into', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to store key.');
|
||||
}
|
||||
|
||||
// "Send" the email
|
||||
await sendMessage(config.api.email, generateApiKeyEmail(query.get('email') || 'no email', newKey)).catch(() => {
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to send email.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to send email.');
|
||||
} else {
|
||||
// Send basic OK to indicate key has been sent
|
||||
requestEvent.respondWith(stdResp.OK('Email Sent.'));
|
||||
return;
|
||||
return stdResp.OK('Email Sent.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
|
||||
export const apiKeyAdmin = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
|
||||
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('a') && (query.get('a') || '').length > 0) {
|
||||
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
|
||||
// Generate new secure key
|
||||
@@ -19,24 +19,22 @@ export const apiKeyAdmin = async (requestEvent: Deno.RequestEvent, query: Map<st
|
||||
// Insert new key/user pair into the db
|
||||
await dbClient.execute('INSERT INTO all_keys(userid,apiKey) values(?,?)', [apiUserid, newKey]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKeyAdmin.ts:24', 'insert into', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to store key.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to store key.');
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(stdResp.OK(JSON.stringify({ key: newKey, userid: query.get('user') })));
|
||||
return;
|
||||
return stdResp.OK(JSON.stringify({ key: newKey, userid: query.get('user') }));
|
||||
}
|
||||
} else {
|
||||
// Only allow the db admin to use this API
|
||||
requestEvent.respondWith(stdResp.Forbidden(stdResp.Strings.restricted));
|
||||
return stdResp.Forbidden(stdResp.Strings.restricted);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import config from '../../../config.ts';
|
||||
import dbClient from '../../db/client.ts';
|
||||
import { queries } from '../../db/common.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
cache,
|
||||
@@ -14,7 +15,7 @@ import stdResp from '../stdResponses.ts';
|
||||
|
||||
const apiWarning = `The following roll was conducted using my built in API. If someone in this channel did not request this roll, please report API abuse here: <${config.api.supportURL}>`;
|
||||
|
||||
export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
|
||||
export const apiRoll = async (query: Map<string, string>, apiUserid: bigint, request: Request): Promise<Response> => {
|
||||
// Make sure query contains all the needed parts
|
||||
if (
|
||||
query.has('rollstr') &&
|
||||
@@ -26,8 +27,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
||||
) {
|
||||
if (query.has('n') && query.has('m')) {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.BadRequest("Cannot have both 'n' and 'm'."));
|
||||
return;
|
||||
return stdResp.BadRequest("Cannot have both 'n' and 'm'.");
|
||||
}
|
||||
|
||||
// Check if user is authenticated to use this endpoint
|
||||
@@ -64,25 +64,23 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
||||
const originalCommand = query.get('rollstr');
|
||||
|
||||
if (rollCmd.length === 0) {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest('rollCmd is required.'));
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient
|
||||
.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'EmptyInput', null])
|
||||
.catch((e) => utils.commonLoggers.dbError('apiRoll.ts:65', 'insert', e));
|
||||
return;
|
||||
|
||||
// Alert API user that they messed up
|
||||
return stdResp.BadRequest('rollCmd is required.');
|
||||
}
|
||||
|
||||
if (query.has('o') && query.get('o')?.toLowerCase() !== 'd' && query.get('o')?.toLowerCase() !== 'a') {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest("Order must be set to 'a' or 'd'."));
|
||||
|
||||
// Always log API rolls for abuse detection
|
||||
dbClient
|
||||
.execute(queries.insertRollLogCmd(1, 1), [originalCommand, 'BadOrder', null])
|
||||
.catch((e) => utils.commonLoggers.dbError('apiRoll.ts:66', 'insert', e));
|
||||
return;
|
||||
|
||||
// Alert API user that they messed up
|
||||
return stdResp.BadRequest("Order must be set to 'a' or 'd'.");
|
||||
}
|
||||
|
||||
// Clip off the leading prefix. API calls must be formatted with a prefix at the start to match how commands are sent in Discord
|
||||
@@ -106,7 +104,7 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
||||
await queueRoll(
|
||||
<QueuedRoll> {
|
||||
apiRoll: true,
|
||||
api: { requestEvent, channelId: BigInt(query.get('channel') || '0'), userId: BigInt(query.get('user') || '') },
|
||||
api: { request, channelId: BigInt(query.get('channel') || '0'), userId: BigInt(query.get('user') || '') },
|
||||
rollCmd,
|
||||
modifiers,
|
||||
originalCommand,
|
||||
@@ -115,18 +113,16 @@ export const apiRoll = async (requestEvent: Deno.RequestEvent, query: Map<string
|
||||
} catch (err) {
|
||||
// Handle any errors we missed
|
||||
log(LT.ERROR, `Unhandled Error: ${JSON.stringify(err)}`);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Something went wrong.'));
|
||||
return stdResp.InternalServerError('Something went wrong.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(
|
||||
stdResp.Forbidden(
|
||||
`Verify you are a member of the guild you are sending this roll to. If you are, the ${config.name} may not have that registered, please send a message in the guild so ${config.name} can register this. This registration is temporary, so if you see this error again, just poke your server again.`,
|
||||
),
|
||||
return stdResp.Forbidden(
|
||||
`Verify you are a member of the guild you are sending this roll to. If you are, the ${config.name} may not have that registered, please send a message in the guild so ${config.name} can register this. This registration is temporary, so if you see this error again, just poke your server again.`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,16 +4,14 @@ import {
|
||||
STATUS_TEXT,
|
||||
} from '../../../deps.ts';
|
||||
|
||||
export const heatmapPng = async (requestEvent: Deno.RequestEvent) => {
|
||||
export const heatmapPng = (): Response => {
|
||||
const file = Deno.readFileSync('./src/endpoints/gets/heatmap.png');
|
||||
const imageHeaders = new Headers();
|
||||
imageHeaders.append('Content-Type', 'image/png');
|
||||
// Send basic OK to indicate key has been sent
|
||||
requestEvent.respondWith(
|
||||
new Response(file, {
|
||||
status: STATUS_CODE.OK,
|
||||
statusText: STATUS_TEXT[STATUS_CODE.OK],
|
||||
headers: imageHeaders,
|
||||
}),
|
||||
);
|
||||
return new Response(file, {
|
||||
status: STATUS_CODE.OK,
|
||||
statusText: STATUS_TEXT[STATUS_CODE.OK],
|
||||
headers: imageHeaders,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import dbClient from '../../db/client.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt) => {
|
||||
export const apiChannelAdd = async (query: Map<string, string>, apiUserid: bigint): Promise<Response> => {
|
||||
if (query.has('user') && (query.get('user') || '').length > 0 && query.has('channel') && (query.get('channel') || '').length > 0) {
|
||||
if (apiUserid === BigInt(query.get('user') || '0')) {
|
||||
// Flag to see if there is an error inside the catch
|
||||
@@ -11,24 +11,22 @@ export const apiChannelAdd = async (requestEvent: Deno.RequestEvent, query: Map<
|
||||
// Insert new user/channel pair into the db
|
||||
await dbClient.execute('INSERT INTO allowed_channels(userid,channelid) values(?,?)', [apiUserid, BigInt(query.get('channel') || '0')]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiChannelAdd.ts:17', 'insert into', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to store channel.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to store channel.');
|
||||
} else {
|
||||
// Send OK to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK('Successfully added channel.'));
|
||||
return;
|
||||
return stdResp.OK('Successfully added channel.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('You can only add channels to your key.'));
|
||||
return stdResp.Forbidden('You can only add channels to your key.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ import dbClient from '../../db/client.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
|
||||
export const apiChannelManageActive = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
|
||||
if (query.has('channel') && (query.get('channel') || '').length > 0 && query.has('user') && (query.get('user') || '').length > 0) {
|
||||
if (apiUserid === BigInt(query.get('user') || '0')) {
|
||||
// Flag to see if there is an error inside the catch
|
||||
@@ -21,24 +21,22 @@ export const apiChannelManageActive = async (requestEvent: Deno.RequestEvent, qu
|
||||
.execute('UPDATE allowed_channels SET active = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')])
|
||||
.catch((e) => {
|
||||
utils.commonLoggers.dbError('apiChannelManageActive.ts:25', 'update', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to update channel.');
|
||||
} else {
|
||||
// Send API key as response
|
||||
requestEvent.respondWith(stdResp.OK(`Successfully active to ${value}.`));
|
||||
return;
|
||||
return stdResp.OK(`Successfully active to ${value}.`);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('You can only manage your own channels.'));
|
||||
return stdResp.Forbidden('You can only manage your own channels.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import dbClient from '../../db/client.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
|
||||
export const apiChannelManageBan = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
|
||||
if (
|
||||
query.has('a') &&
|
||||
(query.get('a') || '').length > 0 &&
|
||||
@@ -29,24 +29,22 @@ export const apiChannelManageBan = async (requestEvent: Deno.RequestEvent, query
|
||||
.execute('UPDATE allowed_channels SET banned = ? WHERE userid = ? AND channelid = ?', [value, apiUserid, BigInt(query.get('channel') || '0')])
|
||||
.catch((e) => {
|
||||
utils.commonLoggers.dbError('apiChannelManageBan.ts:28', 'update', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError('Failed to update channel.'));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError('Failed to update channel.');
|
||||
} else {
|
||||
// Send OK to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK(`Successfully active to ${value}.`));
|
||||
return;
|
||||
return stdResp.OK(`Successfully active to ${value}.`);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden(stdResp.Strings.restricted));
|
||||
return stdResp.Forbidden(stdResp.Strings.restricted);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import dbClient from '../../db/client.ts';
|
||||
import stdResp from '../stdResponses.ts';
|
||||
import utils from '../../utils.ts';
|
||||
|
||||
export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<string, string>, apiUserid: BigInt, path: string) => {
|
||||
export const apiKeyManage = async (query: Map<string, string>, apiUserid: bigint, path: string): Promise<Response> => {
|
||||
if (query.has('a') && (query.get('a') || '').length > 0 && query.has('user') && (query.get('user') || '').length > 0) {
|
||||
if (apiUserid === config.api.admin && apiUserid === BigInt(query.get('a') || '0')) {
|
||||
// Flag to see if there is an error inside the catch
|
||||
@@ -28,24 +28,22 @@ export const apiKeyManage = async (requestEvent: Deno.RequestEvent, query: Map<s
|
||||
// Execute the DB modification
|
||||
await dbClient.execute('UPDATE all_keys SET ?? = ? WHERE userid = ?', [key, value, apiUserid]).catch((e) => {
|
||||
utils.commonLoggers.dbError('apiKeyManage.ts', 'update', e);
|
||||
requestEvent.respondWith(stdResp.InternalServerError(`Failed to ${key} to ${value}.`));
|
||||
erroredOut = true;
|
||||
});
|
||||
|
||||
// Exit this case now if catch errored
|
||||
if (erroredOut) {
|
||||
return;
|
||||
return stdResp.InternalServerError(`Failed to ${key} to ${value}.`);
|
||||
} else {
|
||||
// Send OK as response to indicate modification was successful
|
||||
requestEvent.respondWith(stdResp.OK(`Successfully ${key} to ${value}.`));
|
||||
return;
|
||||
return stdResp.OK(`Successfully ${key} to ${value}.`);
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they shouldn't be doing this
|
||||
requestEvent.respondWith(stdResp.Forbidden('You can only manage your own key.'));
|
||||
return stdResp.Forbidden('You can only manage your own key.');
|
||||
}
|
||||
} else {
|
||||
// Alert API user that they messed up
|
||||
requestEvent.respondWith(stdResp.BadRequest(stdResp.Strings.missingParams));
|
||||
return stdResp.BadRequest(stdResp.Strings.missingParams);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user