🎨 Convert id bigint to string

This commit is contained in:
Flam3rboy 2021-04-06 18:02:10 +02:00
parent 79894e6f27
commit 7089287016
18 changed files with 83 additions and 63 deletions

View File

@ -29,7 +29,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc
const decoded: any = await checkToken(req.headers.authorization); const decoded: any = await checkToken(req.headers.authorization);
req.token = decoded; req.token = decoded;
req.user_id = BigInt(decoded.id); req.user_id = decoded.id;
return next(); return next();
} catch (error) { } catch (error) {
return next(new HTTPError(error.toString(), 400)); return next(new HTTPError(error.toString(), 400));

View File

@ -25,12 +25,20 @@ router.post(
const query: any[] = [{ phone: login }]; const query: any[] = [{ phone: login }];
if (email) query.push({ email }); if (email) query.push({ email });
// * MongoDB Specific query for user with same email or phone number
const user = await UserModel.findOne( const user = await UserModel.findOne(
{ {
$or: query, $or: query,
}, },
`user_data.hash id user_settings.locale user_settings.theme` {
id: true,
user_settings: {
locale: true,
theme: true,
},
user_data: {
hash: true,
},
}
).exec(); ).exec();
if (!user) { if (!user) {
@ -57,13 +65,13 @@ router.post(
} }
); );
export async function generateToken(id: bigint) { export async function generateToken(id: string) {
const iat = Math.floor(Date.now() / 1000); const iat = Math.floor(Date.now() / 1000);
const algorithm = "HS256"; const algorithm = "HS256";
return new Promise((res, rej) => { return new Promise((res, rej) => {
jwt.sign( jwt.sign(
{ id: `${id}`, iat }, { id: id, iat },
Config.get().security.jwtSecret, Config.get().security.jwtSecret,
{ {
algorithm, algorithm,
@ -80,7 +88,6 @@ export async function generateToken(id: bigint) {
* POST /auth/login * POST /auth/login
* @argument { login: "email@gmail.com", password: "cleartextpassword", undelete: false, captcha_key: null, login_source: null, gift_code_sku_id: null, } * @argument { login: "email@gmail.com", password: "cleartextpassword", undelete: false, captcha_key: null, login_source: null, gift_code_sku_id: null, }
* MFA required: * MFA required:
* @returns {"token": null, "mfa": true, "sms": true, "ticket": "SOME TICKET JWT TOKEN"} * @returns {"token": null, "mfa": true, "sms": true, "ticket": "SOME TICKET JWT TOKEN"}

View File

@ -166,10 +166,9 @@ router.post(
} }
// TODO: save date_of_birth // TODO: save date_of_birth
// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed
// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
// constructing final user object
// TODO fix:
// @ts-ignore
const user: User = { const user: User = {
id: Snowflake.generate(), id: Snowflake.generate(),
created_at: new Date(), created_at: new Date(),
@ -178,10 +177,25 @@ router.post(
avatar: null, avatar: null,
bot: false, bot: false,
system: false, system: false,
desktop: false,
mobile: false,
premium: false,
premium_type: 0,
phone: undefined,
mfa_enabled: false, mfa_enabled: false,
verified: false, verified: false,
presence: {
activities: [],
client_status: {
desktop: undefined,
mobile: undefined,
web: undefined,
},
status: "offline",
},
email: adjusted_email, email: adjusted_email,
nsfw_allowed: true, // TODO: depending on age nsfw_allowed: true, // TODO: depending on age
public_flags: 0n,
flags: 0n, // TODO: generate default flags flags: 0n, // TODO: generate default flags
guilds: [], guilds: [],
user_data: { user_data: {
@ -233,7 +247,7 @@ router.post(
}; };
// insert user into database // insert user into database
await new UserModel(user).save({}); await new UserModel(user).save();
return res.json({ token: await generateToken(user.id) }); return res.json({ token: await generateToken(user.id) });
} }

View File

@ -13,7 +13,7 @@ const router: Router = Router();
router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => {
const usID = req.user_id; const usID = req.user_id;
const chID = BigInt(req.params.channel_id); const chID = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: chID }).exec(); const channel = await ChannelModel.findOne({ id: chID }).exec();
if (!channel || !channel.guild_id) { if (!channel || !channel.guild_id) {
@ -47,7 +47,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const usID = req.user_id; const usID = req.user_id;
const chID = BigInt(req.params.channel_id); const chID = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: chID }).exec(); const channel = await ChannelModel.findOne({ id: chID }).exec();
if (!channel || !channel.guild_id) { if (!channel || !channel.guild_id) {

View File

@ -12,8 +12,8 @@ export default router;
// TODO: should users be able to bulk delete messages or only bots? // TODO: should users be able to bulk delete messages or only bots?
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids? // 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 // https://discord.com/developers/docs/resources/channel#bulk-delete-messages
router.post("/", check({ messages: [BigInt] }), async (req, res) => { router.post("/", check({ messages: [String] }), async (req, res) => {
const channel_id = BigInt(req.params.channel_id); const channel_id = req.params.channel_id
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec(); 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); if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
@ -22,7 +22,7 @@ router.post("/", check({ messages: [BigInt] }), async (req, res) => {
const { maxBulkDelete } = Config.get().limits.message; const { maxBulkDelete } = Config.get().limits.message;
const { messages } = req.body as { messages: bigint[] }; const { messages } = req.body as { messages: string[] };
if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete");
if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`); if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`);

View File

@ -27,23 +27,23 @@ function isTextChannel(type: ChannelType): boolean {
// https://discord.com/developers/docs/resources/channel#create-message // https://discord.com/developers/docs/resources/channel#create-message
// get messages // get messages
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
const channel_id = BigInt(req.params.channel_id); const channel_id = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
if (!channel) throw new HTTPError("Channel not found", 404); if (!channel) throw new HTTPError("Channel not found", 404);
isTextChannel(channel.type); isTextChannel(channel.type);
try { try {
instanceOf({ $around: BigInt, $after: BigInt, $before: BigInt, $limit: new Length(Number, 1, 100) }, req.query, { instanceOf({ $around: String, $after: String, $before: String, $limit: new Length(Number, 1, 100) }, req.query, {
path: "query", path: "query",
req, req,
}); });
} catch (error) { } catch (error) {
return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error });
} }
var { around, after, before, limit }: { around?: bigint; after?: bigint; before?: bigint; limit?: number } = req.query; var { around, after, before, limit }: { around?: string; after?: string; before?: string; limit?: number } = req.query;
if (!limit) limit = 50; if (!limit) limit = 50;
var halfLimit = BigInt(Math.floor(limit / 2)); var halfLimit = Math.floor(limit / 2);
if ([ChannelType.GUILD_VOICE, ChannelType.GUILD_CATEGORY, ChannelType.GUILD_STORE].includes(channel.type)) if ([ChannelType.GUILD_VOICE, ChannelType.GUILD_CATEGORY, ChannelType.GUILD_STORE].includes(channel.type))
throw new HTTPError("Not a text channel"); throw new HTTPError("Not a text channel");
@ -89,7 +89,7 @@ const messageUpload = multer({ limits: { fieldSize: 1024 * 1024 * 1024 * 50 } })
// TODO: trim and replace message content and every embed field // TODO: trim and replace message content and every embed field
// Send message // Send message
router.post("/", check(MessageCreateSchema), async (req, res) => { router.post("/", check(MessageCreateSchema), async (req, res) => {
const channel_id = BigInt(req.params.channel_id); const channel_id = req.params.channel_id;
const body = req.body as MessageCreateSchema; const body = req.body as MessageCreateSchema;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();

View File

@ -11,7 +11,7 @@ import { getPublicUser } from "../../../util/User";
const router: Router = Router(); const router: Router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const guild = await GuildModel.findOne({ id: guild_id }).exec(); const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild not found", 404); if (!guild) throw new HTTPError("Guild not found", 404);
@ -21,8 +21,8 @@ router.get("/", async (req: Request, res: Response) => {
}); });
router.get("/:user", async (req: Request, res: Response) => { router.get("/:user", async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const user_id = BigInt(req.params.ban); const user_id = req.params.ban;
var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec(); var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec();
if (!ban) throw new HTTPError("Ban not found", 404); if (!ban) throw new HTTPError("Ban not found", 404);
@ -30,8 +30,8 @@ router.get("/:user", async (req: Request, res: Response) => {
}); });
router.post("/:user_id", 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 guild_id = req.params.id;
const banned_user_id = BigInt(req.params.user_id); const banned_user_id = req.params.user_id;
const banned_user = await getPublicUser(banned_user_id); const banned_user = await getPublicUser(banned_user_id);
const perms = await getPermission(req.user_id, guild_id); const perms = await getPermission(req.user_id, guild_id);
@ -61,8 +61,8 @@ router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Respo
}); });
router.delete("/:user_id", async (req: Request, res: Response) => { router.delete("/:user_id", async (req: Request, res: Response) => {
var guild_id = BigInt(req.params.id); var guild_id = req.params.id;
var banned_user_id = BigInt(req.params.user_id); var banned_user_id = req.params.user_id;
const banned_user = await getPublicUser(banned_user_id); const banned_user = await getPublicUser(banned_user_id);
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();

View File

@ -6,14 +6,14 @@ import { check } from "../../../util/instanceOf";
const router = Router(); const router = Router();
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
const guild_id = BigInt(req.params.id); const guild_id = (req.params.id);
const channels = await ChannelModel.find({ guild_id }).lean().exec(); const channels = await ChannelModel.find({ guild_id }).lean().exec();
res.json(channels); res.json(channels);
}); });
router.post("/", check(ChannelModifySchema), async (req, res) => { router.post("/", check(ChannelModifySchema), async (req, res) => {
const guild_id = BigInt(req.params.id); const guild_id = (req.params.id);
const body = req.body as ChannelModifySchema; const body = req.body as ChannelModifySchema;
if (!body.permission_overwrites) body.permission_overwrites = []; if (!body.permission_overwrites) body.permission_overwrites = [];
if (!body.topic) body.topic = ""; if (!body.topic) body.topic = "";

View File

@ -19,7 +19,7 @@ import { check } from "../../../util/instanceOf";
const router = Router(); const router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const guild = await GuildModel.findOne({ id: guild_id }).exec(); const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild does not exist", 404); if (!guild) throw new HTTPError("Guild does not exist", 404);
@ -32,7 +32,7 @@ router.get("/", async (req: Request, res: Response) => {
router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => { router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => {
const body = req.body as GuildUpdateSchema; const body = req.body as GuildUpdateSchema;
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const guild = await GuildModel.findOne({ id: guild_id }).exec(); const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("This guild does not exist", 404); if (!guild) throw new HTTPError("This guild does not exist", 404);
@ -45,7 +45,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
}); });
router.delete("/", async (req: Request, res: Response) => { router.delete("/", async (req: Request, res: Response) => {
var guild_id = BigInt(req.params.id); var guild_id = req.params.id;
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
if (!guild) throw new HTTPError("This guild does not exist", 404); if (!guild) throw new HTTPError("This guild does not exist", 404);

View File

@ -10,12 +10,12 @@ const router = Router();
// TODO: not allowed for user -> only allowed for bots with privileged intents // TODO: not allowed for user -> only allowed for bots with privileged intents
// TODO: send over websocket // TODO: send over websocket
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const guild = await GuildModel.findOne({ id: guild_id }).exec(); const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild not found", 404); if (!guild) throw new HTTPError("Guild not found", 404);
try { try {
instanceOf({ $limit: new Length(Number, 1, 1000), $after: BigInt }, req.query, { instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, {
path: "query", path: "query",
req, req,
ref: { obj: null, key: "" }, ref: { obj: null, key: "" },
@ -26,7 +26,7 @@ router.get("/", async (req: Request, res: Response) => {
// @ts-ignore // @ts-ignore
if (!req.query.limit) req.query.limit = 1; if (!req.query.limit) req.query.limit = 1;
const { limit, after } = (<unknown>req.query) as { limit: number; after: bigint }; const { limit, after } = (<unknown>req.query) as { limit: number; after: string };
const query = after ? { id: { $gt: after } } : {}; const query = after ? { id: { $gt: after } } : {};
var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection)
@ -39,8 +39,8 @@ router.get("/", async (req: Request, res: Response) => {
}); });
router.get("/:member", async (req: Request, res: Response) => { router.get("/:member", async (req: Request, res: Response) => {
const guild_id = BigInt(req.params.id); const guild_id = req.params.id;
const user_id = BigInt(req.params.member); const user_id = req.params.member;
const member = await MemberModel.findOne({ id: user_id, guild_id }).populate({ path: "user", select: PublicUserProjection }).exec(); const member = await MemberModel.findOne({ id: user_id, guild_id }).populate({ path: "user", select: PublicUserProjection }).exec();
if (!member) throw new HTTPError("Member not found", 404); if (!member) throw new HTTPError("Member not found", 404);

View File

@ -19,7 +19,7 @@ router.get("/", async (req: Request, res: Response) => {
// user send to leave a certain guild // user send to leave a certain guild
router.delete("/:id", async (req: Request, res: Response) => { router.delete("/:id", async (req: Request, res: Response) => {
const guildID = BigInt(req.params.id); const guildID = (req.params.id);
const guild = await GuildModel.findOne({ id: guildID }).exec(); const guild = await GuildModel.findOne({ id: guildID }).exec();
if (!guild) throw new HTTPError("Guild doesn't exist", 404); if (!guild) throw new HTTPError("Guild doesn't exist", 404);

View File

@ -10,7 +10,7 @@ export const ChannelModifySchema = {
$position: Number, $position: Number,
$permission_overwrites: [ $permission_overwrites: [
{ {
id: BigInt, id: String,
type: new Length(Number, 0, 1), // either 0 (role) or 1 (member) type: new Length(Number, 0, 1), // either 0 (role) or 1 (member)
allow: BigInt, allow: BigInt,
deny: BigInt, deny: BigInt,
@ -29,23 +29,23 @@ export interface ChannelModifySchema {
rate_limit_per_user?: Number; rate_limit_per_user?: Number;
position?: number; position?: number;
permission_overwrites?: { permission_overwrites?: {
id: bigint; id: string;
type: number; type: number;
allow: bigint; allow: bigint;
deny: bigint; deny: bigint;
}[]; }[];
parent_id?: bigint; parent_id?: string;
nsfw?: boolean; nsfw?: boolean;
} }
export const ChannelGuildPositionUpdateSchema = [ export const ChannelGuildPositionUpdateSchema = [
{ {
id: BigInt, id: String,
$position: Number, $position: Number,
}, },
]; ];
export type ChannelGuildPositionUpdateSchema = { export type ChannelGuildPositionUpdateSchema = {
id: bigint; id: string;
position?: number; position?: number;
}[]; }[];

View File

@ -7,7 +7,7 @@ export const GuildCreateSchema = {
$icon: String, $icon: String,
$channels: [Object], $channels: [Object],
$guild_template_code: String, $guild_template_code: String,
$system_channel_id: BigInt, $system_channel_id: String,
}; };
export interface GuildCreateSchema { export interface GuildCreateSchema {
@ -16,7 +16,7 @@ export interface GuildCreateSchema {
icon?: string; icon?: string;
channels?: GuildChannel[]; channels?: GuildChannel[];
guild_template_code?: string; guild_template_code?: string;
system_channel_id?: bigint; system_channel_id?: string;
} }
export const GuildUpdateSchema = { export const GuildUpdateSchema = {
@ -29,11 +29,11 @@ export const GuildUpdateSchema = {
$verification_level: Number, $verification_level: Number,
$default_message_notifications: Number, $default_message_notifications: Number,
$system_channel_flags: Number, $system_channel_flags: Number,
$system_channel_id: BigInt, $system_channel_id: String,
$explicit_content_filter: Number, $explicit_content_filter: Number,
$public_updates_channel_id: BigInt, $public_updates_channel_id: String,
$afk_timeout: Number, $afk_timeout: Number,
$afk_channel_id: BigInt, $afk_channel_id: String,
}; };
// @ts-ignore // @ts-ignore
delete GuildUpdateSchema.$channels; delete GuildUpdateSchema.$channels;
@ -47,9 +47,9 @@ export interface GuildUpdateSchema extends Omit<GuildCreateSchema, "channels"> {
default_message_notifications?: number; default_message_notifications?: number;
system_channel_flags?: number; system_channel_flags?: number;
explicit_content_filter?: number; explicit_content_filter?: number;
public_updates_channel_id?: bigint; public_updates_channel_id?: string;
afk_timeout?: number; afk_timeout?: number;
afk_channel_id?: bigint; afk_channel_id?: string;
} }
export const GuildGetSchema = { export const GuildGetSchema = {

View File

@ -44,9 +44,9 @@ export const MessageCreateSchema = {
}, },
$allowed_mentions: [], $allowed_mentions: [],
$message_reference: { $message_reference: {
message_id: BigInt, message_id: String,
channel_id: BigInt, channel_id: String,
$guild_id: BigInt, $guild_id: String,
$fail_if_not_exists: Boolean, $fail_if_not_exists: Boolean,
}, },
$payload_json: String, $payload_json: String,
@ -60,9 +60,9 @@ export interface MessageCreateSchema {
embed?: Embed & { timestamp: string }; embed?: Embed & { timestamp: string };
allowed_mentions?: []; allowed_mentions?: [];
message_reference?: { message_reference?: {
message_id: bigint; message_id: string;
channel_id: bigint; channel_id: string;
guild_id?: bigint; guild_id?: string;
fail_if_not_exists: boolean; fail_if_not_exists: boolean;
}; };
payload_json?: string; payload_json?: string;

View File

@ -1,13 +1,12 @@
import mongoose, { Schema, Types } from "mongoose"; import mongoose, { Schema, Types } from "mongoose";
import { Long as MongoTypeLong } from "mongodb";
require("mongoose-long")(mongoose); require("mongoose-long")(mongoose);
const userSchema = new Schema({ const userSchema = new Schema({
id: MongoTypeLong, id: String,
}); });
const messageSchema = new Schema({ const messageSchema = new Schema({
id: MongoTypeLong, id: String,
content: String, content: String,
}); });
const message = mongoose.model("message", messageSchema, "messages"); const message = mongoose.model("message", messageSchema, "messages");

View File

@ -23,7 +23,7 @@ export interface RateLimit {
export interface DefaultOptions { export interface DefaultOptions {
general: { general: {
instance_id: bigint; instance_id: string;
}; };
permissions: { permissions: {
user: { user: {

View File

@ -25,7 +25,7 @@ export const PublicMemberProjection = {
premium_since: true, premium_since: true,
}; };
export async function addMember(user_id: bigint, guild_id: bigint, cache?: { guild?: Guild }) { export async function addMember(user_id: string, guild_id: string, cache?: { guild?: Guild }) {
const user = await getPublicUser(user_id, { guilds: true }); const user = await getPublicUser(user_id, { guilds: true });
const guildSize = user.guilds.length; const guildSize = user.guilds.length;
@ -83,7 +83,7 @@ export async function addMember(user_id: bigint, guild_id: bigint, cache?: { gui
]); ]);
} }
export async function removeMember(user_id: bigint, guild_id: bigint) { export async function removeMember(user_id: string, guild_id: string) {
const user = await getPublicUser(user_id); const user = await getPublicUser(user_id);
const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec();

View File

@ -9,7 +9,7 @@ export const PublicUserProjection = {
avatar: true, avatar: true,
}; };
export async function getPublicUser(user_id: bigint, additional_fields?: any) { export async function getPublicUser(user_id: string, additional_fields?: any) {
const user = await UserModel.findOne( const user = await UserModel.findOne(
{ id: user_id }, { id: user_id },
{ {