diff --git a/src/middlewares/Authentication.ts b/src/middlewares/Authentication.ts
index 3d49d8e4..65d5a2cf 100644
--- a/src/middlewares/Authentication.ts
+++ b/src/middlewares/Authentication.ts
@@ -7,7 +7,7 @@ export const NO_AUTHORIZATION_ROUTES = ["/api/v8/auth/login", "/api/v8/auth/regi
declare global {
namespace Express {
interface Request {
- userid: any;
+ user_id: any;
token: any;
}
}
@@ -22,7 +22,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc
const decoded: any = await checkToken(req.headers.authorization);
req.token = decoded;
- req.userid = BigInt(decoded.id);
+ req.user_id = BigInt(decoded.id);
return next();
} catch (error) {
return next(new HTTPError(error.toString(), 400));
diff --git a/src/middlewares/RateLimit.ts b/src/middlewares/RateLimit.ts
index ab541b74..09d109e1 100644
--- a/src/middlewares/RateLimit.ts
+++ b/src/middlewares/RateLimit.ts
@@ -8,7 +8,7 @@ export function RateLimit({ count = 10, timespan = 1000 * 5, name = "/" }) {
// TODO: use new db mongoose models
/*
- let id = req.userid || getIpAdress(req);
+ let id = req.user_id || getIpAdress(req);
const limit: { count: number; start: number } = (await db.data.ratelimit.routes[name][id].get()) || {
count: 0,
diff --git a/src/routes/api/v8/auth/register.ts b/src/routes/api/v8/auth/register.ts
index ceb151af..99df82f1 100644
--- a/src/routes/api/v8/auth/register.ts
+++ b/src/routes/api/v8/auth/register.ts
@@ -170,7 +170,7 @@ router.post(
// @ts-ignore
const user: User = {
id: Snowflake.generate(),
- created_at: Date.now(),
+ created_at: new Date(),
username: adjusted_username,
discriminator,
avatar: null,
@@ -182,7 +182,7 @@ router.post(
flags: 0n, // TODO: generate default flags
hash: adjusted_password,
guilds: [],
- valid_tokens_since: Date.now(),
+ valid_tokens_since: new Date(),
user_settings: {
afk_timeout: 300,
allow_accessibility_detection: true,
diff --git a/src/routes/api/v8/channels/#channel_id/invites.ts b/src/routes/api/v8/channels/#channel_id/invites.ts
index 60ff4bb7..0e09c00c 100644
--- a/src/routes/api/v8/channels/#channel_id/invites.ts
+++ b/src/routes/api/v8/channels/#channel_id/invites.ts
@@ -12,7 +12,7 @@ import { getPermission, ChannelModel, InviteModel, InviteCreateEvent } from "fos
const router: Router = Router();
router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => {
- const usID = req.userid;
+ const usID = req.user_id;
const chID = BigInt(req.params.channel_id);
const channel = await ChannelModel.findOne({ id: chID }).exec();
@@ -33,7 +33,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
uses: 0,
max_uses: req.body.max_uses,
max_age: req.body.max_age,
- created_at: Date.now(),
+ created_at: new Date(),
guild_id: guID,
channel_id: chID,
inviter_id: usID,
diff --git a/src/routes/api/v8/guilds/#id/bans.ts b/src/routes/api/v8/guilds/#id/bans.ts
index aaae39cb..5133ee3c 100644
--- a/src/routes/api/v8/guilds/#id/bans.ts
+++ b/src/routes/api/v8/guilds/#id/bans.ts
@@ -29,14 +29,14 @@ router.get("/:user", async (req: Request, res: Response) => {
return res.json(ban);
});
-router.post("/:userid", check(BanCreateSchema), async (req: Request, res: Response) => {
+router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id);
- const banned_user_id = BigInt(req.params.userid);
+ const banned_user_id = BigInt(req.params.user_id);
const banned_user = await getPublicUser(banned_user_id);
- const perms = await getPermission(req.userid, guild_id);
+ const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("BAN_MEMBERS")) throw new HTTPError("You don't have the permission to ban members", 403);
- if (req.userid === banned_user_id) throw new HTTPError("You can't ban yourself", 400);
+ if (req.user_id === banned_user_id) throw new HTTPError("You can't ban yourself", 400);
await removeMember(banned_user_id, guild_id);
@@ -44,7 +44,7 @@ router.post("/:userid", check(BanCreateSchema), async (req: Request, res: Respon
user_id: banned_user_id,
guild_id: guild_id,
ip: getIpAdress(req),
- executor_id: req.userid,
+ executor_id: req.user_id,
reason: req.body.reason, // || otherwise empty
}).save();
@@ -60,15 +60,15 @@ router.post("/:userid", check(BanCreateSchema), async (req: Request, res: Respon
return res.json(ban).send();
});
-router.delete("/:userid", async (req: Request, res: Response) => {
+router.delete("/:user_id", async (req: Request, res: Response) => {
var guild_id = BigInt(req.params.id);
- var banned_user_id = BigInt(req.params.userid);
+ var banned_user_id = BigInt(req.params.user_id);
const banned_user = await getPublicUser(banned_user_id);
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
- const perms = await getPermission(req.userid, guild.id);
+ const perms = await getPermission(req.user_id, guild.id);
if (!perms.has("BAN_MEMBERS")) {
throw new HTTPError("No permissions", 403);
}
diff --git a/src/routes/api/v8/guilds/#id/index.ts b/src/routes/api/v8/guilds/#id/index.ts
index 09f2a5c2..e86d9416 100644
--- a/src/routes/api/v8/guilds/#id/index.ts
+++ b/src/routes/api/v8/guilds/#id/index.ts
@@ -24,7 +24,7 @@ router.get("/", async (req: Request, res: Response) => {
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild does not exist", 404);
- const member = await MemberModel.findOne({ guild_id: guild_id, id: req.userid }, "id").exec();
+ const member = await MemberModel.findOne({ guild_id: guild_id, id: req.user_id }, "id").exec();
if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401);
return res.json(guild);
@@ -37,7 +37,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("This guild does not exist", 404);
- const perms = await getPermission(req.userid, guild_id);
+ const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("MANAGE_GUILD")) throw new HTTPError("You do not have the MANAGE_GUILD permission", 401);
await GuildModel.updateOne({ id: guild_id }, body).exec();
@@ -49,7 +49,7 @@ router.delete("/", async (req: Request, res: Response) => {
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.userid) throw new HTTPError("You are not the owner of this guild", 401);
+ if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
await emitEvent({
event: "GUILD_DELETE",
diff --git a/src/routes/api/v8/guilds/index.ts b/src/routes/api/v8/guilds/index.ts
index 7179ea9e..319184ad 100644
--- a/src/routes/api/v8/guilds/index.ts
+++ b/src/routes/api/v8/guilds/index.ts
@@ -13,7 +13,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
const body = req.body as GuildCreateSchema;
const { maxGuilds } = Config.get().limits.user;
- const user = await getPublicUser(req.userid, { guilds: true });
+ const user = await getPublicUser(req.user_id, { guilds: true });
if (user.guilds.length >= maxGuilds) {
throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403);
@@ -23,7 +23,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
const guild: Guild = {
name: body.name,
region: body.region || "en-US",
- owner_id: req.userid,
+ owner_id: req.user_id,
icon: undefined,
afk_channel_id: undefined,
afk_timeout: 300,
@@ -73,7 +73,7 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
tags: null,
}).save(),
]);
- await addMember(req.userid, guild_id, { guild });
+ await addMember(req.user_id, guild_id, { guild });
res.status(201).json({ id: guild.id });
});
diff --git a/src/routes/api/v8/users/@me/guilds.ts b/src/routes/api/v8/users/@me/guilds.ts
index 6c3b0925..a2a64ce6 100644
--- a/src/routes/api/v8/users/@me/guilds.ts
+++ b/src/routes/api/v8/users/@me/guilds.ts
@@ -7,7 +7,7 @@ import { getPublicUser } from "../../../../../util/User";
const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
- const user = await UserModel.findOne({ id: req.userid }, { guilds: true }).exec();
+ const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec();
if (!user) throw new HTTPError("User not found", 404);
var guildIDs = user.guilds || [];
@@ -21,18 +21,18 @@ router.delete("/:id", async (req: Request, res: Response) => {
const guild = await GuildModel.findOne({ id: guildID }).exec();
if (!guild) throw new HTTPError("Guild doesn't exist", 404);
- if (guild.owner_id === req.userid) throw new HTTPError("You can't leave your own guild", 400);
+ if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400);
- await MemberModel.deleteOne({ id: req.userid, guild_id: guildID }).exec();
- await UserModel.updateOne({ id: req.userid }, { $pull: { guilds: guildID } }).exec();
- const user = await getPublicUser(req.userid);
+ await MemberModel.deleteOne({ id: req.user_id, guild_id: guildID }).exec();
+ await UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guildID } }).exec();
+ const user = await getPublicUser(req.user_id);
await emitEvent({
event: "GUILD_DELETE",
data: {
id: guildID,
},
- user_id: req.userid,
+ user_id: req.user_id,
} as GuildDeleteEvent);
await emitEvent({
diff --git a/src/routes/api/v8/users/@me/index.ts b/src/routes/api/v8/users/@me/index.ts
index 4307ef76..32877dcc 100644
--- a/src/routes/api/v8/users/@me/index.ts
+++ b/src/routes/api/v8/users/@me/index.ts
@@ -6,7 +6,7 @@ const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
// TODO: user projection
- const user = await UserModel.findOne({ id: req.userid }).exec();
+ const user = await UserModel.findOne({ id: req.user_id }).exec();
if (!user) throw new HTTPError("User not found", 404);
res.json(user);
diff --git a/src/schema/Message.ts b/src/schema/Message.ts
index 80897f9d..4ae6c136 100644
--- a/src/schema/Message.ts
+++ b/src/schema/Message.ts
@@ -1,27 +1,69 @@
+import { Embed, EmbedImage } from "fosscord-server-util";
+import { Length } from "../util/instanceOf";
+
export const MessageCreateSchema = {
- content: String,
- nonce: Number,
- tts: Boolean,
- embed: {},
- allowed_mentions: [],
- message_reference: {
+ $content: new Length(String, 0, 2000),
+ $nonce: String,
+ $tts: Boolean,
+ $embed: {
+ $title: new Length(String, 0, 256), //title of embed
+ $type: String, // type of embed (always "rich" for webhook embeds)
+ $description: new Length(String, 0, 2048), // description of embed
+ $url: String, // url of embed
+ $timestamp: String, // ISO8601 timestamp
+ $color: Number, // color code of the embed
+ $footer: {
+ text: new Length(String, 0, 2048),
+ icon_url: String,
+ proxy_icon_url: String,
+ }, // footer object footer information
+ $image: EmbedImage, // image object image information
+ $thumbnail: EmbedImage, // thumbnail object thumbnail information
+ $video: EmbedImage, // video object video information
+ $provider: {
+ name: String,
+ url: String,
+ }, // provider object provider information
+ $author: {
+ name: new Length(String, 0, 256),
+ url: String,
+ icon_url: String,
+ proxy_icon_url: String,
+ }, // author object author information
+ $fields: new Length(
+ [
+ {
+ name: new Length(String, 0, 256),
+ value: new Length(String, 0, 1024),
+ $inline: Boolean,
+ },
+ ],
+ 0,
+ 25
+ ),
+ },
+ $allowed_mentions: [],
+ $message_reference: {
message_id: BigInt,
channel_id: BigInt,
- guild_id: BigInt,
- fail_if_not_exists: Boolean,
+ $guild_id: BigInt,
+ $fail_if_not_exists: Boolean,
},
+ $payload_json: String,
+ $file: Object,
};
export interface MessageCreateSchema {
- content: string;
- nonce: number;
- tts: boolean;
- embed: {};
- allowed_mentions: [];
- message_reference: {
+ content?: string;
+ nonce?: string;
+ tts?: boolean;
+ embed?: Embed & { timestamp: string };
+ allowed_mentions?: [];
+ message_reference?: {
message_id: bigint;
channel_id: bigint;
- guild_id: bigint;
+ guild_id?: bigint;
fail_if_not_exists: boolean;
};
+ payload_json?: string;
}
diff --git a/src/util/Constants.ts b/src/util/Constants.ts
index 1b0e8dbf..f3a8dd67 100644
--- a/src/util/Constants.ts
+++ b/src/util/Constants.ts
@@ -32,9 +32,9 @@ export const Endpoints = {
Emoji: (emojiID: string, format = "png") => `${root}/emojis/${emojiID}.${format}`,
Asset: (name: string) => `${root}/assets/${name}`,
DefaultAvatar: (discriminator: string) => `${root}/embed/avatars/${discriminator}.png`,
- Avatar: (userID: string, hash: string, format = "webp", size: number, dynamic = false) => {
+ Avatar: (user_id: string, hash: string, format = "webp", size: number, dynamic = false) => {
if (dynamic) format = hash.startsWith("a_") ? "gif" : format;
- return makeImageUrl(`${root}/avatars/${userID}/${hash}`, { format, size });
+ return makeImageUrl(`${root}/avatars/${user_id}/${hash}`, { format, size });
},
Banner: (guildID: string, hash: string, format = "webp", size: number) =>
makeImageUrl(`${root}/banners/${guildID}/${hash}`, { format, size }),
@@ -42,27 +42,18 @@ export const Endpoints = {
if (dynamic) format = hash.startsWith("a_") ? "gif" : format;
return makeImageUrl(`${root}/icons/${guildID}/${hash}`, { format, size });
},
- AppIcon: (
- clientID: string,
- hash: string,
- { format = "webp", size }: { format?: string; size?: number } = {}
- ) => makeImageUrl(`${root}/app-icons/${clientID}/${hash}`, { size, format }),
- AppAsset: (
- clientID: string,
- hash: string,
- { format = "webp", size }: { format?: string; size?: number } = {}
- ) => makeImageUrl(`${root}/app-assets/${clientID}/${hash}`, { size, format }),
+ AppIcon: (clientID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) =>
+ makeImageUrl(`${root}/app-icons/${clientID}/${hash}`, { size, format }),
+ AppAsset: (clientID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) =>
+ makeImageUrl(`${root}/app-assets/${clientID}/${hash}`, { size, format }),
GDMIcon: (channelID: string, hash: string, format = "webp", size: number) =>
makeImageUrl(`${root}/channel-icons/${channelID}/${hash}`, { size, format }),
Splash: (guildID: string, hash: string, format = "webp", size: number) =>
makeImageUrl(`${root}/splashes/${guildID}/${hash}`, { size, format }),
DiscoverySplash: (guildID: string, hash: string, format = "webp", size: number) =>
makeImageUrl(`${root}/discovery-splashes/${guildID}/${hash}`, { size, format }),
- TeamIcon: (
- teamID: string,
- hash: string,
- { format = "webp", size }: { format?: string; size?: number } = {}
- ) => makeImageUrl(`${root}/team-icons/${teamID}/${hash}`, { size, format }),
+ TeamIcon: (teamID: string, hash: string, { format = "webp", size }: { format?: string; size?: number } = {}) =>
+ makeImageUrl(`${root}/team-icons/${teamID}/${hash}`, { size, format }),
};
},
invite: (root: string, code: string) => `${root}/${code}`,
@@ -346,9 +337,7 @@ export const MessageTypes = [
* * REPLY
* @typedef {string} SystemMessageType
*/
-export const SystemMessageTypes = MessageTypes.filter(
- (type: string | null) => type && type !== "DEFAULT" && type !== "REPLY"
-);
+export const SystemMessageTypes = MessageTypes.filter((type: string | null) => type && type !== "DEFAULT" && type !== "REPLY");
/**
* <info>Bots cannot set a `CUSTOM_STATUS`, it is only for custom statuses received from users</info>
diff --git a/src/util/Member.ts b/src/util/Member.ts
index 2df34073..319eab60 100644
--- a/src/util/Member.ts
+++ b/src/util/Member.ts
@@ -43,7 +43,7 @@ export async function addMember(user_id: bigint, guild_id: bigint, cache?: { gui
guild_id: guild_id,
nick: undefined,
roles: [guild_id], // @everyone role
- joined_at: Date.now(),
+ joined_at: new Date(),
premium_since: undefined,
deaf: false,
mute: false,
diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts
index bbb30c12..4f30bd46 100644
--- a/src/util/instanceOf.ts
+++ b/src/util/instanceOf.ts
@@ -51,7 +51,7 @@ export class Length {
constructor(public type: any, public min: number, public max: number) {}
check(value: string) {
- if (typeof value === "string") return value.length >= this.min && value.length <= this.max;
+ if (typeof value === "string" || Array.isArray(value)) return value.length >= this.min && value.length <= this.max;
if (typeof value === "number" || typeof value === "bigint") return value >= this.min && value <= this.max;
return false;
}
|