api url setting verification

This commit is contained in:
rushiiMachine 2024-06-25 13:29:17 -07:00
parent 499046e5f0
commit 2672b1d523
No known key found for this signature in database
GPG key ID: DCBE5952BB3B6420
3 changed files with 38 additions and 10 deletions

View file

@ -9,11 +9,26 @@ import { Logger } from "@utils/Logger";
import settings from "./settings";
export const DEFAULT_API = "https://timezonedb.catvibers.me/api";
export type Snowflake = string;
type ApiError = { error: string; };
type UserFetchResponse = ApiError | { timezoneId: string }
type BulkFetchResponse = ApiError | Record<Snowflake, { timezoneId: string | null }>;
export async function verifyApi(url: string): Promise<boolean> {
if (url === DEFAULT_API) return true;
const res = await fetch(url, {
method: "GET",
headers: {
"User-Agent": VENCORD_USER_AGENT,
},
});
return "logged_in" in await res.json();
}
export async function fetchTimezonesBulk(ids: Snowflake[]): Promise<Record<Snowflake, string | null> | undefined> {
try {
const { apiUrl } = settings.store;
@ -21,7 +36,7 @@ export async function fetchTimezonesBulk(ids: Snowflake[]): Promise<Record<Snowf
method: "POST",
headers: {
"Content-Type": "application/json",
"X-User-Agent": VENCORD_USER_AGENT,
"User-Agent": VENCORD_USER_AGENT,
},
body: JSON.stringify(ids),
});
@ -47,8 +62,7 @@ export async function fetchTimezone(userId: Snowflake): Promise<string | null |
const req = await fetch(`${apiUrl}/user/${userId}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"X-User-Agent": VENCORD_USER_AGENT,
"User-Agent": VENCORD_USER_AGENT,
},
});

View file

@ -7,10 +7,13 @@
import { NavContextMenuPatchCallback } from "@api/ContextMenu";
import { CogWheel } from "@components/Icons";
import { Devs } from "@utils/constants";
import { Logger } from "@utils/Logger";
import definePlugin from "@utils/types";
import { Menu, UserStore } from "@webpack/common";
import { Message, User } from "discord-types/general";
import { Promisable } from "type-fest";
import { verifyApi } from "./api";
import { LocalTimestamp, openTimezoneOverrideModal } from "./components";
import settings, { SettingsComponent } from "./settings";
@ -67,6 +70,20 @@ export default definePlugin({
"user-profile-overflow-menu": contextMenuPatch,
},
beforeSave(options: Record<string, any>): Promisable<true | string> {
// Check that API url is valid
const { apiUrl } = options;
if (!apiUrl) return "Invalid API url!";
return verifyApi(apiUrl).then(success => {
if (success) return true;
return "Failed to verify API!";
}).catch(err => {
new Logger("Timezones").info("Failed to verify API url", err);
return "Failed to verify API!";
});
},
renderProfileTimezone: (props?: { user?: User; }) => {
if (!settings.store.displayInProfile || !props?.user?.id) return null;

View file

@ -19,31 +19,28 @@
import { DataStore } from "@api/index";
import { definePluginSettings } from "@api/Settings";
import { Link } from "@components/Link";
import { Margins } from "@utils/margins";
import { classes } from "@utils/misc";
import { IPluginOptionComponentProps, OptionType } from "@utils/types";
import { Text } from "@webpack/common";
import { Snowflake } from "./api";
import { DEFAULT_API, Snowflake } from "./api";
import { TimezoneCache } from "./cache";
import { classes } from "@utils/misc";
import { Margins } from "@utils/margins";
export type TimezoneOverrides = Record<Snowflake, string | null>;
const DEFAULT_API = "https://timezonedb.catvibers.me/api";
const settings = definePluginSettings({
enableApi: {
type: OptionType.BOOLEAN,
description: "Fetch user timezones from TimezoneDB when a local override does not exist",
default: true,
},
// TODO: disable this if enableApi is disabled
apiUrl: {
type: OptionType.STRING,
description: "The TimezoneDB API instance",
default: DEFAULT_API,
placeholder: DEFAULT_API,
onChange(_: any) {
onChange(_: string) {
DataStore.clear(TimezoneCache).catch(_ => _);
},
},