Implement the endpoint for using a counter
This commit is contained in:
parent
eb518341c8
commit
ce3dbbfe47
1 changed files with 43 additions and 0 deletions
43
src/endpoints/useCounter.ts
Normal file
43
src/endpoints/useCounter.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { channelNameSchema, counterNameSchema } from "$/schemas/params";
|
||||||
|
import { nightbotCustomHeadersSchema } from "$/schemas/nightbot";
|
||||||
|
import { config, database } from "$/main";
|
||||||
|
import { ServerRoute } from "@hapi/hapi";
|
||||||
|
import boom from "@hapi/boom";
|
||||||
|
import Joi from "joi";
|
||||||
|
|
||||||
|
const route: ServerRoute = {
|
||||||
|
method: [ `GET`, `PATCH` ], path: `/channels/{channel}/counters/{counter}`,
|
||||||
|
options: {
|
||||||
|
validate: {
|
||||||
|
params: Joi.object({
|
||||||
|
channel: channelNameSchema,
|
||||||
|
counter: counterNameSchema,
|
||||||
|
}),
|
||||||
|
headers: nightbotCustomHeadersSchema.unknown(),
|
||||||
|
query: Joi.object({
|
||||||
|
delta: Joi.number().integer().required(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
async handler(request) {
|
||||||
|
const { channel, counter } = request.params;
|
||||||
|
const { delta } = request.query;
|
||||||
|
|
||||||
|
if (request.headers["nightbot-user"]) {
|
||||||
|
let data = new URLSearchParams(request.headers["nightbot-user"]);
|
||||||
|
|
||||||
|
if (data.has(`name`) && data.has(`provider`)) {
|
||||||
|
let user = data.get(`name`);
|
||||||
|
let provider = data.get(`provider`)!;
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
if (config.users.disallowed[provider].includes(user)) {
|
||||||
|
throw boom.unauthorized();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
return database.changeCount(channel, counter, delta);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
export default route;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue