🎨 clean up code
widget.json.ts: just return instead of long if statement + add ?. operators for error handling widget.png.ts make path universal widget.ts: remove unnecessary guild db query as getPermission already checks it Widget.ts make parameters required
This commit is contained in:
parent
4c0c09c8bc
commit
bb0dec2a9f
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@ -75,8 +75,9 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
.select("id user nick deaf mute")
|
||||
.cursor()
|
||||
.eachAsync(doc => {
|
||||
const status = doc.user?.presence.status || "offline";
|
||||
if (status != "offline") {
|
||||
const status = doc.user?.presence?.status || "offline";
|
||||
if (status == "offline")return
|
||||
|
||||
let item = {}
|
||||
|
||||
item = {
|
||||
@ -88,7 +89,7 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
status: status
|
||||
}
|
||||
|
||||
const activity = doc.user?.presence.activities[0];
|
||||
const activity = doc.user?.presence?.activities?.[0];
|
||||
if (activity) {
|
||||
item = {
|
||||
...item,
|
||||
@ -116,7 +117,6 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
*/
|
||||
|
||||
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)
|
||||
|
||||
@ -3,9 +3,12 @@ import { GuildModel } from "@fosscord/server-util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { Image } from "canvas";
|
||||
import fs from "fs";
|
||||
import path from "path"
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
// TODO: use svg templates instead of node-canvas for improved performance and to change it easily
|
||||
|
||||
// https://discord.com/developers/docs/resources/guild#get-guild-widget-image
|
||||
// TODO: Cache the response
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
@ -32,7 +35,7 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
const sizeOf = require("image-size");
|
||||
|
||||
// TODO: Widget style templates need Fosscord branding
|
||||
const source = __dirname + "../../../../../cache/widget/" + style + ".png";
|
||||
const source = path.join(__dirname, "..", "..", "..", "..", "cache","widget", `${style}.png`)
|
||||
if (!fs.existsSync(source)) {
|
||||
throw new HTTPError("Widget template does not exist.", 400);
|
||||
}
|
||||
|
||||
@ -27,10 +27,7 @@ router.patch("/", check(WidgetModifySchema), async (req: Request, res: Response)
|
||||
const perms = await getPermission(req.user_id, guild_id);
|
||||
perms.hasThrow("MANAGE_GUILD");
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
|
||||
await GuildModel.updateOne({ id: guild_id }, { widget_enabled: body['enabled'], widget_channel_id: body['channel_id'] }).exec();
|
||||
await GuildModel.updateOne({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_id }).exec();
|
||||
// Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request
|
||||
|
||||
return res.json(body);
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// https://discord.com/developers/docs/resources/guild#guild-widget-object
|
||||
export const WidgetModifySchema = {
|
||||
$enabled: Boolean, // whether the widget is enabled
|
||||
$channel_id: String // the widget channel id
|
||||
enabled: Boolean, // whether the widget is enabled
|
||||
channel_id: String // the widget channel id
|
||||
};
|
||||
|
||||
export interface WidgetModifySchema {
|
||||
|
||||
Reference in New Issue
Block a user