diff --git a/src/equicordplugins/timezones/database.tsx b/src/equicordplugins/timezones/database.tsx index f382c917..a313828e 100644 --- a/src/equicordplugins/timezones/database.tsx +++ b/src/equicordplugins/timezones/database.tsx @@ -8,7 +8,6 @@ import { openModal } from "@utils/index"; import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common"; const databaseTimezones: Record = {}; - const DOMAIN = "https://timezone.creations.works"; const REDIRECT_URI = `${DOMAIN}/auth/discord/callback`; const CLIENT_ID = "1377021506810417173"; @@ -26,7 +25,6 @@ export async function loadDatabaseTimezones(): Promise { const res = await fetch(`${DOMAIN}/list`, { headers: { Accept: "application/json" } }); - if (res.ok) { const json = await res.json(); for (const id in json) { @@ -34,10 +32,8 @@ export async function loadDatabaseTimezones(): Promise { value: json[id]?.timezone ?? null }; } - return true; } - return false; } catch (e) { console.error("Failed to fetch timezones list:", e); @@ -45,30 +41,93 @@ export async function loadDatabaseTimezones(): Promise { } } -export async function setTimezone(timezone: string): Promise { - const res = await fetch(`${DOMAIN}/set?timezone=${encodeURIComponent(timezone)}`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json" - }, - credentials: "include" - }); +async function checkAuthentication(): Promise { + try { + const res = await fetch(`${DOMAIN}/me`, { + credentials: "include", + headers: { Accept: "application/json" } + }); + return res.ok; + } catch (e) { + console.error("Failed to check authentication:", e); + return false; + } +} - return res.ok; +export async function setTimezone(timezone: string): Promise { + const isAuthenticated = await checkAuthentication(); + + if (!isAuthenticated) { + return new Promise(resolve => { + authModal(() => { + setTimezoneInternal(timezone).then(resolve); + }); + }); + } + + return setTimezoneInternal(timezone); +} + +async function setTimezoneInternal(timezone: string): Promise { + const formData = new URLSearchParams(); + formData.append("timezone", timezone); + + try { + const res = await fetch(`${DOMAIN}/set`, { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded", + Accept: "application/json" + }, + credentials: "include", + body: formData + }); + + if (!res.ok) { + const error = await res.json().catch(() => ({ message: "Unknown error" })); + showToast(error.message || "Failed to set timezone", Toasts.Type.FAILURE); + return false; + } + + showToast("Timezone updated successfully!", Toasts.Type.SUCCESS); + return true; + } catch (e) { + console.error("Error setting timezone:", e); + showToast("Failed to set timezone", Toasts.Type.FAILURE); + return false; + } } export async function deleteTimezone(): Promise { - const res = await fetch(`${DOMAIN}/delete`, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json" - }, - credentials: "include" - }); + const isAuthenticated = await checkAuthentication(); - return res.ok; + if (!isAuthenticated) { + showToast("You must be logged in to delete your timezone", Toasts.Type.FAILURE); + return false; + } + + try { + const res = await fetch(`${DOMAIN}/delete`, { + method: "DELETE", + headers: { + Accept: "application/json" + }, + credentials: "include" + }); + + if (!res.ok) { + const error = await res.json().catch(() => ({ message: "Unknown error" })); + showToast(error.message || "Failed to delete timezone", Toasts.Type.FAILURE); + return false; + } + + showToast("Timezone deleted successfully!", Toasts.Type.SUCCESS); + return true; + } catch (e) { + console.error("Error deleting timezone:", e); + showToast("Failed to delete timezone", Toasts.Type.FAILURE); + return false; + } } export function authModal(callback?: () => void) { @@ -83,21 +142,17 @@ export function authModal(callback?: () => void) { cancelCompletesFlow={false} callback={async (res: any) => { if (!res || !res.location) return; - try { const url = new URL(res.location); - const r = await fetch(url, { credentials: "include", headers: { Accept: "application/json" } }); - const json = await r.json(); if (!r.ok) { showToast(json.message ?? "Authorization failed", Toasts.Type.FAILURE); return; } - showToast("Authorization successful!", Toasts.Type.SUCCESS); callback?.(); } catch (e) { diff --git a/src/equicordplugins/timezones/index.tsx b/src/equicordplugins/timezones/index.tsx index c16b432d..2871c342 100644 --- a/src/equicordplugins/timezones/index.tsx +++ b/src/equicordplugins/timezones/index.tsx @@ -17,7 +17,7 @@ import { findByPropsLazy } from "@webpack"; import { Button, Menu, showToast, Toasts, Tooltip, useEffect, UserStore, useState } from "@webpack/common"; import { Message, User } from "discord-types/general"; -import { authModal, deleteTimezone, getTimezone, loadDatabaseTimezones, setUserDatabaseTimezone } from "./database"; +import { deleteTimezone, getTimezone, loadDatabaseTimezones, setUserDatabaseTimezone } from "./database"; import { SetTimezoneModal } from "./TimezoneModal"; export let timezones: Record = {}; @@ -68,9 +68,7 @@ export const settings = definePluginSettings({ type: OptionType.COMPONENT, component: () => ( @@ -83,11 +81,19 @@ export const settings = definePluginSettings({ component: () => (