Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace deprecated v1.instance #3183

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions composables/masto/masto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { mastodon } from 'masto'
import type { Ref } from 'vue'
import type { ElkInstance } from '../users'
import type { UserLogin } from '~/types'
import { createRestAPIClient, createStreamingAPIClient } from 'masto'
import { createRestAPIClient, createStreamingAPIClient, MastoHttpError } from 'masto'

export function createMasto() {
return {
Expand All @@ -30,15 +30,47 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
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.streamingClient.value = createStreamingClient(streamingApiUrl)

// 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)
if (newInstance.urls.streamingApi !== streamingApiUrl)
masto.streamingClient.value = createStreamingClient(newInstance.urls.streamingApi)
if (newInstance.configuration.urls.streaming !== streamingApiUrl)
masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming)

instanceStorage.value[server] = newInstance
})
Expand Down
9 changes: 4 additions & 5 deletions composables/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 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> & {
uri: string
export type ElkInstance = Partial<mastodon.v2.Instance> & {
/** support GoToSocial */
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]
}

Expand All @@ -52,7 +51,7 @@ export const currentInstance = computed<null | ElkInstance>(() => {
})

export function getInstanceDomain(instance: ElkInstance) {
return instance.accountDomain || withoutProtocol(instance.uri)
return instance.accountDomain || withoutProtocol(instance.domain || '')
}

export const publicServer = ref('')
Expand Down