findOne auto throws error if it doesn't exist'
This commit is contained in:
parent
52ba4982be
commit
f5b301a161
30
package-lock.json
generated
30
package-lock.json
generated
@ -9,7 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fosscord/server-util": "^1.3.26",
|
"@fosscord/server-util": "^1.3.29",
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
"@types/json-schema": "^7.0.7",
|
"@types/json-schema": "^7.0.7",
|
||||||
"ajv": "^8.4.0",
|
"ajv": "^8.4.0",
|
||||||
@ -39,6 +39,9 @@
|
|||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"require_optional": "^1.0.1"
|
"require_optional": "^1.0.1"
|
||||||
},
|
},
|
||||||
|
"bin": {
|
||||||
|
"api": "dist/start.js"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bcrypt": "^3.0.0",
|
"@types/bcrypt": "^3.0.0",
|
||||||
"@types/express": "^4.17.9",
|
"@types/express": "^4.17.9",
|
||||||
@ -51,6 +54,7 @@
|
|||||||
"0x": "^4.10.2",
|
"0x": "^4.10.2",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
"pkg": "^5.3.0",
|
"pkg": "^5.3.0",
|
||||||
|
"saslprep": "^1.0.3",
|
||||||
"ts-node": "^9.1.1",
|
"ts-node": "^9.1.1",
|
||||||
"ts-node-dev": "^1.1.6",
|
"ts-node-dev": "^1.1.6",
|
||||||
"typescript": "^4.1.2"
|
"typescript": "^4.1.2"
|
||||||
@ -551,9 +555,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@fosscord/server-util": {
|
"node_modules/@fosscord/server-util": {
|
||||||
"version": "1.3.26",
|
"version": "1.3.29",
|
||||||
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.26.tgz",
|
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.29.tgz",
|
||||||
"integrity": "sha512-c6SDflivNVk6gVbFm7FLWVOBTyaiO7ZSiEttCCZQ8dD3kKu0oMocDoHH5S1f7xCpBjFY9igfa/P6mrPOJrUlKw==",
|
"integrity": "sha512-lxb8LGezMG1yo9kR6CVlWN8+pjsduCG1b8wkYsAjv6sqjFz7mgKUo/rvxUenJTCspDHxDF/3RuYBLdQEtVEVXg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/mongoose-autopopulate": "^0.10.1",
|
"@types/mongoose-autopopulate": "^0.10.1",
|
||||||
@ -6403,7 +6407,7 @@
|
|||||||
"version": "1.5.0",
|
"version": "1.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
|
||||||
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
|
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
|
||||||
"optional": true
|
"devOptional": true
|
||||||
},
|
},
|
||||||
"node_modules/meow": {
|
"node_modules/meow": {
|
||||||
"version": "3.7.0",
|
"version": "3.7.0",
|
||||||
@ -8966,7 +8970,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
|
||||||
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
|
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
|
||||||
"optional": true,
|
"devOptional": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"sparse-bitfield": "^3.0.3"
|
"sparse-bitfield": "^3.0.3"
|
||||||
},
|
},
|
||||||
@ -9462,7 +9466,7 @@
|
|||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
|
||||||
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
|
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
|
||||||
"optional": true,
|
"devOptional": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"memory-pager": "^1.0.2"
|
"memory-pager": "^1.0.2"
|
||||||
}
|
}
|
||||||
@ -11470,9 +11474,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@fosscord/server-util": {
|
"@fosscord/server-util": {
|
||||||
"version": "1.3.26",
|
"version": "1.3.29",
|
||||||
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.26.tgz",
|
"resolved": "https://registry.npmjs.org/@fosscord/server-util/-/server-util-1.3.29.tgz",
|
||||||
"integrity": "sha512-c6SDflivNVk6gVbFm7FLWVOBTyaiO7ZSiEttCCZQ8dD3kKu0oMocDoHH5S1f7xCpBjFY9igfa/P6mrPOJrUlKw==",
|
"integrity": "sha512-lxb8LGezMG1yo9kR6CVlWN8+pjsduCG1b8wkYsAjv6sqjFz7mgKUo/rvxUenJTCspDHxDF/3RuYBLdQEtVEVXg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@types/jsonwebtoken": "^8.5.0",
|
"@types/jsonwebtoken": "^8.5.0",
|
||||||
"@types/mongoose-autopopulate": "^0.10.1",
|
"@types/mongoose-autopopulate": "^0.10.1",
|
||||||
@ -16435,7 +16439,7 @@
|
|||||||
"version": "1.5.0",
|
"version": "1.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",
|
||||||
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
|
"integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==",
|
||||||
"optional": true
|
"devOptional": true
|
||||||
},
|
},
|
||||||
"meow": {
|
"meow": {
|
||||||
"version": "3.7.0",
|
"version": "3.7.0",
|
||||||
@ -18493,7 +18497,7 @@
|
|||||||
"version": "1.0.3",
|
"version": "1.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz",
|
||||||
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
|
"integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==",
|
||||||
"optional": true,
|
"devOptional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"sparse-bitfield": "^3.0.3"
|
"sparse-bitfield": "^3.0.3"
|
||||||
}
|
}
|
||||||
@ -18923,7 +18927,7 @@
|
|||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",
|
||||||
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
|
"integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=",
|
||||||
"optional": true,
|
"devOptional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"memory-pager": "^1.0.2"
|
"memory-pager": "^1.0.2"
|
||||||
}
|
}
|
||||||
|
12
package.json
12
package.json
@ -11,7 +11,7 @@
|
|||||||
"start": "npm run build && node dist/start",
|
"start": "npm run build && node dist/start",
|
||||||
"build": "tsc -b .",
|
"build": "tsc -b .",
|
||||||
"dev": "tsnd --respawn src/start.ts",
|
"dev": "tsnd --respawn src/start.ts",
|
||||||
"bundle": "pkg package.json"
|
"bundle": "npm run build && pkg package.json"
|
||||||
},
|
},
|
||||||
"pkg": {
|
"pkg": {
|
||||||
"scripts": [
|
"scripts": [
|
||||||
@ -22,8 +22,11 @@
|
|||||||
"assets"
|
"assets"
|
||||||
],
|
],
|
||||||
"targets": [
|
"targets": [
|
||||||
"latest-win-x64"
|
"latest-linux-x64",
|
||||||
]
|
"latest-win-x64",
|
||||||
|
"latest-macos-x64"
|
||||||
|
],
|
||||||
|
"outputPath": "build"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -43,7 +46,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
"homepage": "https://github.com/fosscord/fosscord-api#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fosscord/server-util": "^1.3.26",
|
"@fosscord/server-util": "^1.3.29",
|
||||||
"@types/jest": "^26.0.22",
|
"@types/jest": "^26.0.22",
|
||||||
"@types/json-schema": "^7.0.7",
|
"@types/json-schema": "^7.0.7",
|
||||||
"ajv": "^8.4.0",
|
"ajv": "^8.4.0",
|
||||||
@ -85,6 +88,7 @@
|
|||||||
"0x": "^4.10.2",
|
"0x": "^4.10.2",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
"pkg": "^5.3.0",
|
"pkg": "^5.3.0",
|
||||||
|
"saslprep": "^1.0.3",
|
||||||
"ts-node": "^9.1.1",
|
"ts-node": "^9.1.1",
|
||||||
"ts-node-dev": "^1.1.6",
|
"ts-node-dev": "^1.1.6",
|
||||||
"typescript": "^4.1.2"
|
"typescript": "^4.1.2"
|
||||||
|
@ -9,6 +9,7 @@ import RateLimit from "../../middlewares/RateLimit";
|
|||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
export default router;
|
export default router;
|
||||||
|
|
||||||
|
// TODO: check if user is deleted/restricted
|
||||||
router.post(
|
router.post(
|
||||||
"/",
|
"/",
|
||||||
RateLimit({ count: 5, window: 60, onylIp: true }),
|
RateLimit({ count: 5, window: 60, onylIp: true }),
|
||||||
@ -43,11 +44,25 @@ router.post(
|
|||||||
// TODO: check captcha
|
// TODO: check captcha
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await UserModel.findOne({ $or: query }, `user_data.hash id user_settings.locale user_settings.theme`).exec();
|
const user = await UserModel.findOne(
|
||||||
|
{ $or: query },
|
||||||
if (!user) {
|
{
|
||||||
throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } });
|
projection: {
|
||||||
}
|
user_data: {
|
||||||
|
hash: true
|
||||||
|
},
|
||||||
|
id: true,
|
||||||
|
user_settings: {
|
||||||
|
locale: true,
|
||||||
|
theme: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.exec()
|
||||||
|
.catch((e) => {
|
||||||
|
throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } });
|
||||||
|
});
|
||||||
|
|
||||||
// the salt is saved in the password refer to bcrypt docs
|
// the salt is saved in the password refer to bcrypt docs
|
||||||
const same_password = await bcrypt.compare(password, user.user_data.hash);
|
const same_password = await bcrypt.compare(password, user.user_data.hash);
|
||||||
|
@ -93,7 +93,9 @@ router.post(
|
|||||||
if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } });
|
if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } });
|
||||||
|
|
||||||
// check if there is already an account with this email
|
// check if there is already an account with this email
|
||||||
const exists = await UserModel.findOne({ email: adjusted_email }).exec();
|
const exists = await UserModel.findOne({ email: adjusted_email })
|
||||||
|
.exec()
|
||||||
|
.catch((e) => {});
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
throw FieldErrors({
|
throw FieldErrors({
|
||||||
@ -130,7 +132,9 @@ router.post(
|
|||||||
|
|
||||||
if (!register.allowMultipleAccounts) {
|
if (!register.allowMultipleAccounts) {
|
||||||
// TODO: check if fingerprint was eligible generated
|
// TODO: check if fingerprint was eligible generated
|
||||||
const exists = await UserModel.findOne({ fingerprints: fingerprint }).exec();
|
const exists = await UserModel.findOne({ fingerprints: fingerprint })
|
||||||
|
.exec()
|
||||||
|
.catch((e) => {});
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
throw FieldErrors({
|
throw FieldErrors({
|
||||||
@ -165,8 +169,12 @@ router.post(
|
|||||||
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
|
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
|
||||||
for (let tries = 0; tries < 5; tries++) {
|
for (let tries = 0; tries < 5; tries++) {
|
||||||
discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
|
discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
|
||||||
exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec();
|
try {
|
||||||
if (!exists) break;
|
exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec();
|
||||||
|
} catch (error) {
|
||||||
|
// doesn't exist -> break
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
|
@ -12,7 +12,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
permission.hasThrow("VIEW_CHANNEL");
|
permission.hasThrow("VIEW_CHANNEL");
|
||||||
@ -24,7 +23,6 @@ router.delete("/", async (req: Request, res: Response) => {
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
|
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
|
||||||
permission.hasThrow("MANAGE_CHANNELS");
|
permission.hasThrow("MANAGE_CHANNELS");
|
||||||
@ -47,7 +45,6 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response
|
|||||||
permission.hasThrow("MANAGE_CHANNELS");
|
permission.hasThrow("MANAGE_CHANNELS");
|
||||||
|
|
||||||
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
|
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const data = toObject(channel);
|
const data = toObject(channel);
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
|
|
||||||
if (!channel || !channel.guild_id) {
|
if (!channel.guild_id) {
|
||||||
throw new HTTPError("This channel doesn't exist", 404);
|
throw new HTTPError("This channel doesn't exist", 404);
|
||||||
}
|
}
|
||||||
const { guild_id } = channel;
|
const { guild_id } = channel;
|
||||||
@ -50,7 +50,7 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
|
|
||||||
if (!channel || !channel.guild_id) {
|
if (!channel.guild_id) {
|
||||||
throw new HTTPError("This channel doesn't exist", 404);
|
throw new HTTPError("This channel doesn't exist", 404);
|
||||||
}
|
}
|
||||||
const { guild_id } = channel;
|
const { guild_id } = channel;
|
||||||
|
@ -13,7 +13,6 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response
|
|||||||
var body = req.body as MessageCreateSchema;
|
var body = req.body as MessageCreateSchema;
|
||||||
|
|
||||||
var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec();
|
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);
|
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||||
|
|
||||||
@ -30,8 +29,8 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response
|
|||||||
edited_timestamp: new Date()
|
edited_timestamp: new Date()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec();
|
message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "MESSAGE_UPDATE",
|
event: "MESSAGE_UPDATE",
|
||||||
@ -51,9 +50,7 @@ router.delete("/", async (req: Request, res: Response) => {
|
|||||||
const { message_id, channel_id } = req.params;
|
const { message_id, channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true });
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true });
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec();
|
const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES");
|
if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES");
|
||||||
|
@ -39,13 +39,11 @@ router.delete("/", async (req: Request, res: Response) => {
|
|||||||
const { message_id, channel_id } = req.params;
|
const { message_id, channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||||
permissions.hasThrow("MANAGE_MESSAGES");
|
permissions.hasThrow("MANAGE_MESSAGES");
|
||||||
|
|
||||||
const message = await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec();
|
await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "MESSAGE_REACTION_REMOVE_ALL",
|
event: "MESSAGE_REACTION_REMOVE_ALL",
|
||||||
@ -66,13 +64,11 @@ router.delete("/:emoji", async (req: Request, res: Response) => {
|
|||||||
const emoji = getEmoji(req.params.emoji);
|
const emoji = getEmoji(req.params.emoji);
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||||
permissions.hasThrow("MANAGE_MESSAGES");
|
permissions.hasThrow("MANAGE_MESSAGES");
|
||||||
|
|
||||||
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
|
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
|
||||||
if (!already_added) throw new HTTPError("Reaction not found", 404);
|
if (!already_added) throw new HTTPError("Reaction not found", 404);
|
||||||
@ -118,10 +114,7 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => {
|
|||||||
const emoji = getEmoji(req.params.emoji);
|
const emoji = getEmoji(req.params.emoji);
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
|
const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name);
|
||||||
|
|
||||||
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||||
@ -130,7 +123,6 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
if (emoji.id) {
|
if (emoji.id) {
|
||||||
const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec();
|
const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec();
|
||||||
if (!external_emoji) throw new HTTPError("Emoji not found", 404);
|
|
||||||
if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS");
|
if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS");
|
||||||
emoji.animated = external_emoji.animated;
|
emoji.animated = external_emoji.animated;
|
||||||
emoji.name = external_emoji.name;
|
emoji.name = external_emoji.name;
|
||||||
@ -168,10 +160,7 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => {
|
|||||||
const emoji = getEmoji(req.params.emoji);
|
const emoji = getEmoji(req.params.emoji);
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
const message = await MessageModel.findOne({ id: message_id, channel_id }).exec();
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ export default router;
|
|||||||
router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => {
|
router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => {
|
||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec();
|
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);
|
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 });
|
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel });
|
||||||
permission.hasThrow("MANAGE_MESSAGES");
|
permission.hasThrow("MANAGE_MESSAGES");
|
||||||
|
@ -31,7 +31,6 @@ export function isTextChannel(type: ChannelType): boolean {
|
|||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
const channel_id = req.params.channel_id;
|
const channel_id = req.params.channel_id;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec();
|
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);
|
|
||||||
|
|
||||||
isTextChannel(channel.type);
|
isTextChannel(channel.type);
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number,
|
|||||||
const body = req.body as { allow: bigint; deny: bigint; type: number; id: string };
|
const body = req.body as { allow: bigint; deny: bigint; type: number; id: string };
|
||||||
|
|
||||||
var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, permission_overwrites: true }).exec();
|
var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, permission_overwrites: true }).exec();
|
||||||
if (!channel || !channel.guild_id) throw new HTTPError("Channel not found", 404);
|
if (!channel.guild_id) throw new HTTPError("Channel not found", 404);
|
||||||
|
|
||||||
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
permissions.hasThrow("MANAGE_ROLES");
|
permissions.hasThrow("MANAGE_ROLES");
|
||||||
@ -38,8 +38,8 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number,
|
|||||||
overwrite.allow = body.allow;
|
overwrite.allow = body.allow;
|
||||||
overwrite.deny = body.deny;
|
overwrite.deny = body.deny;
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel).exec();
|
channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "CHANNEL_UPDATE",
|
event: "CHANNEL_UPDATE",
|
||||||
@ -59,7 +59,7 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => {
|
|||||||
permissions.hasThrow("MANAGE_ROLES");
|
permissions.hasThrow("MANAGE_ROLES");
|
||||||
|
|
||||||
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } });
|
const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } });
|
||||||
if (!channel || !channel.guild_id) throw new HTTPError("Channel not found", 404);
|
if (!channel.guild_id) throw new HTTPError("Channel not found", 404);
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "CHANNEL_UPDATE",
|
event: "CHANNEL_UPDATE",
|
||||||
|
@ -12,12 +12,10 @@ import { HTTPError } from "lambert-server";
|
|||||||
import { emitEvent } from "../../../util/Event";
|
import { emitEvent } from "../../../util/Event";
|
||||||
|
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
// TODO: auto throw error if findOne doesn't find anything
|
|
||||||
|
|
||||||
router.put("/:message_id", async (req: Request, res: Response) => {
|
router.put("/:message_id", async (req: Request, res: Response) => {
|
||||||
const { channel_id, message_id } = req.params;
|
const { channel_id, message_id } = req.params;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
permission.hasThrow("VIEW_CHANNEL");
|
permission.hasThrow("VIEW_CHANNEL");
|
||||||
|
|
||||||
@ -30,7 +28,6 @@ router.put("/:message_id", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec();
|
await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec();
|
||||||
const message = toObject(await MessageModel.findOne({ id: message_id }).exec());
|
const message = toObject(await MessageModel.findOne({ id: message_id }).exec());
|
||||||
if (!message) throw new HTTPError("Message not found", 404);
|
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "MESSAGE_UPDATE",
|
event: "MESSAGE_UPDATE",
|
||||||
@ -57,7 +54,6 @@ router.delete("/:message_id", async (req: Request, res: Response) => {
|
|||||||
const { channel_id, message_id } = req.params;
|
const { channel_id, message_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
permission.hasThrow("VIEW_CHANNEL");
|
permission.hasThrow("VIEW_CHANNEL");
|
||||||
@ -90,7 +86,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
|
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
const channel = await ChannelModel.findOne({ id: channel_id }).exec();
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
|
||||||
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
const permission = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||||
permission.hasThrow("VIEW_CHANNEL");
|
permission.hasThrow("VIEW_CHANNEL");
|
||||||
|
|
||||||
|
@ -7,28 +7,26 @@ import { emitEvent } from "../../../util/Event";
|
|||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
router.post("/", async (req: Request, res: Response) => {
|
router.post("/", async (req: Request, res: Response) => {
|
||||||
const { channel_id } = req.params;
|
const { channel_id } = req.params;
|
||||||
const user_id = req.user_id;
|
const user_id = req.user_id;
|
||||||
const timestamp = Date.now()
|
const timestamp = Date.now();
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id });
|
const channel = await ChannelModel.findOne({ id: channel_id });
|
||||||
if (!channel) throw new HTTPError("Channel not found", 404)
|
const member = await MemberModel.findOne({ id: user_id }).exec();
|
||||||
const member = await MemberModel.findOne({ id: user_id }).exec()
|
|
||||||
if (!member) throw new HTTPError("Member not found", 404)
|
|
||||||
|
|
||||||
|
await emitEvent({
|
||||||
await emitEvent({
|
event: "TYPING_START",
|
||||||
event: "TYPING_START",
|
channel_id: channel_id,
|
||||||
channel_id: channel_id,
|
guild_id: channel.guild_id,
|
||||||
guild_id: channel.guild_id,
|
data: {
|
||||||
data: { // this is the paylod
|
// this is the paylod
|
||||||
member: toObject(member),
|
member: toObject(member),
|
||||||
channel_id,
|
channel_id,
|
||||||
timestamp,
|
timestamp,
|
||||||
user_id,
|
user_id,
|
||||||
guild_id: channel.guild_id
|
guild_id: channel.guild_id
|
||||||
}
|
}
|
||||||
} as TypingStartEvent)
|
} as TypingStartEvent);
|
||||||
res.sendStatus(204)
|
res.sendStatus(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -11,7 +11,6 @@ const router: Router = Router();
|
|||||||
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => {
|
router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => {
|
||||||
const channel_id = req.params.channel_id;
|
const channel_id = req.params.channel_id;
|
||||||
const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec();
|
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);
|
isTextChannel(channel.type);
|
||||||
if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400);
|
if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400);
|
||||||
|
@ -25,7 +25,6 @@ router.get("/:user", async (req: Request, res: Response) => {
|
|||||||
const user_id = req.params.ban;
|
const user_id = req.params.ban;
|
||||||
|
|
||||||
var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec();
|
var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec();
|
||||||
if (!ban) throw new HTTPError("Ban not found", 404);
|
|
||||||
return res.json(ban);
|
return res.json(ban);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -37,15 +37,7 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response
|
|||||||
const body = req.body as ChannelModifySchema;
|
const body = req.body as ChannelModifySchema;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
const channel = await ChannelModel.findOneAndUpdate({ guild_id }, body).exec();
|
||||||
|
|
||||||
const channel = {
|
|
||||||
...body
|
|
||||||
};
|
|
||||||
const channelm = await ChannelModel.find({ guild_id }).exec();
|
|
||||||
if (!channelm) throw new HTTPError("Channel not found", 404);
|
|
||||||
|
|
||||||
await new ChannelModel(channel).save();
|
|
||||||
|
|
||||||
await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent);
|
await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent);
|
||||||
|
|
||||||
|
@ -20,7 +20,6 @@ router.post("/", async (req: Request, res: Response) => {
|
|||||||
var { guild_id } = req.params;
|
var { guild_id } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
|
||||||
if (!guild) throw new HTTPError("This guild does not exist", 404);
|
|
||||||
if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
|
if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401);
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
|
@ -27,7 +27,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const guild = await GuildModel.findOne({ id: guild_id })
|
const guild = await GuildModel.findOne({ id: guild_id })
|
||||||
.populate({ path: "joined_at", match: { id: req.user_id } })
|
.populate({ path: "joined_at", match: { id: req.user_id } })
|
||||||
.exec();
|
.exec();
|
||||||
if (!guild) throw new HTTPError("Guild does not exist", 404);
|
|
||||||
|
|
||||||
const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id });
|
const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id });
|
||||||
if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401);
|
if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401);
|
||||||
|
@ -23,7 +23,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
await isMember(req.user_id, guild_id);
|
await isMember(req.user_id, guild_id);
|
||||||
|
|
||||||
const member = await MemberModel.findOne({ id: member_id, guild_id }).exec();
|
const member = await MemberModel.findOne({ id: member_id, guild_id }).exec();
|
||||||
if (!member) throw new HTTPError("Member not found", 404);
|
|
||||||
|
|
||||||
return res.json(toObject(member));
|
return res.json(toObject(member));
|
||||||
});
|
});
|
||||||
|
@ -11,7 +11,6 @@ const router = Router();
|
|||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
const { guild_id } = req.params;
|
const { guild_id } = req.params;
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
await isMember(req.user_id, guild_id);
|
await isMember(req.user_id, guild_id);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -35,10 +35,7 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) =>
|
|||||||
const body = req.body as RoleModifySchema;
|
const body = req.body as RoleModifySchema;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
||||||
if (!user) throw new HTTPError("User not found", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_ROLES");
|
perms.hasThrow("MANAGE_ROLES");
|
||||||
@ -71,11 +68,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => {
|
|||||||
const { role_id } = req.params;
|
const { role_id } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
if (!role_id) throw new HTTPError("Unknown role_id", 404);
|
|
||||||
|
|
||||||
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
||||||
if (!user) throw new HTTPError("User not found", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
|
|
||||||
@ -106,11 +99,7 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res
|
|||||||
const body = req.body as RoleModifySchema;
|
const body = req.body as RoleModifySchema;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
if (!role_id) throw new HTTPError("Unknown template_id", 404);
|
|
||||||
|
|
||||||
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
const user = await UserModel.findOne({ id: req.user_id }).exec();
|
||||||
if (!user) throw new HTTPError("User not found", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_ROLES");
|
perms.hasThrow("MANAGE_ROLES");
|
||||||
|
@ -21,7 +21,7 @@ const TemplateGuildProjection = {
|
|||||||
afk_channel_id: true,
|
afk_channel_id: true,
|
||||||
system_channel_id: true,
|
system_channel_id: true,
|
||||||
system_channel_flags: true,
|
system_channel_flags: true,
|
||||||
icon_hash: true,
|
icon_hash: true
|
||||||
};
|
};
|
||||||
|
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
@ -35,7 +35,6 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response
|
|||||||
const guild_id = req.params.guild_id;
|
const guild_id = req.params.guild_id;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
@ -47,7 +46,7 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response
|
|||||||
created_at: new Date(),
|
created_at: new Date(),
|
||||||
updated_at: new Date(),
|
updated_at: new Date(),
|
||||||
source_guild_id: guild_id,
|
source_guild_id: guild_id,
|
||||||
serialized_source_guild: guild,
|
serialized_source_guild: guild
|
||||||
}).save();
|
}).save();
|
||||||
|
|
||||||
res.json(toObject(template)).send();
|
res.json(toObject(template)).send();
|
||||||
@ -61,7 +60,7 @@ router.delete("/:code", async (req: Request, res: Response) => {
|
|||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
|
||||||
const template = await TemplateModel.findOneAndDelete({
|
const template = await TemplateModel.findOneAndDelete({
|
||||||
code,
|
code
|
||||||
}).exec();
|
}).exec();
|
||||||
|
|
||||||
res.send(toObject(template));
|
res.send(toObject(template));
|
||||||
@ -72,7 +71,6 @@ router.put("/:code", async (req: Request, res: Response) => {
|
|||||||
const { code } = req.params;
|
const { code } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
@ -8,8 +8,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { guild_id } = req.params;
|
const { guild_id } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
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);
|
if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204);
|
||||||
|
|
||||||
return res.json({ vanity_ur: guild.vanity_url });
|
return res.json({ vanity_ur: guild.vanity_url });
|
||||||
|
@ -13,7 +13,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const guild_id = req.params.guild_id;
|
const guild_id = req.params.guild_id;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id });
|
const guild = await GuildModel.findOne({ id: guild_id });
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
await isMember(req.user_id, guild_id);
|
await isMember(req.user_id, guild_id);
|
||||||
|
|
||||||
@ -25,22 +24,21 @@ router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Reques
|
|||||||
const body = req.body as GuildAddChannelToWelcomeScreenSchema;
|
const body = req.body as GuildAddChannelToWelcomeScreenSchema;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
var channelObject = {
|
var channelObject = {
|
||||||
...body
|
...body
|
||||||
}
|
};
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
|
||||||
if(!guild.welcome_screen.enabled) throw new HTTPError("Welcome screen disabled", 400);
|
if (!guild.welcome_screen.enabled) throw new HTTPError("Welcome screen disabled", 400);
|
||||||
if(guild.welcome_screen.welcome_channels.some(channel => channel.channel_id === body.channel_id)) throw new Error("Welcome Channel exists")
|
if (guild.welcome_screen.welcome_channels.some((channel) => channel.channel_id === body.channel_id))
|
||||||
|
throw new Error("Welcome Channel exists");
|
||||||
|
|
||||||
await GuildModel.findOneAndUpdate(
|
await GuildModel.findOneAndUpdate(
|
||||||
{
|
{
|
||||||
id: guild_id,
|
id: guild_id
|
||||||
},
|
},
|
||||||
{ $push: { "welcome_screen.welcome_channels": channelObject } }
|
{ $push: { "welcome_screen.welcome_channels": channelObject } }
|
||||||
).exec();
|
).exec();
|
||||||
@ -48,5 +46,4 @@ router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Reques
|
|||||||
res.sendStatus(204);
|
res.sendStatus(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
@ -18,7 +18,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { guild_id } = req.params;
|
const { guild_id } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild does not exist", 404);
|
|
||||||
if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404);
|
if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404);
|
||||||
|
|
||||||
// Fetch existing widget invite for widget channel
|
// Fetch existing widget invite for widget channel
|
||||||
@ -46,65 +45,63 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
// Fetch voice channels, and the @everyone permissions object
|
// Fetch voice channels, and the @everyone permissions object
|
||||||
let channels: any[] = [];
|
let channels: any[] = [];
|
||||||
await ChannelModel.find(
|
await ChannelModel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } })
|
||||||
{ guild_id: guild_id, type: 2 },
|
.lean()
|
||||||
{ permission_overwrites: { $elemMatch: { id: guild_id } } }
|
.select("id name position permission_overwrites")
|
||||||
).lean()
|
.sort({ position: 1 })
|
||||||
.select("id name position permission_overwrites")
|
.cursor()
|
||||||
.sort({ position: 1 })
|
.eachAsync((doc) => {
|
||||||
.cursor()
|
// Only return channels where @everyone has the CONNECT permission
|
||||||
.eachAsync(doc => {
|
if (
|
||||||
// Only return channels where @everyone has the CONNECT permission
|
doc.permission_overwrites === undefined ||
|
||||||
if (doc.permission_overwrites === undefined || Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT) {
|
Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT
|
||||||
channels.push(
|
) {
|
||||||
{
|
channels.push({
|
||||||
id: doc.id,
|
id: doc.id,
|
||||||
name: doc.name,
|
name: doc.name,
|
||||||
position: doc.position
|
position: doc.position
|
||||||
}
|
});
|
||||||
)
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Fetch members
|
// Fetch members
|
||||||
// TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file)
|
// TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file)
|
||||||
let members: any[] = [];
|
let members: any[] = [];
|
||||||
await MemberModel.find({ guild_id: guild_id })
|
await MemberModel.find({ guild_id: guild_id })
|
||||||
.lean()
|
.lean()
|
||||||
.populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } })
|
.populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } })
|
||||||
.select("id user nick deaf mute")
|
.select("id user nick deaf mute")
|
||||||
.cursor()
|
.cursor()
|
||||||
.eachAsync(doc => {
|
.eachAsync((doc) => {
|
||||||
const status = doc.user?.presence?.status || "offline";
|
const status = doc.user?.presence?.status || "offline";
|
||||||
if (status == "offline")return
|
if (status == "offline") return;
|
||||||
|
|
||||||
let item = {}
|
let item = {};
|
||||||
|
|
||||||
item = {
|
|
||||||
...item,
|
|
||||||
id: null, // this is updated during the sort outside of the query
|
|
||||||
username: doc.nick || doc.user?.username,
|
|
||||||
discriminator: "0000", // intended (https://github.com/discord/discord-api-docs/issues/1287)
|
|
||||||
avatar: null, // intended, avatar_url below will return a unique guild + user url to the avatar
|
|
||||||
status: status
|
|
||||||
}
|
|
||||||
|
|
||||||
const activity = doc.user?.presence?.activities?.[0];
|
|
||||||
if (activity) {
|
|
||||||
item = {
|
item = {
|
||||||
...item,
|
...item,
|
||||||
game: { name: activity.name }
|
id: null, // this is updated during the sort outside of the query
|
||||||
|
username: doc.nick || doc.user?.username,
|
||||||
|
discriminator: "0000", // intended (https://github.com/discord/discord-api-docs/issues/1287)
|
||||||
|
avatar: null, // intended, avatar_url below will return a unique guild + user url to the avatar
|
||||||
|
status: status
|
||||||
|
};
|
||||||
|
|
||||||
|
const activity = doc.user?.presence?.activities?.[0];
|
||||||
|
if (activity) {
|
||||||
|
item = {
|
||||||
|
...item,
|
||||||
|
game: { name: activity.name }
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: If the member is in a voice channel, return extra widget details
|
// TODO: If the member is in a voice channel, return extra widget details
|
||||||
// Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to)
|
// Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to)
|
||||||
// Get this from VoiceState
|
// Get this from VoiceState
|
||||||
|
|
||||||
|
// TODO: Implement a widget-avatar endpoint on the CDN, and implement logic here to request it
|
||||||
// TODO: Implement a widget-avatar endpoint on the CDN, and implement logic here to request it
|
// Get unique avatar url for guild user, cdn to serve the actual avatar image on this url
|
||||||
// Get unique avatar url for guild user, cdn to serve the actual avatar image on this url
|
/*
|
||||||
/*
|
|
||||||
const avatar = doc.user?.avatar;
|
const avatar = doc.user?.avatar;
|
||||||
if (avatar) {
|
if (avatar) {
|
||||||
const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003";
|
const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003";
|
||||||
@ -116,8 +113,8 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
members.push(item);
|
members.push(item);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort members, and update ids (Unable to do under the mongoose query due to https://mongoosejs.com/docs/faq.html#populate_sort_order)
|
// Sort members, and update ids (Unable to do under the mongoose query due to https://mongoosejs.com/docs/faq.html#populate_sort_order)
|
||||||
members = members.sort((first, second) => 0 - (first.username > second.username ? -1 : 1));
|
members = members.sort((first, second) => 0 - (first.username > second.username ? -1 : 1));
|
||||||
@ -133,7 +130,7 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
channels: channels,
|
channels: channels,
|
||||||
members: members,
|
members: members,
|
||||||
presence_count: guild.presence_count
|
presence_count: guild.presence_count
|
||||||
}
|
};
|
||||||
|
|
||||||
res.set("Cache-Control", "public, max-age=300");
|
res.set("Cache-Control", "public, max-age=300");
|
||||||
return res.json(data);
|
return res.json(data);
|
||||||
|
@ -14,7 +14,6 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const { guild_id } = req.params;
|
const { guild_id } = req.params;
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild does not exist", 404);
|
|
||||||
if (!guild.widget_enabled) throw new HTTPError("Unknown Guild", 404);
|
if (!guild.widget_enabled) throw new HTTPError("Unknown Guild", 404);
|
||||||
|
|
||||||
// Fetch guild information
|
// Fetch guild information
|
||||||
|
@ -14,9 +14,8 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
|
||||||
|
|
||||||
return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null});
|
return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null });
|
||||||
});
|
});
|
||||||
|
|
||||||
// https://discord.com/developers/docs/resources/guild#modify-guild-widget
|
// https://discord.com/developers/docs/resources/guild#modify-guild-widget
|
||||||
|
@ -11,7 +11,6 @@ router.get("/:code", async (req: Request, res: Response) => {
|
|||||||
const { code } = req.params;
|
const { code } = req.params;
|
||||||
|
|
||||||
const template = await TemplateModel.findOne({ id: code }).exec();
|
const template = await TemplateModel.findOne({ id: code }).exec();
|
||||||
if (!template) throw new HTTPError("template not found", 404);
|
|
||||||
|
|
||||||
res.json(toObject(template)).send();
|
res.json(toObject(template)).send();
|
||||||
});
|
});
|
||||||
@ -28,7 +27,6 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res
|
|||||||
}
|
}
|
||||||
|
|
||||||
const template = await TemplateModel.findOne({ code: code }).exec();
|
const template = await TemplateModel.findOne({ code: code }).exec();
|
||||||
if (!template) throw new HTTPError("template not found", 404);
|
|
||||||
|
|
||||||
const guild_id = Snowflake.generate();
|
const guild_id = Snowflake.generate();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user