Skip to content

Commit

Permalink
feat(userbg): init and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico040 committed Jun 8, 2024
1 parent 5c294c4 commit 7e89b76
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions plugins/userbg/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "UserBG",
"description": "https://github.com/Discord-Custom-Covers/usrbg#request-your-own-usrbg",
"authors": [
{
"name": "sapphire",
"id": "757982547861962752"
},
{
"name": "Rico040",
"id": "619474349845643275"
}
],
"main": "src/index.tsx",
"vendetta": {
"icon": "ic_profile_24px"
}
}
29 changes: 29 additions & 0 deletions plugins/userbg/src/Settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { React, url } from "@vendetta/metro/common"
import { getAssetIDByName } from "@vendetta/ui/assets"
import { Forms, General } from "@vendetta/ui/components"
import { showToast } from "@vendetta/ui/toasts"

import { fetchData } from "./index"

const { ScrollView } = General
const { FormSection, FormRow } = Forms

export default () => (<ScrollView>
<FormSection>
<FormRow
label="Discord Server"
leading={<FormRow.Icon source={getAssetIDByName("Discord")} />}
trailing={FormRow.Arrow}
onPress={() => url.openDeeplink("https://discord.gg/TeRQEPb")}
/>
<FormRow
label="Reload DB"
leading={<FormRow.Icon source={getAssetIDByName("ic_message_retry")} />}
onPress={async () => {
const fetch = await fetchData()
if (!fetch) return showToast("Failed to reload DB", getAssetIDByName("small"))
return showToast("Reloaded DB", getAssetIDByName("check"))
}}
/>
</FormSection>
</ScrollView>)
45 changes: 45 additions & 0 deletions plugins/userbg/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { logger } from "@vendetta"
import { findByProps } from "@vendetta/metro"
import { after } from "@vendetta/patcher"
import { safeFetch } from "@vendetta/utils"
import { showToast } from "@vendetta/ui/toasts"

import Settings from "./Settings"

interface userBGData {
endpoint: string;
bucket: string;
prefix: string;
users: Record<string, string>;
}

const getUserBannerURL = findByProps("default", "getUserBannerURL")

let data: userBGData
let unpatch: () => void

export const fetchData = async () => {
try {
data = await (await safeFetch("https://usrbg.is-hardly.online/users", { cache: "no-store" })).json()
return data
} catch (e) {
logger.error("Failed to fetch userBG data", e)
}
}

export const onLoad = async () => {
await fetchData()
if (!data) return showToast("Failed to load DB")
const { endpoint, bucket, prefix, users } = data;
unpatch = after("getUserBannerURL", getUserBannerURL, ([user]) => {
const customBanner = Object.entries(users).find(([userId, etag]) => userId === user?.id)
if (user?.banner === undefined && customBanner) {
const [userId, etag] = customBanner;
return `${endpoint}/${bucket}/${prefix}${userId}?${etag}`
}
})
}

export const onUnload = () => unpatch?.()

export const settings = Settings

0 comments on commit 7e89b76

Please sign in to comment.