diff --git a/src/routes/guilds/#guild_id/widget.json.ts b/src/routes/guilds/#guild_id/widget.json.ts
index 256a633d..6f777ab4 100644
--- a/src/routes/guilds/#guild_id/widget.json.ts
+++ b/src/routes/guilds/#guild_id/widget.json.ts
@@ -18,7 +18,6 @@ router.get("/", async (req: Request, res: Response) => {
const { guild_id } = req.params;
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);
// Fetch existing widget invite for widget channel
@@ -43,68 +42,66 @@ router.get("/", async (req: Request, res: Response) => {
invite = await new InviteModel(body).save();
}
-
+
// Fetch voice channels, and the @everyone permissions object
let channels: any[] = [];
- await ChannelModel.find(
- { guild_id: guild_id, type: 2 },
- { permission_overwrites: { $elemMatch: { id: guild_id } } }
- ).lean()
- .select("id name position permission_overwrites")
- .sort({ position: 1 })
- .cursor()
- .eachAsync(doc => {
- // Only return channels where @everyone has the CONNECT permission
- if (doc.permission_overwrites === undefined || Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT) {
- channels.push(
- {
+ await ChannelModel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } })
+ .lean()
+ .select("id name position permission_overwrites")
+ .sort({ position: 1 })
+ .cursor()
+ .eachAsync((doc) => {
+ // Only return channels where @everyone has the CONNECT permission
+ if (
+ doc.permission_overwrites === undefined ||
+ Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === Permissions.FLAGS.CONNECT
+ ) {
+ channels.push({
id: doc.id,
name: doc.name,
position: doc.position
- }
- )
- }
- });
+ });
+ }
+ });
// Fetch members
// TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file)
let members: any[] = [];
await MemberModel.find({ guild_id: guild_id })
- .lean()
- .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } })
- .select("id user nick deaf mute")
- .cursor()
- .eachAsync(doc => {
- 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
- }
+ .lean()
+ .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } })
+ .select("id user nick deaf mute")
+ .cursor()
+ .eachAsync((doc) => {
+ const status = doc.user?.presence?.status || "offline";
+ if (status == "offline") return;
+
+ let item = {};
- const activity = doc.user?.presence?.activities?.[0];
- if (activity) {
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
- // 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
- /*
+ // 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";
@@ -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)
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,
members: members,
presence_count: guild.presence_count
- }
+ };
res.set("Cache-Control", "public, max-age=300");
return res.json(data);
|