feat: require session for streaming client
This commit is contained in:
parent
8959daae92
commit
c6aa287f26
7 changed files with 82 additions and 49 deletions
|
|
@ -6,7 +6,12 @@ const isSlow = computed(() => isSupported.value && effectiveType.value && ['slow
|
|||
const limit = computed(() => isSlow.value ? 10 : 30)
|
||||
|
||||
const paginator = useMastoClient().v1.timelines.home.list({ limit: limit.value })
|
||||
const stream = useStreaming(client => client.user.subscribe())
|
||||
|
||||
// streaming requires user session
|
||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
||||
if (currentUser.value !== undefined)
|
||||
stream = useStreaming(client => client.user.subscribe())
|
||||
|
||||
function reorderAndFilter(items: mastodon.v1.Status[]) {
|
||||
return reorderedTimeline(items, 'home')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,11 @@ 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)
|
||||
|
||||
// @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())
|
||||
// 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())
|
||||
|
||||
lastAccessedNotificationRoute.value = route.path.replace(/\/notifications\/?/, '')
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,12 @@
|
|||
import type { mastodon } from 'masto'
|
||||
|
||||
const paginator = useMastoClient().v1.timelines.public.list({ limit: 30 })
|
||||
const stream = useStreaming(client => client.public.subscribe())
|
||||
|
||||
// streaming requires user session
|
||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
||||
if (currentUser.value !== undefined)
|
||||
stream = useStreaming(client => client.public.subscribe())
|
||||
|
||||
function reorderAndFilter(items: mastodon.v1.Status[]) {
|
||||
return reorderedTimeline(items, 'public')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,12 @@
|
|||
import type { mastodon } from 'masto'
|
||||
|
||||
const paginator = useMastoClient().v1.timelines.public.list({ limit: 30, local: true })
|
||||
const stream = useStreaming(client => client.public.local.subscribe())
|
||||
|
||||
// streaming requires user session
|
||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
||||
if (currentUser.value !== undefined)
|
||||
stream = useStreaming(client => client.public.local.subscribe())
|
||||
|
||||
function reorderAndFilter(items: mastodon.v1.Status[]) {
|
||||
return reorderedTimeline(items, 'public')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,54 +26,59 @@ export function mastoLogin(masto: ElkMasto, user: Pick<UserLogin, 'server' | 'to
|
|||
const instance: ElkInstance = reactive(getInstanceCache(server) || { uri: server, accountDomain: server })
|
||||
const accessToken = user.token
|
||||
|
||||
const createStreamingClient = (streamingApiUrl: string | undefined) => {
|
||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
||||
}
|
||||
let createStreamingClient: (streamingApiUrl: string | undefined) => mastodon.streaming.Client | 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
|
||||
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)
|
||||
if (currentUser.value !== undefined) {
|
||||
createStreamingClient = (streamingApiUrl: string | undefined) => {
|
||||
return streamingApiUrl ? createStreamingAPIClient({ streamingApiUrl, accessToken, implementation: globalThis.WebSocket }) : undefined
|
||||
}
|
||||
|
||||
return reject(error)
|
||||
})).then((newInstance) => {
|
||||
Object.assign(instance, newInstance)
|
||||
if (newInstance.configuration.urls.streaming !== streamingApiUrl)
|
||||
masto.streamingClient.value = createStreamingClient(newInstance.configuration.urls.streaming)
|
||||
const streamingApiUrl = instance?.configuration?.urls?.streaming
|
||||
masto.streamingClient.value = createStreamingClient(streamingApiUrl)
|
||||
|
||||
instanceStorage.value[server] = newInstance
|
||||
})
|
||||
// Refetch instance info in the background on login
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import type { mastodon } from 'masto'
|
||||
|
||||
definePageMeta({
|
||||
name: 'list',
|
||||
})
|
||||
|
|
@ -9,7 +11,11 @@ const listId = computed(() => params.list as string)
|
|||
const client = useMastoClient()
|
||||
|
||||
const paginator = client.v1.timelines.list.$select(listId.value).list()
|
||||
const stream = useStreaming(client => client.list.subscribe({ list: listId.value }))
|
||||
|
||||
// streaming requires user session
|
||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
||||
if (currentUser.value !== undefined)
|
||||
stream = useStreaming(client => client.list.subscribe({ list: listId.value }))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
|||
|
|
@ -12,7 +12,11 @@ const { client } = useMasto()
|
|||
const { data: tag, refresh } = await useAsyncData(() => `tag-${tagName.value}`, () => client.value.v1.tags.$select(tagName.value).fetch(), { default: () => shallowRef() })
|
||||
|
||||
const paginator = client.value.v1.timelines.tag.$select(tagName.value).list()
|
||||
const stream = useStreaming(client => client.hashtag.subscribe({ tag: tagName.value }))
|
||||
|
||||
// streaming requires user session
|
||||
let stream: Ref<mastodon.streaming.Subscription | undefined>
|
||||
if (currentUser.value !== undefined)
|
||||
stream = useStreaming(client => client.hashtag.subscribe({ tag: tagName.value }))
|
||||
|
||||
if (tag.value) {
|
||||
useHydratedHead({
|
||||
|
|
|
|||
Loading…
Reference in a new issue