diff --git a/src/api/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts
index d0e4d8a0..812888a3 100644
--- a/src/api/middlewares/Authentication.ts
+++ b/src/api/middlewares/Authentication.ts
@@ -92,12 +92,7 @@ export async function Authentication(
Sentry.setUser({ id: req.user_id });
try {
- const { jwtSecret } = Config.get().security;
-
- const { decoded, user } = await checkToken(
- req.headers.authorization,
- jwtSecret,
- );
+ const { decoded, user } = await checkToken(req.headers.authorization);
req.token = decoded;
req.user_id = decoded.id;
diff --git a/src/api/routes/auth/register.ts b/src/api/routes/auth/register.ts
index 321b4a65..14dc319a 100644
--- a/src/api/routes/auth/register.ts
+++ b/src/api/routes/auth/register.ts
@@ -225,6 +225,20 @@ router.post(
}
if (body.password) {
+ const min = register.password.minLength
+ ? register.password.minLength
+ : 8;
+ if (body.password.length < min) {
+ throw FieldErrors({
+ password: {
+ code: "PASSWORD_REQUIREMENTS_MIN_LENGTH",
+ message: req.t(
+ "auth:register.PASSWORD_REQUIREMENTS_MIN_LENGTH",
+ { min: min },
+ ),
+ },
+ });
+ }
// the salt is saved in the password refer to bcrypt docs
body.password = await bcrypt.hash(body.password, 12);
} else if (register.password.required) {
diff --git a/src/api/routes/auth/reset.ts b/src/api/routes/auth/reset.ts
index f97045a6..cb4f8180 100644
--- a/src/api/routes/auth/reset.ts
+++ b/src/api/routes/auth/reset.ts
@@ -48,11 +48,9 @@ router.post(
async (req: Request, res: Response) => {
const { password, token } = req.body as PasswordResetSchema;
- const { jwtSecret } = Config.get().security;
-
let user;
try {
- const userTokenData = await checkToken(token, jwtSecret, true);
+ const userTokenData = await checkToken(token);
user = userTokenData.user;
} catch {
throw FieldErrors({
diff --git a/src/api/routes/auth/verify/index.ts b/src/api/routes/auth/verify/index.ts
index a98c17fa..49f74277 100644
--- a/src/api/routes/auth/verify/index.ts
+++ b/src/api/routes/auth/verify/index.ts
@@ -78,11 +78,10 @@ router.post(
}
}
- const { jwtSecret } = Config.get().security;
let user;
try {
- const userTokenData = await checkToken(token, jwtSecret, true);
+ const userTokenData = await checkToken(token);
user = userTokenData.user;
} catch {
throw FieldErrors({
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts
index f031fa75..edc0321c 100644
--- a/src/api/routes/channels/#channel_id/messages/index.ts
+++ b/src/api/routes/channels/#channel_id/messages/index.ts
@@ -20,7 +20,6 @@ import { handleMessage, postHandleMessage, route } from "@spacebar/api";
import {
Attachment,
Channel,
- ChannelType,
Config,
DmChannelDTO,
FieldErrors,
@@ -93,8 +92,6 @@ router.get(
if (limit < 1 || limit > 100)
throw new HTTPError("limit must be between 1 and 100", 422);
- const halfLimit = Math.floor(limit / 2);
-
const permissions = await getPermission(
req.user_id,
channel.guild_id,
@@ -121,64 +118,72 @@ router.get(
],
};
- if (after) {
- if (BigInt(after) > BigInt(Snowflake.generate()))
- return res.status(422);
- query.where.id = MoreThan(after);
- } else if (before) {
- if (BigInt(before) < BigInt(req.params.channel_id))
- return res.status(422);
- query.where.id = LessThan(before);
- } else if (around) {
- query.where.id = [
- MoreThan((BigInt(around) - BigInt(halfLimit)).toString()),
- LessThan((BigInt(around) + BigInt(halfLimit)).toString()),
- ];
-
- return res.json([]); // TODO: fix around
+ let messages: Message[];
+
+ if (around) {
+ query.take = Math.floor(limit / 2);
+ const [right, left] = await Promise.all([
+ Message.find({ ...query, where: { id: LessThan(around) } }),
+ Message.find({ ...query, where: { id: MoreThan(around) } }),
+ ]);
+ right.push(...left);
+ messages = right;
+ } else {
+ if (after) {
+ if (BigInt(after) > BigInt(Snowflake.generate()))
+ return res.status(422);
+ query.where.id = MoreThan(after);
+ } else if (before) {
+ if (BigInt(before) < BigInt(Snowflake.generate()))
+ return res.status(422);
+ query.where.id = LessThan(before);
+ }
+
+ messages = await Message.find(query);
}
- const messages = await Message.find(query);
const endpoint = Config.get().cdn.endpointPublic;
- return res.json(
- messages.map((x: Partial<Message>) => {
- (x.reactions || []).forEach((y: Partial<Reaction>) => {
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
- //@ts-ignore
- if ((y.user_ids || []).includes(req.user_id)) y.me = true;
- delete y.user_ids;
- });
- if (!x.author)
- x.author = User.create({
- id: "4",
- discriminator: "0000",
- username: "Spacebar Ghost",
- public_flags: 0,
- });
- x.attachments?.forEach((y: Attachment) => {
- // dynamically set attachment proxy_url in case the endpoint changed
- const uri = y.proxy_url.startsWith("http")
- ? y.proxy_url
- : `https://example.org${y.proxy_url}`;
- y.proxy_url = `${endpoint == null ? "" : endpoint}${
- new URL(uri).pathname
- }`;
+ const ret = messages.map((x: Message) => {
+ x = x.toJSON();
+
+ (x.reactions || []).forEach((y: Partial<Reaction>) => {
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ //@ts-ignore
+ if ((y.user_ids || []).includes(req.user_id)) y.me = true;
+ delete y.user_ids;
+ });
+ if (!x.author)
+ x.author = User.create({
+ id: "4",
+ discriminator: "0000",
+ username: "Spacebar Ghost",
+ public_flags: 0,
});
+ x.attachments?.forEach((y: Attachment) => {
+ // dynamically set attachment proxy_url in case the endpoint changed
+ const uri = y.proxy_url.startsWith("http")
+ ? y.proxy_url
+ : `https://example.org${y.proxy_url}`;
+ y.proxy_url = `${endpoint == null ? "" : endpoint}${
+ new URL(uri).pathname
+ }`;
+ });
- /**
+ /**
Some clients ( discord.js ) only check if a property exists within the response,
which causes errors when, say, the `application` property is `null`.
**/
- // for (var curr in x) {
- // if (x[curr] === null)
- // delete x[curr];
- // }
+ // for (var curr in x) {
+ // if (x[curr] === null)
+ // delete x[curr];
+ // }
- return x;
- }),
- );
+ return x;
+ });
+
+ return res.json(ret);
},
);
@@ -304,9 +309,11 @@ router.post(
embeds,
channel_id,
attachments,
- edited_timestamp: undefined,
timestamp: new Date(),
});
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ //@ts-ignore dont care2
+ message.edited_timestamp = null;
channel.last_message_id = message.id;
diff --git a/src/api/routes/guilds/#guild_id/index.ts b/src/api/routes/guilds/#guild_id/index.ts
index afe60614..86777b36 100644
--- a/src/api/routes/guilds/#guild_id/index.ts
+++ b/src/api/routes/guilds/#guild_id/index.ts
@@ -161,7 +161,7 @@ router.patch(
const data = guild.toJSON();
// TODO: guild hashes
// TODO: fix vanity_url_code, template_id
- delete data.vanity_url_code;
+ // delete data.vanity_url_code;
delete data.template_id;
await Promise.all([
diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
index 5f1f6fa7..cafb922e 100644
--- a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
+++ b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -27,6 +27,8 @@ import {
handleFile,
Member,
MemberChangeSchema,
+ PublicMemberProjection,
+ PublicUserProjection,
Role,
Sticker,
} from "@spacebar/util";
@@ -39,7 +41,7 @@ router.get(
route({
responses: {
200: {
- body: "Member",
+ body: "APIPublicMember",
},
403: {
body: "APIErrorResponse",
@@ -55,9 +57,28 @@ router.get(
const member = await Member.findOneOrFail({
where: { id: member_id, guild_id },
+ relations: ["roles", "user"],
+ select: {
+ index: true,
+ // only grab public member props
+ ...Object.fromEntries(
+ PublicMemberProjection.map((x) => [x, true]),
+ ),
+ // and public user props
+ user: Object.fromEntries(
+ PublicUserProjection.map((x) => [x, true]),
+ ),
+ roles: {
+ id: true,
+ },
+ },
});
- return res.json(member);
+ return res.json({
+ ...member.toPublicMember(),
+ user: member.user.toPublicUser(),
+ roles: member.roles.map((x) => x.id),
+ });
},
);
diff --git a/src/api/routes/guilds/index.ts b/src/api/routes/guilds/index.ts
index 26173ed5..545beb18 100644
--- a/src/api/routes/guilds/index.ts
+++ b/src/api/routes/guilds/index.ts
@@ -72,7 +72,7 @@ router.post(
await Member.addToGuild(req.user_id, guild.id);
- res.status(201).json({ id: guild.id });
+ res.status(201).json(guild);
},
);
diff --git a/src/api/routes/users/#id/profile.ts b/src/api/routes/users/#id/profile.ts
index a94eb546..eecec0f3 100644
--- a/src/api/routes/users/#id/profile.ts
+++ b/src/api/routes/users/#id/profile.ts
@@ -84,18 +84,6 @@ router.get(
// TODO: make proper DTO's in util?
- const userDto = {
- username: user.username,
- discriminator: user.discriminator,
- id: user.id,
- public_flags: user.public_flags,
- avatar: user.avatar,
- accent_color: user.accent_color,
- banner: user.banner,
- bio: req.user_bot ? null : user.bio,
- bot: user.bot,
- };
-
const userProfile = {
bio: req.user_bot ? null : user.bio,
accent_color: user.accent_color,
@@ -104,28 +92,6 @@ router.get(
theme_colors: user.theme_colors,
};
- const guildMemberDto = guild_member
- ? {
- avatar: guild_member.avatar,
- banner: guild_member.banner,
- bio: req.user_bot ? null : guild_member.bio,
- communication_disabled_until:
- guild_member.communication_disabled_until,
- deaf: guild_member.deaf,
- flags: user.flags,
- is_pending: guild_member.pending,
- pending: guild_member.pending, // why is this here twice, discord?
- joined_at: guild_member.joined_at,
- mute: guild_member.mute,
- nick: guild_member.nick,
- premium_since: guild_member.premium_since,
- roles: guild_member.roles
- .map((x) => x.id)
- .filter((id) => id != guild_id),
- user: userDto,
- }
- : undefined;
-
const guildMemberProfile = {
accent_color: null,
banner: guild_member?.banner || null,
@@ -139,11 +105,11 @@ router.get(
premium_guild_since: premium_guild_since, // TODO
premium_since: user.premium_since, // TODO
mutual_guilds: mutual_guilds, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
- user: userDto,
+ user: user.toPublicUser(),
premium_type: user.premium_type,
profile_themes_experiment_bucket: 4, // TODO: This doesn't make it available, for some reason?
user_profile: userProfile,
- guild_member: guild_id && guildMemberDto,
+ guild_member: guild_member?.toPublicMember(),
guild_member_profile: guild_id && guildMemberProfile,
});
},
diff --git a/src/api/util/utility/ipAddress.ts b/src/api/util/utility/ipAddress.ts
index 172e9604..c51daf6c 100644
--- a/src/api/util/utility/ipAddress.ts
+++ b/src/api/util/utility/ipAddress.ts
@@ -102,7 +102,7 @@ export function getIpAdress(req: Request): string {
return (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
- req.headers[Config.get().security.forwadedFor] ||
+ req.headers[Config.get().security.forwardedFor] ||
req.socket.remoteAddress
);
}
|