Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: measurements sometimes missing for vanity metadata #3

Merged
merged 3 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading