refactor: inject masto instance via nuxt app (#134)

This commit is contained in:
Daniel Roe 2022-11-26 15:42:58 +00:00 committed by GitHub
parent 5c60497421
commit 39b005899e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 67 additions and 48 deletions

View file

@ -19,7 +19,7 @@ export function fetchStatus(id: string): Promise<Status> {
const cached = cache.get(key)
if (cached)
return cached
const promise = masto.statuses.fetch(id)
const promise = useMasto().statuses.fetch(id)
.then((status) => {
cacheStatus(status)
return status
@ -33,7 +33,7 @@ export function fetchAccount(id: string): Promise<Account> {
const cached = cache.get(key)
if (cached)
return cached
const promise = masto.accounts.fetch(id)
const promise = useMasto().accounts.fetch(id)
.then((account) => {
cacheAccount(account, true)
return account
@ -47,7 +47,7 @@ export async function fetchAccountByName(acct: string): Promise<Account> {
const cached = cache.get(key)
if (cached)
return cached
const account = masto.accounts.lookup({ acct })
const account = useMasto().accounts.lookup({ acct })
.then((r) => {
cacheAccount(r, true)
return r

View file

@ -1,9 +1 @@
import { login } from 'masto'
import { currentUser } from './users'
import { DEFAULT_SERVER } from '~/constants'
// TODO: improve upsteam to make this synchronous (delayed auth)
export const masto = await login({
url: `https://${currentUser.value?.server || DEFAULT_SERVER}`,
accessToken: currentUser.value?.token || undefined,
})
export const useMasto = () => useNuxtApp().$masto

View file

@ -112,7 +112,7 @@ async function fetchRelationships() {
const requested = Array.from(requestedRelationships.entries())
requestedRelationships.clear()
const relationships = await masto.accounts.fetchRelationships(requested.map(([id]) => id))
const relationships = await useMasto().accounts.fetchRelationships(requested.map(([id]) => id))
for (let i = 0; i < requested.length; i++)
requested[i][1].value = relationships[i]
}

View file

@ -12,7 +12,7 @@ export const MentionSuggestion: Partial<SuggestionOptions> = {
if (query.length === 0)
return []
const mentionPaginator = masto.search({ q: query, type: 'accounts', limit: 25, resolve: true })
const mentionPaginator = useMasto().search({ q: query, type: 'accounts', limit: 25, resolve: true })
const results = await mentionPaginator.next()
return results.value.accounts

View file

@ -40,12 +40,12 @@ export async function loginTo(user: UserLogin & { account?: AccountCredentials }
url: `https://${user.server}`,
accessToken: user.token,
})
const me = await masto.accounts.verifyCredentials()
const me = await useMasto().accounts.verifyCredentials()
user.account = me
users.value.push(user)
currentUserId.value = me.id
servers.value[me.id] = await masto.instances.fetch()
servers.value[me.id] = await useMasto().instances.fetch()
await reloadPage()
return true
}