mirror of
https://github.com/Equicord/Equicord.git
synced 2025-06-14 09:03:03 -04:00
change database to use proper methods, fix auth on every single change (#281)
This commit is contained in:
parent
e2a686630c
commit
c90df61b88
2 changed files with 98 additions and 41 deletions
|
@ -8,7 +8,6 @@ import { openModal } from "@utils/index";
|
|||
import { OAuth2AuthorizeModal, showToast, Toasts } from "@webpack/common";
|
||||
|
||||
const databaseTimezones: Record<string, { value: string | null; }> = {};
|
||||
|
||||
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<boolean> {
|
|||
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<boolean> {
|
|||
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<boolean> {
|
|||
}
|
||||
}
|
||||
|
||||
export async function setTimezone(timezone: string): Promise<boolean> {
|
||||
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<boolean> {
|
||||
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<boolean> {
|
||||
const isAuthenticated = await checkAuthentication();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return new Promise(resolve => {
|
||||
authModal(() => {
|
||||
setTimezoneInternal(timezone).then(resolve);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return setTimezoneInternal(timezone);
|
||||
}
|
||||
|
||||
async function setTimezoneInternal(timezone: string): Promise<boolean> {
|
||||
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<boolean> {
|
||||
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) {
|
||||
|
|
|
@ -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<string, string | null> = {};
|
||||
|
@ -68,9 +68,7 @@ export const settings = definePluginSettings({
|
|||
type: OptionType.COMPONENT,
|
||||
component: () => (
|
||||
<Button onClick={() => {
|
||||
authModal(async () => {
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
});
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
}}>
|
||||
Set Timezone on Database
|
||||
</Button>
|
||||
|
@ -83,11 +81,19 @@ export const settings = definePluginSettings({
|
|||
component: () => (
|
||||
<Button
|
||||
color={Button.Colors.RED}
|
||||
onClick={() => {
|
||||
authModal(async () => {
|
||||
onClick={async () => {
|
||||
try {
|
||||
await setUserDatabaseTimezone(UserStore.getCurrentUser().id, null);
|
||||
await deleteTimezone();
|
||||
});
|
||||
const success = await deleteTimezone();
|
||||
if (success) {
|
||||
showToast("Database timezone reset successfully!", Toasts.Type.SUCCESS);
|
||||
} else {
|
||||
showToast("Failed to reset database timezone", Toasts.Type.FAILURE);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error resetting database timezone:", error);
|
||||
showToast("Failed to reset database timezone", Toasts.Type.FAILURE);
|
||||
}
|
||||
}}
|
||||
>
|
||||
Reset Database Timezones
|
||||
|
@ -228,9 +234,7 @@ export default definePlugin({
|
|||
|
||||
toolboxActions: {
|
||||
"Set Database Timezone": () => {
|
||||
authModal(async () => {
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
});
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
},
|
||||
"Refresh Database Timezones": async () => {
|
||||
try {
|
||||
|
@ -265,9 +269,7 @@ export default definePlugin({
|
|||
<Button
|
||||
color={Button.Colors.GREEN}
|
||||
onClick={() => {
|
||||
authModal(async () => {
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
});
|
||||
openModal(modalProps => <SetTimezoneModal userId={UserStore.getCurrentUser().id} modalProps={modalProps} database={true} />);
|
||||
}}
|
||||
>
|
||||
Want to save your timezone to the database? Click here to set it.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue