1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 04:32:35 +01:00

oapi: invites

This commit is contained in:
Puyodead1 2023-03-24 21:36:50 -04:00
parent 5c0a6f4e55
commit 3a40254ca5
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
2 changed files with 117 additions and 38 deletions

View File

@ -7779,8 +7779,35 @@
}
],
"responses": {
"default": {
"description": "No description available"
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Invite"
}
}
}
},
"401": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/APIErrorResponse"
}
}
}
},
"404": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/APIErrorResponse"
}
}
}
}
},
"parameters": [

View File

@ -16,35 +16,64 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Router, Request, Response } from "express";
import { route } from "@spacebar/api";
import {
emitEvent,
getPermission,
Guild,
Invite,
InviteDeleteEvent,
User,
PublicInviteRelation,
User,
} from "@spacebar/util";
import { route } from "@spacebar/api";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router: Router = Router();
router.get("/:code", route({}), async (req: Request, res: Response) => {
const { code } = req.params;
router.get(
"/:code",
route({
responses: {
"200": {
body: "Invite",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await Invite.findOneOrFail({
where: { code },
relations: PublicInviteRelation,
});
const invite = await Invite.findOneOrFail({
where: { code },
relations: PublicInviteRelation,
});
res.status(200).send(invite);
});
res.status(200).send(invite);
},
);
router.post(
"/:code",
route({ right: "USE_MASS_INVITES" }),
route({
right: "USE_MASS_INVITES",
responses: {
"200": {
body: "Invite",
},
401: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { code } = req.params;
const { guild_id } = await Invite.findOneOrFail({
@ -75,33 +104,56 @@ router.post(
);
// * cant use permission of route() function because path doesn't have guild_id/channel_id
router.delete("/:code", route({}), async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await Invite.findOneOrFail({ where: { code } });
const { guild_id, channel_id } = invite;
router.delete(
"/:code",
route({
responses: {
"200": {
body: "Invite",
},
401: {
body: "APIErrorResponse",
},
404: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await Invite.findOneOrFail({ where: { code } });
const { guild_id, channel_id } = invite;
const permission = await getPermission(req.user_id, guild_id, channel_id);
if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS"))
throw new HTTPError(
"You missing the MANAGE_GUILD or MANAGE_CHANNELS permission",
401,
const permission = await getPermission(
req.user_id,
guild_id,
channel_id,
);
await Promise.all([
Invite.delete({ code }),
emitEvent({
event: "INVITE_DELETE",
guild_id: guild_id,
data: {
channel_id: channel_id,
guild_id: guild_id,
code: code,
},
} as InviteDeleteEvent),
]);
if (
!permission.has("MANAGE_GUILD") &&
!permission.has("MANAGE_CHANNELS")
)
throw new HTTPError(
"You missing the MANAGE_GUILD or MANAGE_CHANNELS permission",
401,
);
res.json({ invite: invite });
});
await Promise.all([
Invite.delete({ code }),
emitEvent({
event: "INVITE_DELETE",
guild_id: guild_id,
data: {
channel_id: channel_id,
guild_id: guild_id,
code: code,
},
} as InviteDeleteEvent),
]);
res.json({ invite: invite });
},
);
export default router;