This commit is contained in:
Flam3rboy
2021-04-24 12:49:12 +02:00
47 changed files with 530 additions and 773 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ import { Request, Response, Router } from "express";
import { check, FieldErrors, Length } from "../../util/instanceOf";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
import { User, UserModel } from "fosscord-server-util";
import { UserModel } from "@fosscord/server-util";
import Config from "../../util/Config";
import { adjustEmail } from "./register";
+1 -1
View File
@@ -1,6 +1,6 @@
import { Request, Response, Router } from "express";
import Config from "../../util/Config";
import { trimSpecial, User, Snowflake, UserModel } from "fosscord-server-util";
import { trimSpecial, User, Snowflake, UserModel } from "@fosscord/server-util";
import bcrypt from "bcrypt";
import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf";
import "missing-native-js-functions";
@@ -1,4 +1,14 @@
import { Router } from "express";
const router: Router = Router();
// TODO:
export default router;
/**
*
* @param {"webhook_channel_id":"754001514330062952"}
*
* Creates a WebHook in the channel and returns the id of it
*
* @returns {"channel_id": "816382962056560690", "webhook_id": "834910735095037962"}
*/
+26
View File
@@ -1,4 +1,30 @@
import { ChannelModel, getPermission, toObject } from "@fosscord/server-util";
import { Router } from "express";
import { HTTPError } from "lambert-server";
const router: Router = Router();
// TODO: delete channel
// TODO: Get channel
router.delete("/", async(req,res)=>{
const {channel_id} = req.params
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.guild_id) {
const permission = await getPermission(req.user_id, channel.guild_id)
permission.hasThrow("MANAGE_CHANNELS")
// TODO Channel Update Gateway event will fire for each of them
await ChannelModel.updateMany({parent_id: channel_id}, {$set: {channel_id: null}}).exec()
await ChannelModel.deleteOne({id: channel_id})
}
// TODO: Dm channel "close" not delete
const data = toObject(channel);
//TODO: Reload channel list if request successful
res.send(data)
})
export default router;
+3 -9
View File
@@ -7,7 +7,7 @@ import { emitEvent } from "../../../util/Event";
import { InviteCreateSchema } from "../../../schema/Invite";
import { getPermission, ChannelModel, InviteModel, InviteCreateEvent, toObject } from "fosscord-server-util";
import { getPermission, ChannelModel, InviteModel, InviteCreateEvent, toObject } from "@fosscord/server-util";
const router: Router = Router();
@@ -22,10 +22,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
const { guild_id } = channel;
const permission = await getPermission(user_id, guild_id);
if (!permission.has("CREATE_INSTANT_INVITE")) {
throw new HTTPError("You aren't authorised to access this endpoint", 401);
}
permission.hasThrow("CREATE_INSTANT_INVITE");
const invite = {
code: random(),
@@ -55,10 +52,7 @@ router.get("/", async (req: Request, res: Response) => {
}
const { guild_id } = channel;
const permission = await getPermission(user_id, guild_id);
if (!permission.has("MANAGE_CHANNELS")) {
throw new HTTPError("You aren't authorised to access this endpoint", 401);
}
permission.hasThrow("MANAGE_CHANNELS");
const invites = await InviteModel.find({ guild_id }).exec();
@@ -0,0 +1,8 @@
import { Router } from "express";
const router = Router();
// TODO:
// router.post("/", (req, res) => {});
export default router;
@@ -0,0 +1,6 @@
import { Router } from "express";
const router = Router();
// TODO:
export default router;
@@ -0,0 +1,6 @@
import { Router } from "express";
const router = Router();
// TODO:
export default router;
@@ -1,5 +1,5 @@
import { Router } from "express";
import { ChannelModel, getPermission, MessageDeleteBulkEvent, MessageModel } from "fosscord-server-util";
import { ChannelModel, getPermission, MessageDeleteBulkEvent, MessageModel } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import Config from "../../../../util/Config";
import { emitEvent } from "../../../../util/Event";
@@ -13,12 +13,12 @@ export default router;
// 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
router.post("/", check({ messages: [String] }), async (req, res) => {
const channel_id = 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();
if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
if (!permission.has("MANAGE_MESSAGES")) throw new HTTPError("You are missing the MANAGE_MESSAGES permissions");
permission.hasThrow("MANAGE_MESSAGES");
const { maxBulkDelete } = Config.get().limits.message;
@@ -9,7 +9,7 @@ import {
MessageModel,
Snowflake,
toObject,
} from "fosscord-server-util";
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { MessageCreateSchema } from "../../../../schema/Message";
import { check, instanceOf, Length } from "../../../../util/instanceOf";
@@ -22,7 +22,7 @@ const router: Router = Router();
export default router;
function isTextChannel(type: ChannelType): boolean {
export function isTextChannel(type: ChannelType): boolean {
switch (type) {
case ChannelType.GUILD_VOICE:
case ChannelType.GUILD_CATEGORY:
@@ -62,7 +62,8 @@ router.get("/", async (req, res) => {
if (channel.guild_id) {
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id, { channel });
if (!permissions.has("VIEW_CHANNEL")) throw new HTTPError("You don't have permission to view this channel", 401);
permissions.hasThrow("VIEW_CHANNEL");
if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
} else if (channel.recipients) {
// group/dm channel
@@ -106,11 +107,10 @@ router.post("/", check(MessageCreateSchema), async (req, res) => {
if (channel.guild_id) {
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id, { channel });
if (!permissions.has("SEND_MESSAGES")) throw new HTTPError("You don't have the SEND_MESSAGES permission");
if (body.tts && !permissions.has("SEND_TTS_MESSAGES")) throw new HTTPError("You are missing the SEND_TTS_MESSAGES permission");
permissions.hasThrow("SEND_MESSAGES");
if (body.tts) permissions.hasThrow("SEND_TTS_MESSAGES");
if (body.message_reference) {
if (!permissions.has("READ_MESSAGE_HISTORY"))
throw new HTTPError("You are missing the READ_MESSAGE_HISTORY permission to reply");
permissions.hasThrow("READ_MESSAGE_HISTORY");
if (body.message_reference.guild_id !== channel.guild_id)
throw new HTTPError("You can only reference messages from this guild");
}
@@ -1,4 +1,5 @@
import { Router } from "express";
const router: Router = Router();
// TODO:
export default router;
+1
View File
@@ -1,4 +1,5 @@
import { Router } from "express";
const router: Router = Router();
// TODO:
export default router;
@@ -1,4 +1,5 @@
import { Router } from "express";
const router: Router = Router();
// TODO:
export default router;
@@ -1,4 +1,5 @@
import { Router } from "express";
const router: Router = Router();
// TODO:
export default router;
@@ -1,4 +1,27 @@
import { Router } from "express";
import { check, Length } from "../../../util/instanceOf";
import { ChannelModel, getPermission, trimSpecial } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { isTextChannel } from "./messages/index";
const router: Router = Router();
// TODO:
// TODO: use Image Data Type for avatar instead of String
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req, res) => {
const channel_id = req.params.channel_id;
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec();
if (!channel) throw new HTTPError("Channel not found", 404);
isTextChannel(channel.type);
if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400);
const permission = await getPermission(req.user_id, channel.guild_id);
permission.hasThrow("MANAGE_WEBHOOKS");
var { avatar, name } = req.body as { name: string; avatar?: string };
name = trimSpecial(name);
if (name === "clyde") throw new HTTPError("Invalid name", 400);
});
export default router;
+3 -5
View File
@@ -1,5 +1,5 @@
import { Request, Response, Router } from "express";
import { BanModel, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, GuildModel, toObject } from "fosscord-server-util";
import { BanModel, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, GuildModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { getIpAdress } from "../../../middlewares/GlobalRateLimit";
import { BanCreateSchema } from "../../../schema/Ban";
@@ -35,7 +35,7 @@ router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Respo
const banned_user = await getPublicUser(banned_user_id);
const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("BAN_MEMBERS")) throw new HTTPError("You don't have the permission to ban members", 403);
perms.hasThrow("BAN_MEMBERS");
if (req.user_id === banned_user_id) throw new HTTPError("You can't ban yourself", 400);
await removeMember(banned_user_id, guild_id);
@@ -69,9 +69,7 @@ router.delete("/:user_id", async (req: Request, res: Response) => {
if (!guild) throw new HTTPError("Guild not found", 404);
const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("BAN_MEMBERS")) {
throw new HTTPError("No permissions", 403);
}
perms.hasThrow("BAN_MEMBERS");
await BanModel.deleteOne({
user_id: banned_user_id,
+1 -1
View File
@@ -1,5 +1,5 @@
import { Router } from "express";
import { ChannelCreateEvent, ChannelModel, ChannelType, GuildModel, Snowflake, toObject } from "fosscord-server-util";
import { ChannelCreateEvent, ChannelModel, ChannelType, GuildModel, Snowflake, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel";
import { emitEvent } from "../../../util/Event";
+2 -2
View File
@@ -12,7 +12,7 @@ import {
RoleModel,
toObject,
UserModel,
} from "fosscord-server-util";
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { GuildUpdateSchema } from "../../../schema/Guild";
import { emitEvent } from "../../../util/Event";
@@ -41,7 +41,7 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response)
// TODO: guild update check image
const perms = await getPermission(req.user_id, guild_id);
if (!perms.has("MANAGE_GUILD")) throw new HTTPError("You do not have the MANAGE_GUILD permission", 401);
perms.hasThrow("MANAGE_GUILD");
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
.populate({ path: "joined_at", match: { id: req.user_id } })
+1 -1
View File
@@ -1,5 +1,5 @@
import { Request, Response, Router } from "express";
import { GuildModel, MemberModel, toObject } from "fosscord-server-util";
import { GuildModel, MemberModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { instanceOf, Length } from "../../../util/instanceOf";
import { PublicMemberProjection } from "../../../util/Member";
+3 -1
View File
@@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { RoleModel, GuildModel, Snowflake, Guild } from "fosscord-server-util";
import { RoleModel, GuildModel, Snowflake, Guild } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { check } from "./../../util/instanceOf";
import { GuildCreateSchema } from "../../schema/Guild";
@@ -9,6 +9,8 @@ import { addMember } from "../../util/Member";
const router: Router = Router();
//TODO: create default channel
router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) => {
const body = req.body as GuildCreateSchema;
+3 -2
View File
@@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { getPermission, InviteModel, toObject } from "fosscord-server-util";
import { getPermission, InviteModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
const router: Router = Router();
@@ -21,7 +21,8 @@ router.delete("/:invite_code", async (req: Request, res: Response) => {
const { guild_id, channel_id } = invite;
const perms = await getPermission(req.user_id, guild_id, channel_id);
if (!perms.has("MANAGE_GUILD") || !perms.has("MANAGE_CHANNELS")) throw new HTTPError("You aren't allow", 401);
if (!perms.has("MANAGE_GUILD") && !perms.has("MANAGE_CHANNELS"))
throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401);
await InviteModel.deleteOne({ code }).exec();
+19
View File
@@ -0,0 +1,19 @@
import { Router, Request, Response } from "express";
import { UserModel, toObject } from "@fosscord/server-util";
import { getPublicUser } from "../../../util/User";
import { HTTPError } from "lambert-server";
import { UserUpdateSchema } from "../../../schema/User";
import { check } from "../../../util/instanceOf";
const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
const { id } = req.params;
const user = await getPublicUser(id);
if (!user) throw new HTTPError("User not found", 404);
res.json(user);
});
export default router;
+69
View File
@@ -0,0 +1,69 @@
import {
Router,
Request,
Response
} from "express";
import {
ChannelModel,
ChannelCreateEvent,
DMChannel,
UserModel,
toObject,
ChannelType,
Snowflake
} from "@fosscord/server-util";
import {
HTTPError
} from "lambert-server";
import {
emitEvent
} from "../../../util/Event";
import {
getPublicUser
} from "../../../util/User";
import {
DmChannelCreateSchema
} from "../../../schema/Channel";
import {
check
} from "../../../util/instanceOf";
const router: Router = Router();
router.get("/", async (req: Request, res: Response) => {
const user = await UserModel.findOne({
id: req.user_id
}, {
guilds: true
}).exec();
if (!user) throw new HTTPError("User not found", 404);
var testID = "829044530203328513"; //FOR TEST
var channels = await ChannelModel.find({
recipients: req.user_id,
type: 1
}).exec();
res.json(toObject(channels));
});
router.post("/", check(DmChannelCreateSchema), async (req, res) => {
const body = req.body as DmChannelCreateSchema;
const channel = {
...body,
owner_id: req.user_id,
id: Snowflake.generate(),
type: ChannelType.DM,
created_at: new Date(),
};
await new ChannelModel(channel).save();
/*Event({ event: "CHANNEL_CREATE", data: channel } as ChannelCreateEvent);*/
res.json(channel);
});
export default router;
+1 -1
View File
@@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { GuildModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject } from "fosscord-server-util";
import { GuildModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
import { getPublicUser } from "../../../util/User";
+1 -1
View File
@@ -1,5 +1,5 @@
import { Router, Request, Response } from "express";
import { UserModel } from "fosscord-server-util";
import { UserModel } from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
const router: Router = Router();