feat: support lookup account for gotosocial

This commit is contained in:
三咲智子 Kevin Deng 2023-01-15 17:21:03 +08:00
parent 20a90cc949
commit 2e7979817a
No known key found for this signature in database
GPG key ID: 69992F2250DFD93E
3 changed files with 28 additions and 5 deletions

View file

@ -64,11 +64,22 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
if (cached)
return cached
const domain = currentInstance.value?.uri
const account = useMastoClient().v1.accounts.lookup({ acct })
.then((r) => {
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
})

View file

@ -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<Ref<UserLogin[]> | RemovableRef<UserLo
const users = await initializeUsers()
export const instances = useLocalStorage<Record<string, mastodon.v1.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
export const nodes = useLocalStorage<Record<string, any>>(STORAGE_KEY_NODES, {}, { deep: true })
const currentUserId = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '')
export type ElkInstance = Partial<mastodon.v1.Instance> & { uri: string }
@ -60,11 +62,14 @@ export const currentUser = computed<UserLogin | undefined>(() => {
const publicInstance = ref<ElkInstance | null>(null)
export const currentInstance = computed<null | ElkInstance>(() => 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<string>(() => currentUser.value?.server || publicServer.value)
export const currentNodeInfo = computed<null | Record<string, any>>(() => 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<UserLogin, { acco
const { client } = $(masto)
const instance = mastoLogin(masto, user)
// GoToSocial only API
const url = `https://${user.server}`
fetch(`${url}/nodeinfo/2.0`).then(r => r.json()).then((info) => {
nodes.value[user.server] = info
})
if (!user?.token) {
publicServer.value = user.server
publicInstance.value = instance

View file

@ -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'