refactor: switch to v1 instance api

V2 instance api is too new, and we should
support more versions.
This commit is contained in:
三咲智子 2023-01-08 17:16:15 +08:00
parent d415638bd9
commit c5573cb10b
No known key found for this signature in database
GPG key ID: 69992F2250DFD93E
2 changed files with 9 additions and 15 deletions

View file

@ -40,7 +40,7 @@ const initializeUsers = async (): Promise<Ref<UserLogin[]> | RemovableRef<UserLo
}
const users = await initializeUsers()
const instances = useLocalStorage<Record<string, mastodon.v2.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
const instances = useLocalStorage<Record<string, mastodon.v1.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
const currentUserId = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER, mock ? mock.user.account.id : '')
export const currentUser = computed<UserLogin | undefined>(() => {
@ -53,8 +53,8 @@ export const currentUser = computed<UserLogin | undefined>(() => {
return users.value[0]
})
const publicInstance = ref<mastodon.v2.Instance | null>(null)
export const currentInstance = computed<null | mastodon.v2.Instance>(() => currentUser.value ? instances.value[currentUser.value.server] ?? null : publicInstance.value)
const publicInstance = ref<mastodon.v1.Instance | null>(null)
export const currentInstance = computed<null | mastodon.v1.Instance>(() => currentUser.value ? instances.value[currentUser.value.server] ?? null : publicInstance.value)
export const publicServer = ref('')
export const currentServer = computed<string>(() => 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<UserLogin, 'account'> & { 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<UserLogin, 'account'> & { 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<string, Ref<Record<string, any>>>).forEach((storage) => {
if (storage.value[id])

View file

@ -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<string, mastodon.v1.CustomEmoji>
}
export interface GroupedNotifications {
id: string
type: Exclude<string, 'grouped-reblogs-and-favourites'>