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
|
// @ts-expect-error Type error should be fixed with the following PR to masto.js: https://github.com/neet/masto.js/pull/1355
|
||||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
const stream = useStreaming(client => client.user.notification.subscribe())
|
||||||
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())
|
|
||||||
|
|
||||||
lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '')
|
lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,59 +26,57 @@ 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
|
||||||
|
return streamingApiUrl && currentUser.value
|
||||||
|
? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket })
|
||||||
|
: undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
const streamingApiUrl = instance?.configuration?.urls?.streaming
|
||||||
masto.client.value = createRestAPIClient({ url, accessToken })
|
masto.client.value = createRestAPIClient({ url, accessToken })
|
||||||
|
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
||||||
|
|
||||||
if (currentUser.value !== undefined) {
|
// Refetch instance info in the background on login
|
||||||
createStreamingClient = (streamingApiUrl: string | undefined) => {
|
masto.client.value.v2.instance.fetch().catch(error => new Promise<mastodon.v2.Instance>((resolve, reject) => {
|
||||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
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
|
return reject(error)
|
||||||
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
})).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
|
instanceStorage.value[server] = 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.configuration.urls.streaming !== streamingApiUrl)
|
|
||||||
masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming)
|
|
||||||
|
|
||||||
instanceStorage.value[server] = newInstance
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue