feat: check user session before subscribing to streaming (#3)
Reviewed-on: #3 Co-authored-by: Ayo <ayo@ayco.io> Co-committed-by: Ayo <ayo@ayco.io>
This commit is contained in:
parent
ef4c06d982
commit
29b1841280
2 changed files with 47 additions and 52 deletions
|
|
@ -14,11 +14,8 @@ const options = { limit: 30, types: filter ? [filter] : [] }
|
||||||
// Default limit is 20 notifications, and servers are normally caped to 30
|
// Default limit is 20 notifications, and servers are normally caped to 30
|
||||||
const paginator = useMastoClient().v1.notifications.list(options)
|
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
|
// @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\/?/, '')
|
lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 instance: ElkInstance = reactive(getInstanceCache(server) || { uri: server, accountDomain: server })
|
||||||
const accessToken = user.token
|
const accessToken = user.token
|
||||||
|
|
||||||
let createStreamingClient: (streamingApiUrl: string | undefined) => mastodon.streaming.Client | undefined
|
const createStreamingClient = (streamingApiUrl: string | undefined) => {
|
||||||
|
// Only create the streaming client when there is a user session
|
||||||
masto.client.value = createRestAPIClient({ url, accessToken })
|
return streamingApiUrl && currentUser.value
|
||||||
|
? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket })
|
||||||
if (currentUser.value !== undefined) {
|
: undefined
|
||||||
createStreamingClient = (streamingApiUrl: string | undefined) => {
|
|
||||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const streamingApiUrl = instance?.configuration?.urls?.streaming
|
const streamingApiUrl = instance?.configuration?.urls?.streaming
|
||||||
|
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
|
||||||
|
|
@ -78,7 +77,6 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
|
||||||
|
|
||||||
instanceStorage.value[server] = newInstance
|
instanceStorage.value[server] = newInstance
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue