🎨 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:
Flam3rboy 2021-06-22 18:41:21 +02:00
parent 4c0c09c8bc
commit bb0dec2a9f
5 changed files with 44 additions and 44 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@ -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: 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: 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);
});
// Sort members, and update ids (Unable to do under the mongoose query due to https://mongoosejs.com/docs/faq.html#populate_sort_order)

View File

@ -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);
}

View File

@ -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);

View File

@ -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 {