fix: replace deprecated v1.instance (#3183)
This commit is contained in:
parent
4cbafc5f56
commit
9f4945bad8
2 changed files with 41 additions and 10 deletions
|
@ -3,7 +3,7 @@ import type { mastodon } from 'masto'
|
||||||
import type { Ref } from 'vue'
|
import type { Ref } from 'vue'
|
||||||
import type { ElkInstance } from '../users'
|
import type { ElkInstance } from '../users'
|
||||||
import type { UserLogin } from '~/types'
|
import type { UserLogin } from '~/types'
|
||||||
import { createRestAPIClient, createStreamingAPIClient } from 'masto'
|
import { createRestAPIClient, createStreamingAPIClient, MastoHttpError } from 'masto'
|
||||||
|
|
||||||
export function createMasto() {
|
export function createMasto() {
|
||||||
return {
|
return {
|
||||||
|
@ -30,15 +30,47 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
|
||||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const streamingApiUrl = instance?.urls?.streamingApi
|
const streamingApiUrl = instance?.configuration?.urls?.streaming
|
||||||
masto.client.value = createRestAPIClient({ url, accessToken })
|
masto.client.value = createRestAPIClient({ url, accessToken })
|
||||||
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
||||||
|
|
||||||
// Refetch instance info in the background on login
|
// Refetch instance info in the background on login
|
||||||
masto.client.value.v1.instance.fetch().then((newInstance) => {
|
masto.client.value.v2.instance.fetch().catch(error => new Promise<mastodon.v2.Instance>((resolve, reject) => {
|
||||||
|
if (error instanceof MastoHttpError && error.statusCode === 404) {
|
||||||
|
return masto.client.value.v1.instance.fetch().then((newInstance) => {
|
||||||
|
console.warn(`Instance ${server} on version ${newInstance.version} does not support "GET /api/v2/instance" API, try converting to v2 instance... expect some errors`)
|
||||||
|
const v2Instance = {
|
||||||
|
...newInstance,
|
||||||
|
domain: newInstance.uri,
|
||||||
|
sourceUrl: '',
|
||||||
|
usage: {
|
||||||
|
users: {
|
||||||
|
activeMonth: 0,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
icon: [],
|
||||||
|
apiVersions: {
|
||||||
|
mastodon: newInstance.version,
|
||||||
|
},
|
||||||
|
contact: {
|
||||||
|
email: newInstance.email,
|
||||||
|
},
|
||||||
|
configuration: {
|
||||||
|
...(newInstance.configuration ?? {}),
|
||||||
|
urls: {
|
||||||
|
streaming: newInstance.urls.streamingApi,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as unknown as mastodon.v2.Instance
|
||||||
|
return resolve(v2Instance)
|
||||||
|
}).catch(reject)
|
||||||
|
}
|
||||||
|
|
||||||
|
return reject(error)
|
||||||
|
})).then((newInstance) => {
|
||||||
Object.assign(instance, newInstance)
|
Object.assign(instance, newInstance)
|
||||||
if (newInstance.urls.streamingApi !== streamingApiUrl)
|
if (newInstance.configuration.urls.streaming !== streamingApiUrl)
|
||||||
masto.streamingClient.value = createStreamingClient(newInstance.urls.streamingApi)
|
masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming)
|
||||||
|
|
||||||
instanceStorage.value[server] = newInstance
|
instanceStorage.value[server] = newInstance
|
||||||
})
|
})
|
||||||
|
|
|
@ -20,14 +20,13 @@ const mock = process.mock
|
||||||
const users: Ref<UserLogin[]> | RemovableRef<UserLogin[]> = import.meta.server ? ref<UserLogin[]>([]) : ref<UserLogin[]>([]) as RemovableRef<UserLogin[]>
|
const users: Ref<UserLogin[]> | RemovableRef<UserLogin[]> = import.meta.server ? ref<UserLogin[]>([]) : ref<UserLogin[]>([]) as RemovableRef<UserLogin[]>
|
||||||
const nodes = useLocalStorage<Record<string, any>>(STORAGE_KEY_NODES, {}, { deep: true })
|
const nodes = useLocalStorage<Record<string, any>>(STORAGE_KEY_NODES, {}, { deep: true })
|
||||||
export const currentUserHandle = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '')
|
export const currentUserHandle = useLocalStorage<string>(STORAGE_KEY_CURRENT_USER_HANDLE, mock ? mock.user.account.id : '')
|
||||||
export const instanceStorage = useLocalStorage<Record<string, mastodon.v1.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
|
export const instanceStorage = useLocalStorage<Record<string, mastodon.v2.Instance>>(STORAGE_KEY_SERVERS, mock ? mock.server : {}, { deep: true })
|
||||||
|
|
||||||
export type ElkInstance = Partial<mastodon.v1.Instance> & {
|
export type ElkInstance = Partial<mastodon.v2.Instance> & {
|
||||||
uri: string
|
|
||||||
/** support GoToSocial */
|
/** support GoToSocial */
|
||||||
accountDomain?: string | null
|
accountDomain?: string | null
|
||||||
}
|
}
|
||||||
export function getInstanceCache(server: string): mastodon.v1.Instance | undefined {
|
export function getInstanceCache(server: string): mastodon.v2.Instance | undefined {
|
||||||
return instanceStorage.value[server]
|
return instanceStorage.value[server]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ export const currentInstance = computed<null | ElkInstance>(() => {
|
||||||
})
|
})
|
||||||
|
|
||||||
export function getInstanceDomain(instance: ElkInstance) {
|
export function getInstanceDomain(instance: ElkInstance) {
|
||||||
return instance.accountDomain || withoutProtocol(instance.uri)
|
return instance.accountDomain || withoutProtocol(instance.domain || '')
|
||||||
}
|
}
|
||||||
|
|
||||||
export const publicServer = ref('')
|
export const publicServer = ref('')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue