2024-01-09 11:56:15 +03:00
|
|
|
import type { mastodon } from 'masto'
|
2023-01-10 15:29:16 +03:00
|
|
|
|
2024-01-09 11:56:15 +03:00
|
|
|
const notifications = reactive<Record<string, undefined | [Promise<mastodon.streaming.Subscription>, string[]]>>({})
|
2023-01-10 15:29:16 +03:00
|
|
|
|
2023-03-30 22:01:24 +03:00
|
|
|
export function useNotifications() {
|
2023-01-10 15:29:16 +03:00
|
|
|
const id = currentUser.value?.account.id
|
2023-01-15 11:38:02 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const { client, streamingClient } = useMasto()
|
2023-01-10 15:29:16 +03:00
|
|
|
|
|
|
|
async function clearNotifications() {
|
|
|
|
if (!id || !notifications[id])
|
|
|
|
return
|
2024-02-16 19:48:53 +03:00
|
|
|
|
2023-01-10 15:29:16 +03:00
|
|
|
const lastReadId = notifications[id]![1][0]
|
|
|
|
notifications[id]![1] = []
|
2024-02-16 19:48:53 +03:00
|
|
|
|
2023-01-15 23:26:59 +03:00
|
|
|
if (lastReadId) {
|
2024-02-21 18:20:08 +03:00
|
|
|
await client.value.v1.markers.create({
|
2023-01-15 23:26:59 +03:00
|
|
|
notifications: { lastReadId },
|
|
|
|
})
|
|
|
|
}
|
2023-01-10 15:29:16 +03:00
|
|
|
}
|
|
|
|
|
2024-01-09 11:56:15 +03:00
|
|
|
async function processNotifications(stream: mastodon.streaming.Subscription, id: string) {
|
|
|
|
for await (const entry of stream) {
|
|
|
|
if (entry.event === 'notification' && notifications[id])
|
|
|
|
notifications[id]![1].unshift(entry.payload.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:29:16 +03:00
|
|
|
async function connect(): Promise<void> {
|
2024-01-09 11:56:15 +03:00
|
|
|
if (!isHydrated.value || !id || notifications[id] !== undefined || !currentUser.value?.token)
|
2023-01-10 15:29:16 +03:00
|
|
|
return
|
|
|
|
|
2024-02-16 19:48:53 +03:00
|
|
|
let resolveStream: ((value: mastodon.streaming.Subscription | PromiseLike<mastodon.streaming.Subscription>) => void) | undefined
|
2024-01-09 11:56:15 +03:00
|
|
|
const streamPromise = new Promise<mastodon.streaming.Subscription>(resolve => resolveStream = resolve)
|
|
|
|
notifications[id] = [streamPromise, []]
|
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
await until(streamingClient).toBeTruthy()
|
2023-01-15 23:26:59 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const stream = streamingClient.value!.user.subscribe()
|
2024-01-09 11:56:15 +03:00
|
|
|
resolveStream!(stream)
|
2023-01-15 11:38:02 +03:00
|
|
|
|
2024-01-09 11:56:15 +03:00
|
|
|
processNotifications(stream, id)
|
2023-01-10 15:29:16 +03:00
|
|
|
|
2024-02-21 18:20:08 +03:00
|
|
|
const position = await client.value.v1.markers.fetch({ timeline: ['notifications'] })
|
|
|
|
const paginator = client.value.v1.notifications.list({ limit: 30 })
|
2024-02-16 19:48:53 +03:00
|
|
|
|
2023-01-10 15:29:16 +03:00
|
|
|
do {
|
|
|
|
const result = await paginator.next()
|
|
|
|
if (!result.done && result.value.length) {
|
|
|
|
for (const notification of result.value) {
|
|
|
|
if (notification.id === position.notifications.lastReadId)
|
|
|
|
return
|
|
|
|
notifications[id]![1].push(notification.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} while (true)
|
|
|
|
}
|
|
|
|
|
|
|
|
function disconnect(): void {
|
|
|
|
if (!id || !notifications[id])
|
|
|
|
return
|
2024-01-09 11:56:15 +03:00
|
|
|
notifications[id]![0].then(stream => stream.unsubscribe())
|
2023-01-10 15:29:16 +03:00
|
|
|
notifications[id] = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(currentUser, disconnect)
|
2023-01-15 11:38:02 +03:00
|
|
|
|
|
|
|
onHydrated(() => {
|
2023-01-10 15:29:16 +03:00
|
|
|
connect()
|
2023-01-15 11:38:02 +03:00
|
|
|
})
|
2023-01-10 15:29:16 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
notifications: computed(() => id ? notifications[id]?.[1].length ?? 0 : 0),
|
|
|
|
clearNotifications,
|
|
|
|
}
|
|
|
|
}
|