🎨 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,48 +75,48 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
.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") {
|
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,
|
||||||
id: null, // this is updated during the sort outside of the query
|
game: { name: activity.name }
|
||||||
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
|
|
||||||
// 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)
|
// 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 { HTTPError } from "lambert-server";
|
||||||
import { Image } from "canvas";
|
import { Image } from "canvas";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
const router: Router = Router();
|
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
|
// https://discord.com/developers/docs/resources/guild#get-guild-widget-image
|
||||||
// TODO: Cache the response
|
// TODO: Cache the response
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
@ -32,7 +35,7 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
const sizeOf = require("image-size");
|
const sizeOf = require("image-size");
|
||||||
|
|
||||||
// TODO: Widget style templates need Fosscord branding
|
// 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)) {
|
if (!fs.existsSync(source)) {
|
||||||
throw new HTTPError("Widget template does not exist.", 400);
|
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);
|
const perms = await getPermission(req.user_id, guild_id);
|
||||||
perms.hasThrow("MANAGE_GUILD");
|
perms.hasThrow("MANAGE_GUILD");
|
||||||
|
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
await GuildModel.updateOne({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_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();
|
|
||||||
// Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request
|
// Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request
|
||||||
|
|
||||||
return res.json(body);
|
return res.json(body);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
// https://discord.com/developers/docs/resources/guild#guild-widget-object
|
// https://discord.com/developers/docs/resources/guild#guild-widget-object
|
||||||
export const WidgetModifySchema = {
|
export const WidgetModifySchema = {
|
||||||
$enabled: Boolean, // whether the widget is enabled
|
enabled: Boolean, // whether the widget is enabled
|
||||||
$channel_id: String // the widget channel id
|
channel_id: String // the widget channel id
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface WidgetModifySchema {
|
export interface WidgetModifySchema {
|
||||||
|
|||||||
Reference in New Issue
Block a user