feat: check user session before subscribing to streaming (#3)
Some checks failed
ci / ci (push) Has been cancelled
build & push docker container / docker (push) Has been cancelled
ci / check-provenance (push) Has been cancelled

Reviewed-on: #3
Co-authored-by: Ayo <ayo@ayco.io>
Co-committed-by: Ayo <ayo@ayco.io>
This commit is contained in:
Ayo Ayco 2025-11-23 17:42:51 +00:00 committed by Ayo Ayco
parent ef4c06d982
commit 29b1841280
2 changed files with 47 additions and 52 deletions

View file

@ -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<mastodon.streaming.Subscription | undefined>
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())
const stream = useStreaming(client => client.user.notification.subscribe())
lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '')

View file

@ -26,16 +26,15 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
const instance: ElkInstance = reactive(getInstanceCache(server) || { uri: server, accountDomain: server })
const accessToken = user.token
let createStreamingClient: (streamingApiUrl: string | undefined) => mastodon.streaming.Client | undefined
masto.client.value = createRestAPIClient({ url, accessToken })
if (currentUser.value !== undefined) {
createStreamingClient = (streamingApiUrl: string | undefined) => {
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : 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)
// Refetch instance info in the background on login
@ -78,7 +77,6 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
instanceStorage.value[server] = newInstance
})
}
return instance
}