generated from replugged-org/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c2a4ef
commit 445b12e
Showing
11 changed files
with
247 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,45 @@ | ||
import { API_URL } from "./constants"; | ||
import { useAuthorizationStore } from "./stores/AuthorizationStore"; | ||
|
||
export interface Decoration { | ||
hash: string; | ||
animated: boolean; | ||
alt: string | null; | ||
authorId: string | null; | ||
reviewed: boolean | null; | ||
presetId: string | null; | ||
} | ||
|
||
export interface NewDecoration { | ||
uri: string; | ||
fileName: string; | ||
fileType: string; | ||
alt: string | null; | ||
} | ||
|
||
export async function fetchApi(url: RequestInfo, options?: RequestInit) { | ||
const res = await fetch(url, { | ||
...options, | ||
headers: { | ||
...options?.headers, | ||
Authorization: `Bearer ${useAuthorizationStore.getState().token}`, | ||
}, | ||
}); | ||
|
||
if (res.ok) return res; | ||
else throw new Error(await res.text()); | ||
} | ||
|
||
export const getUsersDecorations = async (ids: string[] | undefined = undefined) => { | ||
if (ids && ids.length === 0) return {} | ||
if (ids && ids.length === 0) return {}; | ||
const url = new URL(API_URL + "/users"); | ||
if (ids && ids.length !== 0) url.searchParams.set("ids", JSON.stringify(ids)); | ||
|
||
return (await fetch(url).then(c => c.json())) as Record<string, string | null>; | ||
return (await fetch(url).then((c) => c.json())) as Record<string, string | null>; | ||
}; | ||
|
||
export const getUserDecorations = async (id: string = "@me"): Promise<Decoration[]> => | ||
fetchApi(API_URL + `/users/${id}/decorations`).then((c) => c.json()); | ||
|
||
export const getUserDecoration = async (id: string = "@me"): Promise<Decoration | null> => | ||
fetchApi(API_URL + `/users/${id}/decoration`).then((c) => c.json()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { StateStorage } from "zustand/middleware"; | ||
import { authorizationToken } from "../utils/settings"; | ||
import { persist, create } from "../zustand"; | ||
import { common } from "replugged"; | ||
import showAuthorizationModal from "../utils/showAuthorizationModal"; | ||
|
||
const { users } = common; | ||
|
||
interface AuthorizationState { | ||
token: string | null; | ||
tokens: Record<string, string>; | ||
init: () => void; | ||
authorize: () => Promise<void>; | ||
setToken: (token: string) => void; | ||
remove: (id: string) => void; | ||
isAuthorized: () => boolean; | ||
} | ||
|
||
const indexedDBStorage: StateStorage = { | ||
async getItem(name: string): Promise<string | null> { | ||
return (await authorizationToken).get(name).then((v) => v ?? null); | ||
}, | ||
async setItem(name: string, value: string): Promise<void> { | ||
await (await authorizationToken).set(name, value); | ||
}, | ||
async removeItem(name: string): Promise<void> { | ||
await authorizationToken.del(name); | ||
}, | ||
}; | ||
|
||
export const useAuthorizationStore = create<AuthorizationState>( | ||
persist( | ||
(set, get) => ({ | ||
token: null, | ||
tokens: {}, | ||
init: () => { | ||
set({ token: get().tokens[users.getCurrentUser().id] ?? null }); | ||
}, | ||
setToken: (token: string) => | ||
set({ token, tokens: { ...get().tokens, [users.getCurrentUser().id]: token } }), | ||
remove: (id: string) => { | ||
const { tokens, init } = get(); | ||
const newTokens = { ...tokens }; | ||
delete newTokens[id]; | ||
set({ tokens: newTokens }); | ||
|
||
init(); | ||
}, | ||
authorize: () => void showAuthorizationModal(), | ||
isAuthorized: () => !!get().token, | ||
}), | ||
{ | ||
name: "decor-auth", | ||
getStorage: () => indexedDBStorage, | ||
partialize: (state) => ({ tokens: state.tokens }), | ||
onRehydrateStorage: () => (state) => state?.init(), | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { common, lodash } from "replugged"; | ||
import { Decoration, NewDecoration, getUserDecoration, getUserDecorations } from "../api"; | ||
import discordifyDecoration from "../utils/discordifyDecoration"; | ||
import { useUsersDecorationsStore } from "./UserDecorationsStore"; | ||
import decorationToString from "../utils/decorationToString"; | ||
import { create } from "../zustand"; | ||
|
||
const { lodash, users, fluxDispatcher } = common; | ||
|
||
interface CurrentUserDecorationsState { | ||
decorations: Decoration[]; | ||
selectedDecoration: Decoration | null; | ||
fetched: boolean; | ||
fetch: () => Promise<void>; | ||
delete: (decoration: Decoration | string) => Promise<void>; | ||
create: (decoration: NewDecoration) => Promise<void>; | ||
select: (decoration: Decoration | null) => Promise<void>; | ||
clear: () => void; | ||
} | ||
|
||
function updateCurrentUserAvatarDecoration(decoration: Decoration | null) { | ||
const user = users.getCurrentUser(); | ||
user.avatarDecoration = decoration ? discordifyDecoration(decoration) : null; | ||
user.avatarDecorationData = user.avatarDecoration; | ||
|
||
useUsersDecorationsStore | ||
.getState() | ||
.set(user.id, decoration ? decorationToString(decoration) : null); | ||
fluxDispatcher.dispatch({ type: "CURRENT_USER_UPDATE", user }); | ||
fluxDispatcher.dispatch({ type: "USER_SETTINGS_ACCOUNT_SUBMIT_SUCCESS" }); | ||
} | ||
|
||
export const useCurrentUserDecorationsStore = create<CurrentUserDecorationsState>((set, get) => ({ | ||
decorations: [], | ||
selectedDecoration: null, | ||
async fetch() { | ||
const decorations = await getUserDecorations(); | ||
const selectedDecoration = await getUserDecoration(); | ||
|
||
set({ decorations, selectedDecoration }); | ||
}, | ||
clear: () => set({ decorations: [], selectedDecoration: null }) | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Decoration } from "../api"; | ||
|
||
export default (decoration: Decoration) => `${decoration.animated ? 'a_' : ''}${decoration.hash}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Decoration } from "../api"; | ||
import { SKU_ID } from "../constants"; | ||
import decorationToString from "./decorationToString"; | ||
|
||
export default (d: Decoration) => ({ asset: decorationToString(d), skuId: SKU_ID }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { settings } from "replugged" | ||
|
||
export const defaultSettings = {} | ||
export const authorizationToken = settings.init("decor.auth", defaultSettings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { webpack, common } from "replugged" | ||
import { useAuthorizationStore } from "../stores/AuthorizationStore"; | ||
import { AUTHORIZE_URL, CLIENT_ID } from "../constants"; | ||
import { logger } from "../.."; | ||
|
||
const { modal: { openModal } } = common | ||
const OAuth = webpack.getByProps("OAuth2AuthorizeModal") | ||
|
||
export default async () => new Promise(r => openModal(props => | ||
<OAuth.OAuth2AuthorizeModal | ||
{...props} | ||
scopes={["identify"]} | ||
responseType="code" | ||
redirectUri={AUTHORIZE_URL} | ||
permissions={0} | ||
clientId={CLIENT_ID} | ||
cancelCompletesFlow={false} | ||
callback={async (response: any) => { | ||
try { | ||
const url = new URL(response.location); | ||
url.searchParams.append("client", "vencord"); | ||
|
||
const req = await fetch(url); | ||
|
||
if (req?.ok) { | ||
const token = await req.text(); | ||
useAuthorizationStore.getState().setToken(token); | ||
} else { | ||
throw new Error("Request not OK"); | ||
} | ||
r(void 0); | ||
} catch (e) { | ||
logger("Decor").error("Failed to authorize", e); | ||
} | ||
}} | ||
/> | ||
)) | ||
|
||
|
Oops, something went wrong.