1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-25 03:33:33 +01:00

🎨 refactor/format

This commit is contained in:
Flam3rboy 2021-05-21 22:12:21 +02:00
parent f435d33da1
commit d48b286e4b
12 changed files with 412 additions and 553 deletions

750
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,9 +7,8 @@
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"start": "npm run build:util && npm run build && node dist/start",
"start": "npm run build && node dist/start",
"build": "tsc -b .",
"build:util": "tsc -b ./node_modules/@fosscord/server-util/",
"postinstall": "patch-package",
"dev": "tsnd --respawn src/start.ts"
},
@ -31,9 +30,9 @@
},
"homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": {
"@fosscord/server-util": "^1.1.5",
"@fosscord/server-util": "^1.2.2",
"@types/jest": "^26.0.22",
"bcrypt": "^5.0.0",
"bcrypt": "^5.0.1",
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",

View File

@ -11,10 +11,12 @@ router.delete("/", async (req, res) => {
const { message_id, channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true });
if (!channel) throw new HTTPError("Channel doesn't exist", 404);
if (!channel) throw new HTTPError("Channel not found", 404);
const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec();
if (!message) throw new HTTPError("Message not found", 404);
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
permission.hasThrow("MANAGE_MESSAGES");
if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES");
await MessageModel.deleteOne({ id: message_id }).exec();
@ -25,8 +27,8 @@ router.delete("/", async (req, res) => {
data: {
id: message_id,
channel_id,
guild_id: channel.guild_id,
},
guild_id: channel.guild_id
}
} as MessageDeleteEvent);
res.sendStatus(204);

View File

@ -13,7 +13,7 @@ export default router;
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
router.post("/", check({ messages: [String] }), async (req, res) => {
const channel_id = req.params.channel_id;
const { channel_id } = req.params;
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
@ -31,7 +31,7 @@ router.post("/", check({ messages: [String] }), async (req, res) => {
await emitEvent({
event: "MESSAGE_DELETE_BULK",
channel_id,
data: { ids: messages, channel_id, guild_id: channel.guild_id },
data: { ids: messages, channel_id, guild_id: channel.guild_id }
} as MessageDeleteBulkEvent);
res.sendStatus(204);

View File

@ -1,5 +1,14 @@
import { Router } from "express";
import { ChannelCreateEvent, ChannelModel, ChannelType, GuildModel, Snowflake, toObject, ChannelUpdateEvent } from "@fosscord/server-util";
import {
ChannelCreateEvent,
ChannelModel,
ChannelType,
GuildModel,
Snowflake,
toObject,
ChannelUpdateEvent,
AnyChannel
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel";
import { emitEvent } from "../../../util/Event";
@ -45,7 +54,8 @@ router.post("/", check(ChannelModifySchema), async (req, res) => {
...body,
id: Snowflake.generate(),
created_at: new Date(),
guild_id
guild_id,
recipients: null
};
await new ChannelModel(channel).save();

View File

@ -0,0 +1,45 @@
import {
ChannelModel,
EmojiModel,
GuildDeleteEvent,
GuildModel,
InviteModel,
MessageModel,
RoleModel,
UserModel
} from "@fosscord/server-util";
import { Router, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
const router = Router();
// discord prefixes this route with /delete instead of using the delete method
// docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild
router.post("/", async (req: Request, res: Response) => {
var { guild_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
if (!guild) throw new HTTPError("This guild does not exist", 404);
if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
await emitEvent({
event: "GUILD_DELETE",
data: {
id: guild_id
},
guild_id: guild_id
} as GuildDeleteEvent);
await GuildModel.deleteOne({ id: guild_id }).exec();
await UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec();
await RoleModel.deleteMany({ guild_id }).exec();
await ChannelModel.deleteMany({ guild_id }).exec();
await EmojiModel.deleteMany({ guild_id }).exec();
await InviteModel.deleteMany({ guild_id }).exec();
await MessageModel.deleteMany({ guild_id }).exec();
return res.sendStatus(204);
});
export default router;

View File

@ -11,7 +11,7 @@ import {
MessageModel,
RoleModel,
toObject,
UserModel,
UserModel
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { GuildUpdateSchema } from "../../../schema/Guild";
@ -54,41 +54,13 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
return res.send(data);
});
// discord prefixes this route with /delete instead of using the delete method
// docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild
router.post("/delete", async (req: Request, res: Response) => {
var { guild_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
if (!guild) throw new HTTPError("This guild does not exist", 404);
if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
await emitEvent({
event: "GUILD_DELETE",
data: {
id: guild_id,
},
guild_id: guild_id,
} as GuildDeleteEvent);
await GuildModel.deleteOne({ id: guild_id }).exec();
await UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec();
await RoleModel.deleteMany({ guild_id }).exec();
await ChannelModel.deleteMany({ guild_id }).exec();
await EmojiModel.deleteMany({ guild_id }).exec();
await InviteModel.deleteMany({ guild_id }).exec();
await MessageModel.deleteMany({ guild_id }).exec();
return res.sendStatus(204);
});
router.get("/vanity-url", async (req: Request, res: Response) => {
const { guild_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild does not exist", 404);
if(!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204)
if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204);
return res.json(guild.vanity_url);
});

View File

@ -15,14 +15,13 @@ router.get("/", async (req: Request, res: Response) => {
if (!guild) throw new HTTPError("Guild not found", 404);
var roles = await RoleModel.find({ guild_id: guild_id }).exec();
if(!roles) res.send("No roles");
if (!roles) res.send("No roles");
return res.json(toObject(roles));
});
router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
const body = req.body as RoleCreateSchema;
const guild_id = req.params.guild_id;
const body = req.body as RoleCreateSchema;
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
@ -32,8 +31,7 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) =>
const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("MANAGE_ROLES"))
throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
const role_id = Snowflake.generate();
@ -43,17 +41,16 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) =>
guild_id: guild_id,
managed: false,
position: 0,
tags: null,
}
tags: null
};
const roleNew = await new RoleModel(role).save();
res.json(toObject(roleNew)).send();
});
router.delete("/:role_id", async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
const { role_id } = req.params;
const guild_id = req.params.guild_id;
const { role_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
@ -64,8 +61,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => {
const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("MANAGE_ROLES"))
throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
await RoleModel.findOneAndDelete({
id: role_id,
@ -75,10 +71,11 @@ router.delete("/:role_id", async (req: Request, res: Response) => {
res.send("Deleted");
});
// TODO: check role hierarchy
router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => {
const guild_id = req.params.guild_id;
const { role_id } = req.params;
const guild_id = req.params.guild_id;
const { role_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
@ -87,20 +84,18 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
const user = await UserModel.findOne({ id: req.user_id }).exec();
if (!user) throw new HTTPError("User not found", 404);
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
if (!role) throw new HTTPError("role not found", 404);
const perms = await getPermission(req.user_id, guild_id);
perms.hasThrow("MANAGE_ROLES");
if (!perms.has("MANAGE_ROLES"))
throw new HTTPError("You missing the MANAGE_ROLES permission", 401);
const role = await RoleModel.findOneAndUpdate(
{
id: role_id,
guild_id: guild_id
},
...req.body
).exec();
var roleObj = await RoleModel.findOneAndUpdate({
id: role_id, guild_id: guild_id
}, ...req.body).exec();
res.json(toObject(roleObj)).send();
res.json(toObject(role));
});
export default router;

View File

@ -0,0 +1,18 @@
import { GuildModel } from "@fosscord/server-util";
import { Router, Request, Response } from "express";
import { HTTPError } from "lambert-server";
const router = Router();
router.get("/", async (req: Request, res: Response) => {
const { guild_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild does not exist", 404);
if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204);
return res.json({ vanity_ur: guild.vanity_url });
});
export default router;

View File

@ -1,19 +1,31 @@
import { Router, Request, Response } from "express";
import { getPermission, InviteModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { addMember } from "../../util/Member";
const router: Router = Router();
router.get("/:invite_code", async (req: Request, res: Response) => {
const { invite_code: code } = req.params;
router.get("/:code", async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await InviteModel.findOne({ code }).exec();
if (!invite) throw new HTTPError("Unknown Invite", 404);
res.status(200).send({ invite: toObject(invite) });
res.status(200).send(toObject(invite));
});
router.delete("/:invite_code", async (req: Request, res: Response) => {
const { invite_code: code } = req.params;
router.post("/:code", async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }).exec();
if (!invite) throw new HTTPError("Unknown Invite", 404);
await addMember(req.user_id, invite.guild_id);
res.status(200).send(toObject(invite));
});
router.delete("/:code", async (req: Request, res: Response) => {
const { code } = req.params;
const invite = await InviteModel.findOne({ code }).exec();
if (!invite) throw new HTTPError("Unknown Invite", 404);

View File

@ -0,0 +1,10 @@
import { Router } from "express";
const router = Router();
router.get("/", (req, res) => {
// TODO:
res.status(200).send({ user_affinities: [], inverse_user_affinities: [] });
});
export default router;

View File

@ -1,11 +1,11 @@
{
"include": ["src/**/*.ts", "src/test/rethink_test.ts.disabled"],
"include": ["src/**/*.ts"],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": ["ES2020"] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,