rename req.userid -> req.user_id

This commit is contained in:
Flam3rboy 2021-03-08 18:40:37 +01:00
parent 56c9e43777
commit 6a1e0594dc
15 changed files with 98 additions and 65 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -36,6 +36,7 @@
"missing-native-js-functions": "^1.2.4",
"mongodb": "^3.6.4",
"mongoose-long": "^0.3.2",
"multer": "^1.4.2",
"patch-package": "^6.2.2"
},
"devDependencies": {
@ -43,6 +44,7 @@
"@types/express": "^4.17.9",
"@types/i18next-node-fs-backend": "^2.1.0",
"@types/jsonwebtoken": "^8.5.0",
"@types/multer": "^1.4.5",
"@types/node": "^14.14.22",
"@types/node-fetch": "^2.5.7",
"0x": "^4.10.2",

View File

@ -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));

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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);
}

View File

@ -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",

View File

@ -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 });
});

View File

@ -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({

View File

@ -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);

View File

@ -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;
}

View File

@ -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>

View File

@ -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,

View File

@ -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;
}