This commit is contained in:
Flam3rboy
2021-08-24 16:35:04 +02:00
parent 1e5ed28514
commit a567551c8a
45 changed files with 381 additions and 454 deletions
+5 -5
View File
@@ -1,10 +1,10 @@
import {
ChannelCreateEvent,
ChannelModel,
Channel,
ChannelType,
emitEvent,
getPermission,
GuildModel,
Guild,
Snowflake,
TextChannel,
toObject,
@@ -29,7 +29,7 @@ export async function createChannel(
case ChannelType.GUILD_TEXT:
case ChannelType.GUILD_VOICE:
if (channel.parent_id && !opts?.skipExistsCheck) {
const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec();
const exists = await Channel.findOneOrFail({ id: channel.parent_id }, { guild_id: true });
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");
}
@@ -49,7 +49,7 @@ export async function createChannel(
if (!channel.permission_overwrites) channel.permission_overwrites = [];
// TODO: auto generate position
channel = await new ChannelModel({
channel = await new Channel({
...channel,
...(!opts?.keepId && { id: Snowflake.generate() }),
created_at: new Date(),
@@ -57,7 +57,7 @@ export async function createChannel(
recipient_ids: null
}).save();
await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), guild_id: channel.guild_id } as ChannelCreateEvent);
await emitEvent({ event: "CHANNEL_CREATE", data: channel), guild_id: channel.guild_id } as ChannelCreateEvent;
return channel;
}
+28 -30
View File
@@ -5,11 +5,11 @@ import {
GuildMemberAddEvent,
GuildMemberRemoveEvent,
GuildMemberUpdateEvent,
GuildModel,
MemberModel,
RoleModel,
Guild,
Member,
Role,
toObject,
UserModel,
User,
GuildDocument,
Config,
emitEvent
@@ -32,7 +32,7 @@ export const PublicMemberProjection = {
};
export async function isMember(user_id: string, guild_id: string) {
const exists = await MemberModel.exists({ id: user_id, guild_id });
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;
}
@@ -45,11 +45,11 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
}
const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec());
const guild = cache?.guild || (await Guild.findOneOrFail({ id: guild_id }));
if (!guild) throw new HTTPError("Guild not found", 404);
if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400);
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,
@@ -64,7 +64,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
};
await Promise.all([
new MemberModel({
new Member({
...member,
read_state: {},
settings: {
@@ -79,8 +79,8 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
}
}).save(),
UserModel.updateOne({ id: user_id }, { $push: { guilds: guild_id } }).exec(),
GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: 1 } }).exec(),
User.update({ id: user_id }, { $push: { guilds: guild_id } }),
Guild.update({ id: guild_id }, { $inc: { member_count: 1 } }),
emitEvent({
event: "GUILD_MEMBER_ADD",
@@ -95,12 +95,10 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
await emitEvent({
event: "GUILD_CREATE",
data: toObject(
await guild
.populate({ path: "members", match: { guild_id } })
.populate({ path: "joined_at", match: { id: user.id } })
.execPopulate()
),
data: await guild
.populate({ path: "members", match: { guild_id } })
.populate({ path: "joined_at", match: { id: user.id } })
.execPopulate(),
user_id
} as GuildCreateEvent);
}
@@ -108,19 +106,19 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
export async function removeMember(user_id: string, guild_id: string) {
const user = await getPublicUser(user_id);
const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec();
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 MemberModel.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404);
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([
MemberModel.deleteOne({
Member.deleteOne({
id: user_id,
guild_id: guild_id
}).exec(),
UserModel.updateOne({ id: user.id }, { $pull: { guilds: guild_id } }).exec(),
GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: -1 } }).exec(),
}),
User.update({ id: user.id }, { $pull: { guilds: guild_id } }),
Guild.update({ id: guild_id }, { $inc: { member_count: -1 } }),
emitEvent({
event: "GUILD_DELETE",
@@ -143,17 +141,17 @@ export async function removeMember(user_id: string, guild_id: string) {
export async function addRole(user_id: string, guild_id: string, role_id: string) {
const user = await getPublicUser(user_id);
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id });
if (!role) throw new HTTPError("role not found", 404);
var memberObj = await MemberModel.findOneAndUpdate(
var memberObj = await Member.findOneOrFailAndUpdate(
{
id: user_id,
guild_id: guild_id
},
{ $push: { roles: role_id } },
{ new: true }
).exec();
);
if (!memberObj) throw new HTTPError("Member not found", 404);
@@ -171,17 +169,17 @@ export async function addRole(user_id: string, guild_id: string, role_id: string
export async function removeRole(user_id: string, guild_id: string, role_id: string) {
const user = await getPublicUser(user_id);
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id });
if (!role) throw new HTTPError("role not found", 404);
var memberObj = await MemberModel.findOneAndUpdate(
var memberObj = await Member.findOneOrFailAndUpdate(
{
id: user_id,
guild_id: guild_id
},
{ $pull: { roles: role_id } },
{ new: true }
).exec();
);
if (!memberObj) throw new HTTPError("Member not found", 404);
@@ -199,14 +197,14 @@ export async function removeRole(user_id: string, guild_id: string, role_id: str
export async function changeNickname(user_id: string, guild_id: string, nickname: string) {
const user = await getPublicUser(user_id);
var memberObj = await MemberModel.findOneAndUpdate(
var memberObj = await Member.findOneOrFailAndUpdate(
{
id: user_id,
guild_id: guild_id
},
{ nick: nickname },
{ new: true }
).exec();
);
if (!memberObj) throw new HTTPError("Member not found", 404);
+12 -15
View File
@@ -1,5 +1,5 @@
import {
ChannelModel,
Channel,
Embed,
emitEvent,
Message,
@@ -7,13 +7,11 @@ import {
MessageUpdateEvent,
getPermission,
CHANNEL_MENTION,
toObject,
MessageModel,
Snowflake,
PublicMemberProjection,
USER_MENTION,
ROLE_MENTION,
RoleModel,
Role,
EVERYONE_MENTION,
HERE_MENTION
} from "@fosscord/util";
@@ -37,13 +35,11 @@ const DEFAULT_FETCH_OPTIONS: any = {
method: "GET"
};
export async function handleMessage(opts: Partial<Message>) {
const channel = await ChannelModel.findOne(
export async function handleMessage(opts: Partial<Message>): Promise<Message> {
const channel = await Channel.findOneOrFail(
{ id: opts.channel_id },
{ guild_id: true, type: true, permission_overwrites: true, recipient_ids: true, owner_id: true }
)
.lean() // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids
.exec();
{ select: ["guild_id", "type", "permission_overwrites", "recipient_ids", "owner_id"] }
); // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids
if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404);
// TODO: are tts messages allowed in dm channels? should permission be checked?
@@ -83,7 +79,7 @@ export async function handleMessage(opts: Partial<Message>) {
await Promise.all(
Array.from(content.matchAll(ROLE_MENTION)).map(async ([_, mention]) => {
const role = await RoleModel.findOne({ id: mention, guild_id: channel.guild_id }).exec();
const role = await Role.findOneOrFail({ id: mention, guild_id: channel.guild_id });
if (role.mentionable || permission.has("MANAGE_ROLES")) {
mention_role_ids.push(mention);
}
@@ -160,16 +156,17 @@ export async function postHandleMessage(message: Message) {
channel_id: message.channel_id,
data
} as MessageUpdateEvent),
MessageModel.updateOne({ id: message.id, channel_id: message.channel_id }, data).exec()
Message.update({ id: message.id, channel_id: message.channel_id }, data)
]);
}
export async function sendMessage(opts: Partial<Message>) {
const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() });
const data = toObject(
await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).populate("referenced_message").save()
);
const data = await new Message(message)
.populate({ path: "member", select: PublicMemberProjection })
.populate("referenced_message")
.save();
await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data } as MessageCreateEvent);
+4 -4
View File
@@ -1,16 +1,16 @@
import { toObject, UserModel, PublicUserProjection } from "@fosscord/util";
import { toObject, User, PublicUserProjection } from "@fosscord/util";
import { HTTPError } from "lambert-server";
export { PublicUserProjection };
export async function getPublicUser(user_id: string, additional_fields?: any) {
const user = await UserModel.findOne(
const user = await User.findOneOrFail(
{ id: user_id },
{
...PublicUserProjection,
...additional_fields
}
).exec();
);
if (!user) throw new HTTPError("User not found", 404);
return toObject(user);
return user;
}