Skip to content

Commit

Permalink
fix: measurements sometimes missing for vanity metadata (#3)
Browse files Browse the repository at this point in the history
* fix: `measurements` sometimes missing for vanity metadata

* fix: type `memberCount` in vanity info interface

* chore: lint, format
  • Loading branch information
shayypy authored Dec 10, 2023
1 parent f91e613 commit 815befb
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ module.exports = {
project: "./tsconfig.json",
},
extends: "@sapphire",
ignorePatterns: ["**/node_modules/**", "**/dist/**", "**/types/**", "**/scripts/**", "*.d.ts"],
ignorePatterns: [
"**/node_modules/**",
"**/dist/**",
"**/types/**",
"**/scripts/**",
"*.d.ts",
],
rules: {
"@typescript-eslint/member-ordering": "off",
"@typescript-eslint/no-floating-promises": "off",
Expand Down
8 changes: 4 additions & 4 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"singleQuote": false,
"semi": true,
"trailingComma": "all",
"tabWidth": 4
"singleQuote": false,
"semi": true,
"trailingComma": "all",
"tabWidth": 4
}
2 changes: 1 addition & 1 deletion _config.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
theme: jekyll-theme-slate
theme: jekyll-theme-slate
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ services:
api:
command: ["npm", "run", "start:dev"]
volumes:
- "./src:/usr/app/src"
- "./src:/usr/app/src"
14 changes: 9 additions & 5 deletions src/controllers/shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export const getMemberCountFromGuilded = async (

let returnData;
if (type === "vanity")
returnData = (data.metadata as VanityInviteInfo)?.team?.measurements
?.numMembers;
returnData =
(data.metadata as VanityInviteInfo)?.team?.measurements
?.numMembers ??
(data.metadata as VanityInviteInfo)?.team?.memberCount;
else if (type === "i")
returnData = (data.metadata as IInviteInfo)?.inviteInfo?.team
?.memberCount;
Expand Down Expand Up @@ -67,9 +69,11 @@ export const generateSvg = async (
label: "Guilded",
message: `${msg} members`,
});
await redis.set(`badge:${type}:${inviteId}:${color}:${style}`, svg, {
EX: 900,
}).catch(() => void 0);
await redis
.set(`badge:${type}:${inviteId}:${color}:${style}`, svg, {
EX: 900,
})
.catch(() => void 0);
return svg;
} catch (e) {
return internalError(res, (e as Error).message);
Expand Down
14 changes: 9 additions & 5 deletions src/routes/shields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ export const getServerShield = async (
return badRequest(res, "Invalid style.");
}

const cachedBadge = await redis.get(
`badge:${type}:${inviteId}:${color}:${receivedStyle}`,
).catch(() => null);
const cachedBadge = await redis
.get(`badge:${type}:${inviteId}:${color}:${receivedStyle}`)
.catch(() => null);
if (cachedBadge) {
res.header("Content-Type", "image/svg+xml");
return res.status(200).send(cachedBadge);
}

let memberCount: string | null;
const cachedGuildedReq = await redis.get(`req:${type}:${inviteId}`).catch(() => null);
const cachedGuildedReq = await redis
.get(`req:${type}:${inviteId}`)
.catch(() => null);
if (cachedGuildedReq) memberCount = cachedGuildedReq;
else {
const memberCountReq = await getMemberCountFromGuilded(inviteId, type);
Expand All @@ -51,7 +53,9 @@ export const getServerShield = async (
);

memberCount = memberCountReq.toString();
await redis.set(`req:${type}:${inviteId}`, memberCount, { EX: 900 }).catch(() => void 0);
await redis
.set(`req:${type}:${inviteId}`, memberCount, { EX: 900 })
.catch(() => void 0);
}

const svg = await generateSvg(redis, res, {
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export interface VanityInviteInfo {
team: {
id: string;
name: string;
measurements: { numMembers: number };
measurements?: { numMembers: number };
memberCount: number;
};
}

Expand Down

0 comments on commit 815befb

Please sign in to comment.