diff --git a/app/components/timeline/TimelineNotifications.vue b/app/components/timeline/TimelineNotifications.vue index 035446e9..548e7020 100644 --- a/app/components/timeline/TimelineNotifications.vue +++ b/app/components/timeline/TimelineNotifications.vue @@ -14,11 +14,8 @@ const options = { limit: 30, types: filter ? [filter] : [] } // Default limit is 20 notifications, and servers are normally caped to 30 const paginator = useMastoClient().v1.notifications.list(options) -// streaming requires user session -let stream: Ref -if (currentUser.value !== undefined) - // @ts-expect-error Type error should be fixed with the following PR to masto.js: https://github.com/neet/masto.js/pull/1355 - stream = useStreaming(client => client.user.notification.subscribe()) +// @ts-expect-error Type error should be fixed with the following PR to masto.js: https://github.com/neet/masto.js/pull/1355 +const stream = useStreaming(client => client.user.notification.subscribe()) lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '') diff --git a/app/composables/masto/masto.ts b/app/composables/masto/masto.ts index 3a217519..5b90ccc9 100644 --- a/app/composables/masto/masto.ts +++ b/app/composables/masto/masto.ts @@ -26,59 +26,57 @@ export function mastoLogin(masto: ElkMasto, user: Pick mastodon.streaming.Client | undefined + const createStreamingClient = (streamingApiUrl: string | undefined) => { + // Only create the streaming client when there is a user session + return streamingApiUrl && currentUser.value + ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) + : undefined + } + const streamingApiUrl = instance?.configuration?.urls?.streaming masto.client.value = createRestAPIClient({ url, accessToken }) + masto.streamingClient.value = createStreamingClient(streamingApiUrl) - if (currentUser.value !== undefined) { - createStreamingClient = (streamingApiUrl: string | undefined) => { - return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined + // Refetch instance info in the background on login + masto.client.value.v2.instance.fetch().catch(error => new Promise((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) } - const streamingApiUrl = instance?.configuration?.urls?.streaming - masto.streamingClient.value = createStreamingClient(streamingApiUrl) + return reject(error) + })).then((newInstance) => { + Object.assign(instance, newInstance) + if (newInstance.configuration.urls.streaming !== streamingApiUrl) + masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming) - // Refetch instance info in the background on login - masto.client.value.v2.instance.fetch().catch(error => new Promise((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.configuration.urls.streaming !== streamingApiUrl) - masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming) - - instanceStorage.value[server] = newInstance - }) - } + instanceStorage.value[server] = newInstance + }) return instance }