Skip to content

Commit

Permalink
fix(autos): Stop fetching ScratchDB for user embeds
Browse files Browse the repository at this point in the history
Signed-off-by: cobalt <61329810+RedGuy12@users.noreply.github.com>
  • Loading branch information
cobaltt7 committed Jul 17, 2024
1 parent bb1eae0 commit 266915d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 135 deletions.
1 change: 1 addition & 0 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const domains = {
scratch: "https://scratch.mit.edu",
scratchAddons: "https://scratchaddons.com",
scratchApi: "https://corsproxy.io/?https://api.scratch.mit.edu",
/** @deprecated */
scratchdb: "https://scratchdb.lefty.one/v3",
} as const;

Expand Down
78 changes: 18 additions & 60 deletions modules/autos/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { TimestampStyles, cleanCodeBlockContent, time, type APIEmbed } from "dis
import { parser, type Node } from "posthtml-parser";
import constants from "../../common/constants.js";
import { escapeMessage } from "../../util/markdown.js";
import { nth } from "../../util/numbers.js";
import { gracefulFetch } from "../../util/promises.js";
import { fetchUser } from "../../util/scratch.js";
import { truncateText } from "../../util/text.js";
Expand Down Expand Up @@ -133,69 +132,28 @@ export async function handleUser(urlParts: string[]): Promise<APIEmbed | undefin
if (!user) return;

const embed = {
title: `${user.username}${
(
"status" in user ? user.status == "Scratch Team" : user.scratchteam
) ?
"*"
: ""
}`,
title: `${user.username}${user.scratchteam ? "*" : ""}`,
color: constants.scratchColor,

fields:
"statistics" in user && user.statistics ?
[
{
name: `${constants.emojis.scratch.followers} Followers`,
value: `${user.statistics.followers.toLocaleString()} (ranked ${nth(
user.statistics.ranks.followers,
)})`,
inline: true,
},
{
name: `${constants.emojis.scratch.following} Following`,
value: user.statistics.following.toLocaleString(),
inline: true,
},
]
: [],
fields: [
// TODO: partition instead of just half and half

Check warning on line 139 in modules/autos/scratch.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected 'todo' comment: 'TODO: partition instead of just half and...'
user.profile.bio && {
name: "👋 About me",
value: truncateText(linkifyMentions(user.profile.bio), EMBED_LENGTH / 2, true),
inline: false,
},
user.profile.status && {
// eslint-disable-next-line unicorn/string-content
name: "🛠️ What I'm working on",
value: truncateText(linkifyMentions(user.profile.status), EMBED_LENGTH / 2, true),
inline: false,
},
].filter(Boolean),
thumbnail: { url: `https://uploads.scratch.mit.edu/get_image/user/${user.id}_90x90.png` },
author: {
name: `${"country" in user ? user.country : user.profile.country}${
"status" in user && user.status == "New Scratcher" ?
`${constants.footerSeperator}${user.status}`
: ""
}`,
},
author: { name: user.profile.country },
url: `${constants.domains.scratch}/users/${user.username}`,
timestamp: new Date("joined" in user ? user.joined : user.history.joined).toISOString(),
};

if ("profile" in user ? user.profile.status : user.work) {
embed.fields.unshift({
// eslint-disable-next-line unicorn/string-content
name: "🛠️ What I'm working on",
value: truncateText(
"profile" in user ?
linkifyMentions(user.profile.status)
: htmlToMarkdown(user.work),
EMBED_LENGTH / 2, // TODO: partition instead of just half and half
true,
),
inline: false,
});
}
if ("profile" in user ? user.profile.bio : user.bio) {
embed.fields.unshift({
name: "👋 About me",
value: truncateText(
"profile" in user ? linkifyMentions(user.profile.bio) : htmlToMarkdown(user.bio),
EMBED_LENGTH / 2,
true,
),
inline: false,
});
}
timestamp: new Date(user.history.joined).toISOString(),
} satisfies APIEmbed;

return embed;
}
Expand Down
6 changes: 1 addition & 5 deletions modules/roles/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ export default async function linkScratchRole(
body: JSON.stringify({
platform_name: "Scratch",
platform_username: username,
metadata: {
joined: ("joined" in scratch ? scratch.joined : scratch.history.joined).split(
"T",
)[0],
},
metadata: { joined: scratch.history.joined.split("T")[0] },
} satisfies RESTPutAPICurrentUserApplicationRoleConnectionJSONBody),
passThroughBody: true,
headers: {
Expand Down
88 changes: 18 additions & 70 deletions util/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,26 @@ import { gracefulFetch } from "./promises.js";
export async function fetchUser(username: string) {
const user = await gracefulFetch<
| {
username: string;
id: null;
sys_id: number;
joined: "1970-01-01T00:00:00.000Z";
country: null;
bio: null;
work: null;
status: null;
school: null;
}
| {
username: string;
id: number;
sys_id: number;
joined: string;
country: string;
bio: string;
work: string;
status: "New Scratcher" | "Scratch Team" | "Scratcher";
school: number | null;
statistics?: {
ranks: {
country: {
loves: number;
favorites: number;
comments: number;
views: number;
followers: number;
following: number;
};
loves: number;
favorites: number;
comments: number;
views: number;
followers: number;
following: number;
username: string;
scratchteam: boolean;
history: { joined: string; login?: string };
profile: {
id: number;
avatar?: string;
images: {
"90x90": string;
"60x60": string;
"55x55": string;
"50x50": string;
"32x32": string;
};
loves: number;
favorites: number;
comments: number;
views: number;
followers: number;
following: number;
status: string;
bio: string;
country: string;
};
}
| { error: "UserNotFoundError" | "UserNotValidError" }
>(`${constants.domains.scratchdb}/user/info/${username}/`);
const scratchUser =
!user || "error" in user || !user.id ?
await gracefulFetch<
| {
id: number;
username: string;
scratchteam: boolean;
history: { joined: string; login?: string };
profile: {
id: number;
avatar?: string;
images: {
"90x90": string;
"60x60": string;
"55x55": string;
"50x50": string;
"32x32": string;
};
status: string;
bio: string;
country: string;
};
}
| { code: string; message: string }
>(`${constants.domains.scratchApi}/users/${username}/`)
: user;
return !scratchUser || "code" in scratchUser || !scratchUser.id ? undefined : scratchUser;
| { code: string; message: string }
>(`${constants.domains.scratchApi}/users/${username}/`);
return !user || "code" in user ? undefined : user;
}

0 comments on commit 266915d

Please sign in to comment.