From c9ff1774b435b5af72faa97386890b3cb659744c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 29 Aug 2021 00:03:40 +0200 Subject: :sparkles: typeorm api rewrite done --- api/src/util/Channel.ts | 17 +--- api/src/util/Member.ts | 220 ------------------------------------------------ api/src/util/Message.ts | 19 ++--- 3 files changed, 11 insertions(+), 245 deletions(-) delete mode 100644 api/src/util/Member.ts (limited to 'api/src/util') diff --git a/api/src/util/Channel.ts b/api/src/util/Channel.ts index f518aefd..bc9217ce 100644 --- a/api/src/util/Channel.ts +++ b/api/src/util/Channel.ts @@ -1,20 +1,9 @@ -import { - ChannelCreateEvent, - Channel, - ChannelType, - emitEvent, - getPermission, - Guild, - Snowflake, - TextChannel, - toObject, - VoiceChannel -} from "@fosscord/util"; +import { ChannelCreateEvent, Channel, ChannelType, emitEvent, getPermission, Snowflake } from "@fosscord/util"; import { HTTPError } from "lambert-server"; // TODO: DM channel export async function createChannel( - channel: Partial, + channel: Partial, user_id: string = "0", opts?: { keepId?: boolean; @@ -29,7 +18,7 @@ export async function createChannel( case ChannelType.GUILD_TEXT: case ChannelType.GUILD_VOICE: if (channel.parent_id && !opts?.skipExistsCheck) { - const exists = await Channel.findOneOrFail({ id: channel.parent_id }, { guild_id: true }); + const exists = await Channel.findOneOrFail({ id: channel.parent_id }); 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"); } diff --git a/api/src/util/Member.ts b/api/src/util/Member.ts deleted file mode 100644 index 6cb14d71..00000000 --- a/api/src/util/Member.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { - Guild, - GuildCreateEvent, - GuildDeleteEvent, - GuildMemberAddEvent, - GuildMemberRemoveEvent, - GuildMemberUpdateEvent, - Guild, - Member, - Role, - toObject, - User, - GuildDocument, - Config, - emitEvent -} from "@fosscord/util"; - -import { HTTPError } from "lambert-server"; - -import { getPublicUser } from "./User"; - -export const PublicMemberProjection = { - id: true, - guild_id: true, - nick: true, - roles: true, - joined_at: true, - pending: true, - deaf: true, - mute: true, - premium_since: true -}; - -export async function isMember(user_id: string, guild_id: string) { - const exists = await Member.exists({ id: user_id, guild_id }); - if (!exists) throw new HTTPError("You are not a member of this guild", 403); - return exists; -} - -export async function addMember(user_id: string, guild_id: string, cache?: { guild?: GuildDocument }) { - const user = await getPublicUser(user_id, { guilds: true }); - - const { maxGuilds } = Config.get().limits.user; - if (user.guilds.length >= maxGuilds) { - throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); - } - - const guild = cache?.guild || (await Guild.findOneOrFail({ id: guild_id })); - - if (!guild) throw new HTTPError("Guild not found", 404); - - if (await Member.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400); - - const member = { - id: user_id, - guild_id: guild_id, - nick: undefined, - roles: [guild_id], // @everyone role - joined_at: new Date(), - premium_since: undefined, - deaf: false, - mute: false, - pending: false - }; - - await Promise.all([ - new Member({ - ...member, - read_state: {}, - settings: { - channel_overrides: [], - message_notifications: 0, - mobile_push: true, - mute_config: null, - muted: false, - suppress_everyone: false, - suppress_roles: false, - version: 0 - } - }).save(), - - User.update({ id: user_id }, { $push: { guilds: guild_id } }), - Guild.update({ id: guild_id }, { $inc: { member_count: 1 } }), - - emitEvent({ - event: "GUILD_MEMBER_ADD", - data: { - ...member, - user, - guild_id: guild_id - }, - guild_id: guild_id - } as GuildMemberAddEvent) - ]); - - await emitEvent({ - event: "GUILD_CREATE", - data: await guild - .populate({ path: "members", match: { guild_id } }) - .populate({ path: "joined_at", match: { id: user.id } }) - .execPopulate(), - user_id - } as GuildCreateEvent); -} - -export async function removeMember(user_id: string, guild_id: string) { - const user = await getPublicUser(user_id); - - const guild = await Guild.findOneOrFail({ id: guild_id }, { owner_id: true }); - if (!guild) throw new HTTPError("Guild not found", 404); - if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); - if (!(await Member.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404); - - // use promise all to execute all promises at the same time -> save time - return Promise.all([ - Member.deleteOne({ - id: user_id, - guild_id: guild_id - }), - User.update({ id: user.id }, { $pull: { guilds: guild_id } }), - Guild.update({ id: guild_id }, { $inc: { member_count: -1 } }), - - emitEvent({ - event: "GUILD_DELETE", - data: { - id: guild_id - }, - user_id: user_id - } as GuildDeleteEvent), - emitEvent({ - event: "GUILD_MEMBER_REMOVE", - data: { - guild_id: guild_id, - user: user - }, - guild_id: guild_id - } as GuildMemberRemoveEvent) - ]); -} - -export async function addRole(user_id: string, guild_id: string, role_id: string) { - const user = await getPublicUser(user_id); - - const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id }); - if (!role) throw new HTTPError("role not found", 404); - - var memberObj = await Member.findOneOrFailAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { $push: { roles: role_id } }, - { new: true } - ); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - roles: memberObj.roles - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} - -export async function removeRole(user_id: string, guild_id: string, role_id: string) { - const user = await getPublicUser(user_id); - - const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id }); - if (!role) throw new HTTPError("role not found", 404); - - var memberObj = await Member.findOneOrFailAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { $pull: { roles: role_id } }, - { new: true } - ); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - roles: memberObj.roles - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} - -export async function changeNickname(user_id: string, guild_id: string, nickname: string) { - const user = await getPublicUser(user_id); - - var memberObj = await Member.findOneOrFailAndUpdate( - { - id: user_id, - guild_id: guild_id - }, - { nick: nickname }, - { new: true } - ); - - if (!memberObj) throw new HTTPError("Member not found", 404); - - await emitEvent({ - event: "GUILD_MEMBER_UPDATE", - data: { - guild_id: guild_id, - user: user, - nick: nickname - }, - guild_id: guild_id - } as GuildMemberUpdateEvent); -} diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index 5561904b..70989301 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -8,18 +8,17 @@ import { getPermission, CHANNEL_MENTION, Snowflake, - PublicMemberProjection, USER_MENTION, ROLE_MENTION, Role, EVERYONE_MENTION, - HERE_MENTION + HERE_MENTION, + MessageType } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import fetch from "node-fetch"; import cheerio from "cheerio"; -import { MessageType } from "@fosscord/util/dist/util/Constants"; // TODO: check webhook, application, system author const LINK_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g; @@ -61,7 +60,7 @@ export async function handleMessage(opts: Partial): Promise { } var content = opts.content; - var mention_channels_ids = [] as string[]; + var mention_channel_ids = [] as string[]; var mention_role_ids = [] as string[]; var mention_user_ids = [] as string[]; var mention_everyone = false; @@ -70,7 +69,7 @@ export async function handleMessage(opts: Partial): Promise { if (content) { content = content.trim(); for (const [_, mention] of content.matchAll(CHANNEL_MENTION)) { - if (!mention_channels_ids.includes(mention)) mention_channels_ids.push(mention); + if (!mention_channel_ids.includes(mention)) mention_channel_ids.push(mention); } for (const [_, mention] of content.matchAll(USER_MENTION)) { @@ -92,11 +91,12 @@ export async function handleMessage(opts: Partial): Promise { } // TODO: check and put it all in the body + return { ...opts, guild_id: channel.guild_id, channel_id: opts.channel_id, - mention_channels_ids, + mention_channel_ids, mention_role_ids, mention_user_ids, mention_everyone, @@ -104,7 +104,7 @@ export async function handleMessage(opts: Partial): Promise { embeds: opts.embeds || [], reactions: opts.reactions || [], type: opts.type ?? 0 - }; + } as Message; } // TODO: cache link result in db @@ -163,10 +163,7 @@ export async function postHandleMessage(message: Message) { export async function sendMessage(opts: Partial) { const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); - const data = await new Message(message) - .populate({ path: "member", select: PublicMemberProjection }) - .populate("referenced_message") - .save(); + const data = await new Message(message).save(); await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data } as MessageCreateEvent); -- cgit 1.4.1