From c5573cb10bec6f046c092d1e38a0d251f643fc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sun, 8 Jan 2023 17:16:15 +0800 Subject: [PATCH] refactor: switch to v1 instance api V2 instance api is too new, and we should support more versions. --- composables/users.ts | 18 +++++++++--------- types/index.ts | 6 ------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/composables/users.ts b/composables/users.ts index 98d08ded..3c8ab824 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -40,7 +40,7 @@ const initializeUsers = async (): Promise | RemovableRef>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) +const instances = useLocalStorage>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '') export const currentUser = computed(() => { @@ -53,8 +53,8 @@ export const currentUser = computed(() => { return users.value[0] }) -const publicInstance = ref(null) -export const currentInstance = computed(() => currentUser.value ? instances.value[currentUser.value.server] ?? null : publicInstance.value) +const publicInstance = ref(null) +export const currentInstance = computed(() => currentUser.value ? instances.value[currentUser.value.server] ?? null : publicInstance.value) export const publicServer = ref('') export const currentServer = computed(() => currentUser.value?.server || publicServer.value) @@ -91,7 +91,7 @@ if (process.client) { } export const currentUserHandle = computed(() => currentUser.value?.account.id - ? `${currentUser.value.account.acct}@${currentInstance.value?.domain || currentServer.value}` + ? `${currentUser.value.account.acct}@${currentInstance.value?.uri || currentServer.value}` : '[anonymous]', ) @@ -111,14 +111,14 @@ async function loginTo(user?: Omit & { account?: mastodon. if (!user?.token) { publicServer.value = server - publicInstance.value = await masto.v2.instance.fetch() + publicInstance.value = await masto.v1.instances.fetch() } else { try { const [me, instance, pushSubscription] = await Promise.all([ masto.v1.accounts.verifyCredentials(), - masto.v2.instance.fetch(), + masto.v1.instances.fetch(), // if PWA is not enabled, don't get push subscription useRuntimeConfig().public.pwaEnabled // we get 404 response instead empty data @@ -127,7 +127,7 @@ async function loginTo(user?: Omit & { account?: mastodon. ]) if (!me.acct.includes('@')) - me.acct = `${me.acct}@${instance.domain}` + me.acct = `${me.acct}@${instance.uri}` user.account = me user.pushSubscription = pushSubscription @@ -169,7 +169,7 @@ export function setAccountInfo(userId: string, account: mastodon.v1.AccountCrede export async function pullMyAccountInfo() { const account = await useMasto().v1.accounts.verifyCredentials() if (!account.acct.includes('@')) - account.acct = `${account.acct}@${currentInstance.value!.domain}` + account.acct = `${account.acct}@${currentInstance.value!.uri}` setAccountInfo(currentUserId.value, account) cacheAccount(account, currentServer.value, true) @@ -334,7 +334,7 @@ export function clearUserLocalStorage(account?: mastodon.v1.Account) { if (!account) return - const id = `${account.acct}@${currentInstance.value?.domain || currentServer.value}` + const id = `${account.acct}@${currentInstance.value?.uri || currentServer.value}` // @ts-expect-error bind value to the function ;(useUserLocalStorage._ as Map>>).forEach((storage) => { if (storage.value[id]) diff --git a/types/index.ts b/types/index.ts index 47c3bfac..2320951f 100644 --- a/types/index.ts +++ b/types/index.ts @@ -27,12 +27,6 @@ export interface ElkMasto extends mastodon.Client { export type PaginatorState = 'idle' | 'loading' | 'done' | 'error' -export interface ServerInfo extends mastodon.v2.Instance { - server: string - timeUpdated: number - customEmojis?: Record -} - export interface GroupedNotifications { id: string type: Exclude