From 2e7979817a539496925bb0b897fdee1165dfd133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sun, 15 Jan 2023 17:21:03 +0800 Subject: [PATCH] feat: support lookup account for gotosocial --- composables/cache.ts | 19 +++++++++++++++---- composables/users.ts | 13 ++++++++++++- constants/index.ts | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/composables/cache.ts b/composables/cache.ts index 6a9a7448..05aa7d4d 100644 --- a/composables/cache.ts +++ b/composables/cache.ts @@ -64,11 +64,22 @@ export async function fetchAccountByHandle(acct: string): Promise { - if (r.acct && !r.acct.includes('@') && domain) - r.acct = `${r.acct}@${domain}` + async function lookupAccount() { + const client = useMastoClient() + let account: mastodon.v1.Account + if (!isGotoSocial.value) + account = await client.v1.accounts.lookup({ acct }) + else + account = (await client.v1.search({ q: `@${acct}`, type: 'accounts' })).accounts[0] + + if (account.acct && !account.acct.includes('@') && domain) + account.acct = `${account.acct}@${domain}` + return account + } + + const account = lookupAccount() + .then((r) => { cacheAccount(r, server, true) return r }) diff --git a/composables/users.ts b/composables/users.ts index a21c6a64..2a9d8a60 100644 --- a/composables/users.ts +++ b/composables/users.ts @@ -8,6 +8,7 @@ import { DEFAULT_POST_CHARS_LIMIT, STORAGE_KEY_CURRENT_USER, STORAGE_KEY_CURRENT_USER_HANDLE, + STORAGE_KEY_NODES, STORAGE_KEY_NOTIFICATION, STORAGE_KEY_NOTIFICATION_POLICY, STORAGE_KEY_SERVERS, @@ -43,6 +44,7 @@ const initializeUsers = async (): Promise | RemovableRef>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true }) +export const nodes = useLocalStorage>(STORAGE_KEY_NODES, {}, { deep: true }) const currentUserId = useLocalStorage(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '') export type ElkInstance = Partial & { uri: string } @@ -60,11 +62,14 @@ export const currentUser = computed(() => { const publicInstance = ref(null) export const currentInstance = computed(() => currentUser.value ? instances.value[currentUser.value.server] ?? null : publicInstance.value) -export const isGlitchEdition = computed(() => currentInstance.value?.version?.includes('+glitch')) export const publicServer = ref('') export const currentServer = computed(() => currentUser.value?.server || publicServer.value) +export const currentNodeInfo = computed>(() => nodes.value[currentServer.value] || null) +export const isGotoSocial = computed(() => currentNodeInfo.value?.software?.name === 'gotosocial') +export const isGlitchEdition = computed(() => currentInstance.value?.version?.includes('+glitch')) + // when multiple tabs: we need to reload window when sign in, switch account or sign out if (process.client) { const windowReload = () => { @@ -112,6 +117,12 @@ export async function loginTo(masto: ElkMasto, user: Overwrite r.json()).then((info) => { + nodes.value[user.server] = info + }) + if (!user?.token) { publicServer.value = user.server publicInstance.value = instance diff --git a/constants/index.ts b/constants/index.ts index a431c01f..f3552c65 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -7,6 +7,7 @@ export const DEFAULT_LANGUAGE = 'en-US' export const STORAGE_KEY_DRAFTS = 'elk-drafts' export const STORAGE_KEY_USERS = 'elk-users' export const STORAGE_KEY_SERVERS = 'elk-servers' +export const STORAGE_KEY_NODES = 'elk-nodes' export const STORAGE_KEY_CURRENT_USER = 'elk-current-user' export const STORAGE_KEY_CURRENT_USER_HANDLE = 'elk-current-user-handle' export const STORAGE_KEY_NOTIFY_TAB = 'elk-notify-tab'