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

move Schema from util to api

This commit is contained in:
Flam3rboy 2021-02-10 22:03:40 +01:00
parent 91f2e73da0
commit d3427b42ea
4 changed files with 156 additions and 114 deletions

36
package-lock.json generated
View File

@ -13,7 +13,7 @@
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-validator": "^6.9.2",
"fosscord-server-util": "git+https://github.com/fosscord/fosscord-server-util.git",
"fosscord-server-util": "github:fosscord/fosscord-server-util",
"i18next": "^19.8.5",
"i18next-http-middleware": "^3.1.0",
"i18next-node-fs-backend": "^2.1.3",
@ -777,25 +777,15 @@
}
},
"node_modules/fosscord-server-util": {
"resolved": "git+ssh://git@github.com/fosscord/fosscord-server-util.git#def655c738c5968af1722e5d32c5e8ec83327527",
"version": "1.0.0",
"resolved": "git+ssh://git@github.com/fosscord/fosscord-server-util.git#3d54ed476240702812caf0b6e1ce94bfb0329cb5",
"license": "ISC",
"dependencies": {
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.7",
"missing-native-js-functions": "^1.2.2"
}
},
"node_modules/fosscord-server-util/node_modules/lambert-db": {
"version": "1.1.7",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.7.tgz",
"integrity": "sha512-OJzXySr/eISttGbkB3uE4ybJW8H/eO1lKWzCt0R38OBuMKnVBfCDO3yOQCXiIzjk9kXOggL4vsKy3OujLDXoDg==",
"dependencies": {
"missing-native-js-functions": "^1.1.8",
"mongodb": "^3.6.2",
"mongodb-memory-server": "^6.9.2",
"mongoose": "^5.11.8",
"rethinkdb-ts": "^2.4.5"
}
},
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
@ -2997,26 +2987,12 @@
"integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
},
"fosscord-server-util": {
"version": "git+ssh://git@github.com/fosscord/fosscord-server-util.git#def655c738c5968af1722e5d32c5e8ec83327527",
"from": "fosscord-server-util@git+https://github.com/fosscord/fosscord-server-util.git",
"version": "git+ssh://git@github.com/fosscord/fosscord-server-util.git#3d54ed476240702812caf0b6e1ce94bfb0329cb5",
"from": "fosscord-server-util@github:fosscord/fosscord-server-util",
"requires": {
"jsonwebtoken": "^8.5.1",
"lambert-db": "^1.1.7",
"missing-native-js-functions": "^1.2.2"
},
"dependencies": {
"lambert-db": {
"version": "1.1.7",
"resolved": "https://registry.npmjs.org/lambert-db/-/lambert-db-1.1.7.tgz",
"integrity": "sha512-OJzXySr/eISttGbkB3uE4ybJW8H/eO1lKWzCt0R38OBuMKnVBfCDO3yOQCXiIzjk9kXOggL4vsKy3OujLDXoDg==",
"requires": {
"missing-native-js-functions": "^1.1.8",
"mongodb": "^3.6.2",
"mongodb-memory-server": "^6.9.2",
"mongoose": "^5.11.8",
"rethinkdb-ts": "^2.4.5"
}
}
}
},
"fresh": {

View File

@ -24,7 +24,7 @@
"body-parser": "^1.19.0",
"express": "^4.17.1",
"express-validator": "^6.9.2",
"fosscord-server-util": "git+https://github.com/fosscord/fosscord-server-util.git",
"fosscord-server-util": "github:fosscord/fosscord-server-util",
"i18next": "^19.8.5",
"i18next-http-middleware": "^3.1.0",
"i18next-node-fs-backend": "^2.1.3",

View File

@ -1,7 +1,8 @@
import { Router, Request, Response } from "express";
import { db, GuildSchema, Guild, Snowflake } from "fosscord-server-util";
import { db, Guild, Snowflake } from "fosscord-server-util";
import { HTTPError } from "lambert-server";
import { check } from "./../../../../util/instanceOf";
import { GuildCreateSchema, GuildGetSchema, GuildUpdateSchema } from "../../../../schema/Guild";
const router: Router = Router();
@ -12,75 +13,38 @@ router.get("/:id", async (req: Request, res: Response) => {
throw new HTTPError("you arent a member of the guild you are trying to access", 401);
}
const guild = await db.data.guilds({ id: req.params.id }).get({
id: true,
name: true,
icon: true,
// icon_hash: true,
splash: true,
discovery_splash: true,
owner: true,
owner_id: true,
permissions: true,
region: true,
afk_channel_id: true,
afk_timeout: true,
widget_enabled: true,
widget_channel_id: true,
verification_level: true,
default_message_notifications: true,
explicit_content_filter: true,
roles: true,
emojis: true,
features: true,
mfa_level: true,
application_id: true,
system_channel_id: true,
system_channel_flags: true,
rules_channel_id: true,
joined_at: true,
// large: true,
// unavailable: true,
member_count: true,
// voice_states: true,
// members: true,
// channels: true,
// presences: true,
max_presences: true,
max_members: true,
vanity_url_code: true,
description: true,
banner: true,
premium_tier: true,
premium_subscription_count: true,
preferred_locale: true,
public_updates_channel_id: true,
max_video_channel_users: true,
approximate_member_count: true,
approximate_presence_count: true,
// welcome_screen: true,
});
const guild = await db.data.guilds({ id: req.params.id }).get(GuildGetSchema);
return res.json(guild);
});
// TODO: add addGuildSchema & createGuildSchema
// router.put("/:id", check(GuildSchema), async (req: Request, res: Response) => {});
router.patch("/:id", check(GuildUpdateSchema), async (req: Request, res: Response) => {
// TODO: check permission of member
const body = req.body as GuildUpdateSchema;
const guild = await db.data.guilds({ id: req.params.id }).get({ id: true });
if (!guild) throw new HTTPError("This guild doesnt exist", 404);
throw "not finished";
});
// // TODO: finish POST route
router.post("/", check(GuildSchema), async (req: Request, res: Response) => {
const body = req.body as GuildSchema;
router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => {
const body = req.body as GuildCreateSchema;
const guildID = Snowflake.generate();
const guild: Guild = {
// name: undefined,
// owner: undefined,
...body, // ! contains name & icon values
owner_id: req.userid,
afk_channel_id: undefined,
afk_timeout: undefined,
afk_timeout: 300,
application_id: undefined,
approximate_member_count: undefined,
approximate_presence_count: undefined,
banner: undefined,
channels: [],
default_message_notifications: undefined,
description: undefined,
splash: undefined,
discovery_splash: undefined,
emojis: [],
explicit_content_filter: undefined,
@ -89,25 +53,44 @@ router.post("/", check(GuildSchema), async (req: Request, res: Response) => {
id: guildID,
// joined_at: undefined,
large: undefined,
max_members: undefined,
max_members: 250000,
max_presences: undefined,
max_video_channel_users: undefined,
member_count: undefined,
members: undefined,
mfa_level: undefined,
// name: undefined,
owner_id: req.userid, // ! important
// owner: undefined,
permissions: undefined,
preferred_locale: undefined,
premium_subscription_count: undefined,
premium_tier: undefined,
max_video_channel_users: 25,
member_count: 0,
presence_count: 0,
members: [
{
id: req.userid,
roles: [], // @everyone role is not explicitly set, the client and server automatically assumes it
joined_at: Date.now(),
nick: undefined,
premium_since: undefined,
deaf: false,
mute: false,
pending: false,
permissions: 8n, // value will be computed if a role is changed
},
],
mfa_level: 0,
preferred_locale: "en-US",
premium_subscription_count: 0,
premium_tier: 0,
presences: [],
public_updates_channel_id: undefined,
region: undefined,
roles: [],
roles: [
{
color: 0,
hoist: false,
name: "@everyone",
permissions: 0n,
id: guildID,
managed: true, // ? discord set this to true,
mentionable: false,
position: 0,
},
],
rules_channel_id: undefined,
splash: undefined,
system_channel_flags: undefined,
system_channel_id: undefined,
unavailable: undefined,
@ -116,7 +99,7 @@ router.post("/", check(GuildSchema), async (req: Request, res: Response) => {
voice_states: [],
welcome_screen: [],
widget_channel_id: undefined,
widget_enabled: undefined,
widget_enabled: false,
};
try {
@ -132,19 +115,10 @@ router.delete("/:id", async (req: Request, res: Response) => {
const guild = await db.data.guilds({ id: guildID }).get({ owner_id: true });
if (!guild) {
throw new HTTPError("This guild doesnt exist", 404);
}
if (!guild) throw new HTTPError("This guild doesnt exist", 404);
if (guild.owner_id !== req.userid) throw new HTTPError("You arent the owner of this guild", 401);
if (guild.owner_id !== req.userid) {
throw new HTTPError("You arent the owner of this guild", 401);
}
try {
await db.data.guilds({ id: guildID }).delete();
} catch (error) {
throw new HTTPError(`Couldnt delete guild: ${error}`, 500);
}
await db.data.guilds({ id: guildID }).delete();
return res.status(204);
});

92
src/schema/Guild.ts Normal file
View File

@ -0,0 +1,92 @@
import { Length } from "../util/instanceOf";
export const GuildCreateSchema = {
name: new Length(String, 2, 100), // ! 2-100 chars
$region: String, // auto complete voice region of the user
};
export interface GuildCreateSchema {
name: string;
region?: string;
}
export const GuildUpdateSchema = {
...GuildCreateSchema,
$banner: String,
$splash: String,
$description: String,
$features: [String],
$icon: String,
$verification_level: Number,
$default_message_notifications: Number,
$system_channel_flags: Number,
$system_channel_id: BigInt,
$explicit_content_filter: Number,
$public_updates_channel_id: BigInt,
$afk_timeout: Number,
$afk_channel_id: BigInt,
};
export interface GuildUpdateSchema extends GuildCreateSchema {
banner?: string;
splash?: string;
description?: string;
features?: [string];
icon?: string;
verification_level?: number;
default_message_notifications?: number;
system_channel_flags?: number;
system_channel_id?: bigint;
explicit_content_filter?: number;
public_updates_channel_id?: bigint;
afk_timeout?: number;
afk_channel_id?: bigint;
}
export const GuildGetSchema = {
id: true,
name: true,
icon: true,
splash: true,
discovery_splash: true,
owner: true,
owner_id: true,
permissions: true,
region: true,
afk_channel_id: true,
afk_timeout: true,
widget_enabled: true,
widget_channel_id: true,
verification_level: true,
default_message_notifications: true,
explicit_content_filter: true,
roles: true,
emojis: true,
features: true,
mfa_level: true,
application_id: true,
system_channel_id: true,
system_channel_flags: true,
rules_channel_id: true,
joined_at: true,
// large: true,
// unavailable: true,
member_count: true,
// voice_states: true,
// members: true,
// channels: true,
// presences: true,
max_presences: true,
max_members: true,
vanity_url_code: true,
description: true,
banner: true,
premium_tier: true,
premium_subscription_count: true,
preferred_locale: true,
public_updates_channel_id: true,
max_video_channel_users: true,
approximate_member_count: true,
approximate_presence_count: true,
// welcome_screen: true,
};