From 8a13a1f2ea990d7e1c07138847644653eb0aece6 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 22 May 2021 17:39:28 +0200 Subject: :art: Move member routes in separate files --- src/schema/Member.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src/schema') diff --git a/src/schema/Member.ts b/src/schema/Member.ts index 49a4d7ce..607d0a06 100644 --- a/src/schema/Member.ts +++ b/src/schema/Member.ts @@ -1,21 +1,29 @@ export const MemberCreateSchema = { id: String, - nick: String, - guild_id: String, - joined_at: Date, + nick: String, + guild_id: String, + joined_at: Date }; export interface MemberCreateSchema { id: string; - nick: string; - guild_id: string; + nick: string; + guild_id: string; joined_at: Date; } export const MemberNickChangeSchema = { - nick: String, -} + nick: String +}; export interface MemberNickChangeSchema { - nick: string, -} \ No newline at end of file + nick: string; +} + +export const MemberChangeSchema = { + $roles: [String] +}; + +export interface MemberChangeSchema { + roles?: string[]; +} -- cgit 1.5.1 From 4715ef1eab99f8158b4ddaa0b9b7bc9a61d0e337 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 22 May 2021 17:40:10 +0200 Subject: :sparkles: createChannel() --- src/routes/guilds/#guild_id/channels.ts | 37 ++-------------------- src/routes/guilds/#guild_id/index.ts | 11 ------- src/routes/guilds/index.ts | 4 ++- src/schema/Invite.ts | 2 +- src/schema/Roles.ts | 31 +++++-------------- src/test/test.ts | 9 ++---- src/util/Channel.ts | 54 +++++++++++++++++++++++++++++++++ src/util/instanceOf.ts | 17 ++++++----- 8 files changed, 78 insertions(+), 87 deletions(-) create mode 100644 src/util/Channel.ts (limited to 'src/schema') diff --git a/src/routes/guilds/#guild_id/channels.ts b/src/routes/guilds/#guild_id/channels.ts index 5c90b5b9..9d8a95b0 100644 --- a/src/routes/guilds/#guild_id/channels.ts +++ b/src/routes/guilds/#guild_id/channels.ts @@ -13,6 +13,7 @@ import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; import { emitEvent } from "../../../util/Event"; import { check } from "../../../util/instanceOf"; +import { createChannel } from "../../../util/Channel"; const router = Router(); router.get("/", async (req, res) => { @@ -26,41 +27,7 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { const { guild_id } = req.params; const body = req.body as ChannelModifySchema; - if (!body.permission_overwrites) body.permission_overwrites = []; - if (!body.topic) body.topic = ""; - if (!body.rate_limit_per_user) body.rate_limit_per_user = 0; - - switch (body.type) { - case ChannelType.DM: - case ChannelType.GROUP_DM: - throw new HTTPError("You can't create a dm channel in a guild"); - // TODO: - case ChannelType.GUILD_STORE: - throw new HTTPError("Not yet supported"); - case ChannelType.GUILD_NEWS: - // TODO: check if guild is community server - } - - if (body.parent_id) { - const exists = await ChannelModel.findOne({ id: body.parent_id }, { guild_id: true }).exec(); - if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); - if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild"); - } - - const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - - const channel = { - ...body, - id: Snowflake.generate(), - created_at: new Date(), - guild_id, - recipients: null - }; - - await new ChannelModel(channel).save(); - - await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent); + const channel = await createChannel({ ...body, guild_id }, req.user_id); res.json(channel); }); diff --git a/src/routes/guilds/#guild_id/index.ts b/src/routes/guilds/#guild_id/index.ts index 47675609..9ef0127a 100644 --- a/src/routes/guilds/#guild_id/index.ts +++ b/src/routes/guilds/#guild_id/index.ts @@ -54,15 +54,4 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) return res.send(data); }); -router.get("/vanity-url", async (req: Request, res: Response) => { - const { guild_id } = req.params; - - const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild does not exist", 404); - - if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); - - return res.json(guild.vanity_url); -}); - export default router; diff --git a/src/routes/guilds/index.ts b/src/routes/guilds/index.ts index 1ed9d0ff..c286ad51 100644 --- a/src/routes/guilds/index.ts +++ b/src/routes/guilds/index.ts @@ -6,6 +6,7 @@ import { GuildCreateSchema } from "../../schema/Guild"; import Config from "../../util/Config"; import { getPublicUser } from "../../util/User"; import { addMember } from "../../util/Member"; +import { createChannel } from "../../util/Channel"; const router: Router = Router(); @@ -80,7 +81,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = }).save() ]); - await addMember(req.user_id, guild_id, { guild: guild_doc }); + await createChannel({ name: "general", type: 0, guild_id, position: 0, permission_overwrites: [] }, req.user_id); + await addMember(req.user_id, guild_id); res.status(201).json({ id: guild.id }); }); diff --git a/src/schema/Invite.ts b/src/schema/Invite.ts index 3c944037..a22449ba 100644 --- a/src/schema/Invite.ts +++ b/src/schema/Invite.ts @@ -7,7 +7,7 @@ export const InviteCreateSchema = { $temporary: Boolean, $unique: Boolean, $target_user: String, - $target_user_type: Number, + $target_user_type: Number }; export interface InviteCreateSchema { target_user_id?: String; diff --git a/src/schema/Roles.ts b/src/schema/Roles.ts index fe76dadc..f662e61b 100644 --- a/src/schema/Roles.ts +++ b/src/schema/Roles.ts @@ -1,34 +1,17 @@ -export const RoleCreateSchema = { - name: String, - permissions: BigInt, - color: Number, - hoist: Boolean, // whether the role should be displayed separately in the sidebar - mentionable: Boolean // whether the role should be mentionable -}; - -export interface RoleCreateSchema { - name: string, - permissions: BigInt, - color: number, - hoist: boolean, // whether the role should be displayed separately in the sidebar - mentionable: boolean // whether the role should be mentionable -} - export const RoleModifySchema = { $name: String, $permissions: BigInt, $color: Number, $hoist: Boolean, // whether the role should be displayed separately in the sidebar $mentionable: Boolean, // whether the role should be mentionable - $position: Number, - + $position: Number }; export interface RoleModifySchema { - name?: string, - permissions?: BigInt, - color?: number, - hoist?: boolean, // whether the role should be displayed separately in the sidebar - mentionable?: boolean, // whether the role should be mentionable - position?: number, + name?: string; + permissions?: BigInt; + color?: number; + hoist?: boolean; // whether the role should be displayed separately in the sidebar + mentionable?: boolean; // whether the role should be mentionable + position?: number; } diff --git a/src/test/test.ts b/src/test/test.ts index b7d877b3..478edcc4 100644 --- a/src/test/test.ts +++ b/src/test/test.ts @@ -1,8 +1,3 @@ -import { getPermission } from "@fosscord/server-util"; +import { Snowflake } from "@fosscord/server-util"; -async function main() { - const t = await getPermission("811642917432066048", "812327318532915201"); - console.log(t); -} - -main(); +console.log(Snowflake.deconstruct("0")); diff --git a/src/util/Channel.ts b/src/util/Channel.ts new file mode 100644 index 00000000..c8df85bc --- /dev/null +++ b/src/util/Channel.ts @@ -0,0 +1,54 @@ +import { + ChannelCreateEvent, + ChannelModel, + ChannelType, + getPermission, + GuildModel, + Snowflake, + TextChannel, + VoiceChannel +} from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "./Event"; + +// TODO: DM channel +export async function createChannel(channel: Partial, user_id: string = "0") { + if (!channel.permission_overwrites) channel.permission_overwrites = []; + + switch (channel.type) { + case ChannelType.GUILD_TEXT: + case ChannelType.GUILD_VOICE: + break; + case ChannelType.DM: + case ChannelType.GROUP_DM: + throw new HTTPError("You can't create a dm channel in a guild"); + // TODO: check if guild is community server + case ChannelType.GUILD_STORE: + case ChannelType.GUILD_NEWS: + default: + throw new HTTPError("Not yet supported"); + } + + const permissions = await getPermission(user_id, channel.guild_id); + permissions.hasThrow("MANAGE_CHANNELS"); + + if (channel.parent_id) { + const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); + if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); + if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); + } + + // TODO: auto generate position + + channel = await new ChannelModel({ + ...channel, + id: Snowflake.generate(), + created_at: new Date(), + // @ts-ignore + recipients: null + }).save(); + + await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent); + + return channel; +} diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts index e4e58092..b67bde27 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts @@ -5,7 +5,8 @@ import { Tuple } from "lambert-server"; import "missing-native-js-functions"; export const OPTIONAL_PREFIX = "$"; -export const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; +export const EMAIL_REGEX = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; export function check(schema: any) { return (req: Request, res: Response, next: NextFunction) => { @@ -27,9 +28,9 @@ export function FieldErrors(fields: Record Date: Sat, 22 May 2021 22:06:18 +0200 Subject: :sparkles: Message edit --- .../#channel_id/messages/#message_id/index.ts | 40 ++++++++++++++++++++-- .../#channel_id/messages/#message_id/reactions.ts | 2 +- src/schema/Message.ts | 18 +++++----- src/util/Message.ts | 12 ++++--- 4 files changed, 56 insertions(+), 16 deletions(-) (limited to 'src/schema') diff --git a/src/routes/channels/#channel_id/messages/#message_id/index.ts b/src/routes/channels/#channel_id/messages/#message_id/index.ts index 88b8d89b..5a61b4ad 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/index.ts @@ -1,11 +1,47 @@ -import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel } from "@fosscord/server-util"; +import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/server-util"; import { Router } from "express"; import { HTTPError } from "lambert-server"; +import { MessageCreateSchema } from "../../../../../schema/Message"; import { emitEvent } from "../../../../../util/Event"; import { check } from "../../../../../util/instanceOf"; +import { handleMessage } from "../../../../../util/Message"; const router = Router(); -// TODO: + +router.patch("/", check(MessageCreateSchema), async (req, res) => { + const { message_id, channel_id } = req.params; + var body = req.body as MessageCreateSchema; + + var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec(); + if (!message) throw new HTTPError("Message not found", 404); + + const permissions = await getPermission(req.user_id, undefined, channel_id); + + if (req.user_id !== message.author_id) { + permissions.hasThrow("MANAGE_MESSAGES"); + body = { flags: body.flags }; + } + + const opts = await handleMessage({ + ...body, + author_id: message.author_id, + channel_id, + id: message_id, + edited_timestamp: new Date() + }); + + message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec(); + if (!message) throw new HTTPError("Message not found", 404); + + await emitEvent({ + event: "MESSAGE_UPDATE", + channel_id, + guild_id: message.guild_id, + data: { ...toObject(message), nonce: undefined } + } as MessageUpdateEvent); + + return res.json(toObject(message)); +}); router.delete("/", async (req, res) => { const { message_id, channel_id } = req.params; diff --git a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts index f61977f4..1bfaae39 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts @@ -13,7 +13,7 @@ import { toObject, UserModel } from "@fosscord/server-util"; -import { Request, Response, Router } from "express"; +import { Router } from "express"; import { HTTPError } from "lambert-server"; import { emitEvent } from "../../../../../util/Event"; diff --git a/src/schema/Message.ts b/src/schema/Message.ts index 9b62edcf..e6aa42b3 100644 --- a/src/schema/Message.ts +++ b/src/schema/Message.ts @@ -5,6 +5,7 @@ export const MessageCreateSchema = { $content: new Length(String, 0, 2000), $nonce: String, $tts: Boolean, + $flags: BigInt, $embed: { $title: new Length(String, 0, 256), //title of embed $type: String, // type of embed (always "rich" for webhook embeds) @@ -15,48 +16,49 @@ export const MessageCreateSchema = { $footer: { text: new Length(String, 0, 2048), icon_url: String, - proxy_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, + url: String }, // provider object provider information $author: { name: new Length(String, 0, 256), url: String, icon_url: String, - proxy_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, - }, + $inline: Boolean + } ], 0, 25 - ), + ) }, $allowed_mentions: [], $message_reference: { message_id: String, channel_id: String, $guild_id: String, - $fail_if_not_exists: Boolean, + $fail_if_not_exists: Boolean }, $payload_json: String, - $file: Object, + $file: Object }; export interface MessageCreateSchema { content?: string; nonce?: string; tts?: boolean; + flags?: bigint; embed?: Embed & { timestamp?: string }; allowed_mentions?: []; message_reference?: { diff --git a/src/util/Message.ts b/src/util/Message.ts index 8e9f98cd..0d3cdac7 100644 --- a/src/util/Message.ts +++ b/src/util/Message.ts @@ -9,7 +9,7 @@ import { HTTPError } from "lambert-server"; import { emitEvent } from "./Event"; // TODO: check webhook, application, system author -export async function sendMessage(opts: Partial) { +export async function handleMessage(opts: Partial) { const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); // TODO: are tts messages allowed in dm channels? should permission be checked? @@ -28,10 +28,8 @@ export async function sendMessage(opts: Partial) { } // TODO: check and put it all in the body - const message: Message = { + return { ...opts, - id: Snowflake.generate(), - timestamp: new Date(), guild_id: channel.guild_id, channel_id: opts.channel_id, // TODO: generate mentions and check permissions @@ -43,10 +41,14 @@ export async function sendMessage(opts: Partial) { reactions: opts.reactions || [], type: opts.type ?? 0 }; +} + +export async function sendMessage(opts: Partial) { + const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); - await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); + await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent); return data; } -- cgit 1.5.1