From bb0dec2a9ff4467896356298dac6fa34fa314b9d Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 22 Jun 2021 18:41:21 +0200 Subject: :art: 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 --- src/routes/guilds/#guild_id/widget.json.ts | 68 +++++++++++++++--------------- src/routes/guilds/#guild_id/widget.png.ts | 5 ++- src/routes/guilds/#guild_id/widget.ts | 5 +-- src/schema/Widget.ts | 4 +- 4 files changed, 41 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/routes/guilds/#guild_id/widget.json.ts b/src/routes/guilds/#guild_id/widget.json.ts index c1565e6d..256a633d 100644 --- a/src/routes/guilds/#guild_id/widget.json.ts +++ b/src/routes/guilds/#guild_id/widget.json.ts @@ -75,48 +75,48 @@ 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") { - let item = {} + const status = doc.user?.presence?.status || "offline"; + if (status == "offline")return + + 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, - 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 + game: { name: activity.name } } + } - 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 + // Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to) + // Get this from VoiceState - // 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) - // Get this from VoiceState - - - // 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 - /* - const avatar = doc.user?.avatar; - if (avatar) { - const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003"; - const avatar_url = "/widget-avatars/" + ; - item = { - ...item, - avatar_url: avatar_url - } - } - */ - members.push(item); + // 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 + /* + const avatar = doc.user?.avatar; + if (avatar) { + const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003"; + const avatar_url = "/widget-avatars/" + ; + item = { + ...item, + avatar_url: avatar_url + } } + */ + + 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) diff --git a/src/routes/guilds/#guild_id/widget.png.ts b/src/routes/guilds/#guild_id/widget.png.ts index 986cb05a..3ddccf20 100644 --- a/src/routes/guilds/#guild_id/widget.png.ts +++ b/src/routes/guilds/#guild_id/widget.png.ts @@ -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); } diff --git a/src/routes/guilds/#guild_id/widget.ts b/src/routes/guilds/#guild_id/widget.ts index d8468da2..7682bc87 100644 --- a/src/routes/guilds/#guild_id/widget.ts +++ b/src/routes/guilds/#guild_id/widget.ts @@ -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); diff --git a/src/schema/Widget.ts b/src/schema/Widget.ts index 6a15a139..d37a38de 100644 --- a/src/schema/Widget.ts +++ b/src/schema/Widget.ts @@ -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 { -- cgit 1.5.1