From 6be080743310608002f74fae161e1e18ab770105 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Fri, 10 Jun 2022 14:32:21 +0100 Subject: [PATCH] feat: add disable / delete funct; bump revolt-api --- package.json | 2 +- src/context/intermediate/modals/Input.tsx | 2 +- src/context/revoltjs/RevoltClient.tsx | 10 ++++----- src/mobx/legacy/redux.ts | 2 +- src/mobx/stores/Auth.ts | 4 ++-- src/pages/settings/panes/Account.tsx | 26 +++++++++++++++++------ src/types/revolt-api.d.ts | 7 ++++++ yarn.lock | 20 ++++++++--------- 8 files changed, 47 insertions(+), 26 deletions(-) create mode 100644 src/types/revolt-api.d.ts diff --git a/package.json b/package.json index a4a89c0c..28279792 100644 --- a/package.json +++ b/package.json @@ -145,7 +145,7 @@ "react-router-dom": "^5.2.0", "react-scroll": "^1.8.2", "react-virtuoso": "^2.12.0", - "revolt.js": "6.0.2", + "revolt.js": "6.0.3", "rimraf": "^3.0.2", "sass": "^1.35.1", "shade-blend-color": "^1.0.0", diff --git a/src/context/intermediate/modals/Input.tsx b/src/context/intermediate/modals/Input.tsx index 1e70b2ac..e34cad30 100644 --- a/src/context/intermediate/modals/Input.tsx +++ b/src/context/intermediate/modals/Input.tsx @@ -177,7 +177,7 @@ export function SpecialInputModal(props: SpecialProps) { question={"Add Friend"} callback={(username) => client.api - .put(`/users/${username as ""}/friend`) + .post(`/users/friend`, { username }) .then(undefined) } /> diff --git a/src/context/revoltjs/RevoltClient.tsx b/src/context/revoltjs/RevoltClient.tsx index 57b0a091..e166c309 100644 --- a/src/context/revoltjs/RevoltClient.tsx +++ b/src/context/revoltjs/RevoltClient.tsx @@ -3,7 +3,7 @@ import { observer } from "mobx-react-lite"; import { Client } from "revolt.js"; import { createContext } from "preact"; -import { useContext, useEffect, useState } from "preact/hooks"; +import { useCallback, useContext, useEffect, useState } from "preact/hooks"; import { Preloader } from "@revoltchat/ui"; @@ -29,7 +29,7 @@ export interface ClientOperations { export const AppContext = createContext(null!); export const StatusContext = createContext(null!); -export const LogOutContext = createContext(() => {}); +export const LogOutContext = createContext((avoidReq?: boolean) => {}); type Props = { children: Children; @@ -42,10 +42,10 @@ export default observer(({ children }: Props) => { const [status, setStatus] = useState(ClientStatus.LOADING); const [loaded, setLoaded] = useState(false); - function logout() { + const logout = useCallback((avoidReq?: boolean) => { setLoaded(false); - client.logout(false); - } + client.logout(avoidReq); + }, []); useEffect(() => { if (navigator.onLine) { diff --git a/src/mobx/legacy/redux.ts b/src/mobx/legacy/redux.ts index 589fa9c4..81c5c37f 100644 --- a/src/mobx/legacy/redux.ts +++ b/src/mobx/legacy/redux.ts @@ -58,7 +58,7 @@ export interface LegacySyncOptions { export interface LegacyAuthState { accounts: { [key: string]: { - session: API.Session; + session: Session; }; }; active?: string; diff --git a/src/mobx/stores/Auth.ts b/src/mobx/stores/Auth.ts index bf1d0aec..8662bb1f 100644 --- a/src/mobx/stores/Auth.ts +++ b/src/mobx/stores/Auth.ts @@ -8,7 +8,7 @@ import Persistent from "../interfaces/Persistent"; import Store from "../interfaces/Store"; interface Account { - session: API.Session; + session: Session; } export interface Data { @@ -82,7 +82,7 @@ export default class Auth implements Store, Persistent { * Add a new session to the auth manager. * @param session Session */ - @action setSession(session: API.Session) { + @action setSession(session: Session) { this.sessions.set(session.user_id, { session }); this.current = session.user_id; } diff --git a/src/pages/settings/panes/Account.tsx b/src/pages/settings/panes/Account.tsx index 4874421f..eb67ea01 100644 --- a/src/pages/settings/panes/Account.tsx +++ b/src/pages/settings/panes/Account.tsx @@ -21,6 +21,7 @@ import { stopPropagation } from "../../../lib/stopPropagation"; import { useIntermediate } from "../../../context/intermediate/Intermediate"; import { ClientStatus, + LogOutContext, StatusContext, useClient, } from "../../../context/revoltjs/RevoltClient"; @@ -30,6 +31,7 @@ import UserIcon from "../../../components/common/user/UserIcon"; export const Account = observer(() => { const { openScreen, writeClipboard } = useIntermediate(); + const logOut = useContext(LogOutContext); const status = useContext(StatusContext); const client = useClient(); @@ -207,9 +209,15 @@ export const Account = observer(() => { "Disable your account. You won't be able to access it unless you contact support." } action="chevron" - onClick={() => { - // - }}> + onClick={() => + client.api + .post("/auth/account/disable", undefined, { + headers: { + "X-MFA-Ticket": "TICKET", + }, + }) + .then(() => logOut(true)) + }> { "Your account will be queued for deletion, a confirmation email will be sent." } action="chevron" - onClick={() => { - // - }}> + onClick={() => + client.api + .post("/auth/account/delete", undefined, { + headers: { + "X-MFA-Ticket": "TICKET", + }, + }) + .then(() => logOut(true)) + }> diff --git a/src/types/revolt-api.d.ts b/src/types/revolt-api.d.ts new file mode 100644 index 00000000..664e6ab1 --- /dev/null +++ b/src/types/revolt-api.d.ts @@ -0,0 +1,7 @@ +// TODO: re-export from revolt-api in some way +declare type Session = { + _id: string; + token: string; + name: string; + user_id: string; +}; diff --git a/yarn.lock b/yarn.lock index 88208886..0c7de6ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3578,7 +3578,7 @@ __metadata: react-router-dom: ^5.2.0 react-scroll: ^1.8.2 react-virtuoso: ^2.12.0 - revolt.js: 6.0.2 + revolt.js: 6.0.3 rimraf: ^3.0.2 sass: ^1.35.1 shade-blend-color: ^1.0.0 @@ -6858,20 +6858,20 @@ __metadata: languageName: node linkType: hard -"revolt-api@npm:0.5.3-5-patch.4": - version: 0.5.3-5-patch.4 - resolution: "revolt-api@npm:0.5.3-5-patch.4" +"revolt-api@npm:0.5.3-7": + version: 0.5.3-7 + resolution: "revolt-api@npm:0.5.3-7" dependencies: "@insertish/oapi": 0.1.16 axios: ^0.26.1 lodash.defaultsdeep: ^4.6.1 - checksum: 4f01c43bff96c4030d13ab0bb5dc83614445763602cfdd8b3ff1dbf61620446a22513ca259bbfc9c490f6b9b19c79d610921a252b667d25adf4040b4222d98cf + checksum: acc2f412d1be90f0cfa8a24ba715d8257813762c56ba0e48c649efff349674517036a8fa5939eda61ce31fd64a37f0c54af3d8fae56edf4596f05b10304e090e languageName: node linkType: hard -"revolt.js@npm:6.0.2": - version: 6.0.2 - resolution: "revolt.js@npm:6.0.2" +"revolt.js@npm:6.0.3": + version: 6.0.3 + resolution: "revolt.js@npm:6.0.3" dependencies: "@insertish/exponential-backoff": 3.1.0-patch.2 "@insertish/isomorphic-ws": ^4.0.1 @@ -6882,10 +6882,10 @@ __metadata: lodash.isequal: ^4.5.0 long: ^5.2.0 mobx: ^6.3.2 - revolt-api: 0.5.3-5-patch.4 + revolt-api: 0.5.3-7 ulid: ^2.3.0 ws: ^8.2.2 - checksum: 1b0c6ce0ceae5d20aec373f24432a5e804677c455fbc39edadaeb6ae0427a0b4fe2ea2b950df92262cd56a43e3f8759d80887b65d66111882b6ecb82e813ff38 + checksum: cfecbde7a9b795da75bfac3cec05f2aed5fd02cd14fcac1b2fad26285de1b887cf7ccb339e8a63a019f1e83058b041305927e029d7b5eedcc00823582d9a842a languageName: node linkType: hard