refactor: move notification to masto composables
This commit is contained in:
parent
60ccfc84fa
commit
de696d0300
66
composables/masto/notification.ts
Normal file
66
composables/masto/notification.ts
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import type { WsEvents } from 'masto'
|
||||||
|
|
||||||
|
const notifications = reactive<Record<string, undefined | [Promise<WsEvents>, string[]]>>({})
|
||||||
|
|
||||||
|
export const useNotifications = () => {
|
||||||
|
const id = currentUser.value?.account.id
|
||||||
|
const masto = useMasto()
|
||||||
|
|
||||||
|
async function clearNotifications() {
|
||||||
|
if (!id || !notifications[id])
|
||||||
|
return
|
||||||
|
const lastReadId = notifications[id]![1][0]
|
||||||
|
notifications[id]![1] = []
|
||||||
|
|
||||||
|
await masto.v1.markers.create({
|
||||||
|
notifications: { lastReadId },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async function connect(): Promise<void> {
|
||||||
|
if (!isMastoInitialised.value || !id || notifications[id] || !currentUser.value?.token)
|
||||||
|
return
|
||||||
|
|
||||||
|
const stream = masto.v1.stream.streamUser()
|
||||||
|
notifications[id] = [stream, []]
|
||||||
|
stream.then(s => s.on('notification', (n) => {
|
||||||
|
if (notifications[id])
|
||||||
|
notifications[id]![1].unshift(n.id)
|
||||||
|
}))
|
||||||
|
|
||||||
|
const position = await masto.v1.markers.fetch({ timeline: ['notifications'] })
|
||||||
|
const paginator = masto.v1.notifications.list({ limit: 30 })
|
||||||
|
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
|
||||||
|
notifications[id]![0].then(stream => stream.disconnect())
|
||||||
|
notifications[id] = undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(currentUser, disconnect)
|
||||||
|
if (isMastoInitialised.value)
|
||||||
|
connect()
|
||||||
|
else
|
||||||
|
watchOnce(isMastoInitialised, connect)
|
||||||
|
|
||||||
|
return {
|
||||||
|
notifications: computed(() => id ? notifications[id]?.[1].length ?? 0 : 0),
|
||||||
|
disconnect,
|
||||||
|
clearNotifications,
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { login as loginMasto } from 'masto'
|
import { login as loginMasto } from 'masto'
|
||||||
import type { WsEvents, mastodon } from 'masto'
|
import type { mastodon } from 'masto'
|
||||||
import type { Ref } from 'vue'
|
import type { Ref } from 'vue'
|
||||||
import type { MaybeComputedRef, RemovableRef } from '@vueuse/core'
|
import type { MaybeComputedRef, RemovableRef } from '@vueuse/core'
|
||||||
import type { ElkMasto, UserLogin } from '~/types'
|
import type { ElkMasto, UserLogin } from '~/types'
|
||||||
|
@ -259,71 +259,6 @@ export async function signout() {
|
||||||
await masto.loginTo(currentUser.value)
|
await masto.loginTo(currentUser.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifications = reactive<Record<string, undefined | [Promise<WsEvents>, string[]]>>({})
|
|
||||||
|
|
||||||
export const useNotifications = () => {
|
|
||||||
const id = currentUser.value?.account.id
|
|
||||||
const masto = useMasto()
|
|
||||||
|
|
||||||
async function clearNotifications() {
|
|
||||||
if (!id || !notifications[id])
|
|
||||||
return
|
|
||||||
const lastReadId = notifications[id]![1][0]
|
|
||||||
notifications[id]![1] = []
|
|
||||||
|
|
||||||
await masto.v1.markers.create({
|
|
||||||
notifications: { lastReadId },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async function connect(): Promise<void> {
|
|
||||||
if (!isMastoInitialised.value || !id || notifications[id] || !currentUser.value?.token)
|
|
||||||
return
|
|
||||||
|
|
||||||
const stream = masto.v1.stream.streamUser()
|
|
||||||
notifications[id] = [stream, []]
|
|
||||||
stream.then(s => s.on('notification', (n) => {
|
|
||||||
if (notifications[id])
|
|
||||||
notifications[id]![1].unshift(n.id)
|
|
||||||
}))
|
|
||||||
|
|
||||||
const position = await masto.v1.markers.fetch({ timeline: ['notifications'] })
|
|
||||||
const paginator = masto.v1.notifications.list({ limit: 30 })
|
|
||||||
do {
|
|
||||||
const result = await paginator.next()
|
|
||||||
if (result.value?.length) {
|
|
||||||
for (const notification of result.value as mastodon.v1.Notification[]) {
|
|
||||||
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
|
|
||||||
notifications[id]![0].then(stream => stream.disconnect())
|
|
||||||
notifications[id] = undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(currentUser, disconnect)
|
|
||||||
if (isMastoInitialised.value)
|
|
||||||
connect()
|
|
||||||
else
|
|
||||||
watchOnce(isMastoInitialised, connect)
|
|
||||||
|
|
||||||
return {
|
|
||||||
notifications: computed(() => id ? notifications[id]?.[1].length ?? 0 : 0),
|
|
||||||
disconnect,
|
|
||||||
clearNotifications,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function checkLogin() {
|
export function checkLogin() {
|
||||||
if (!currentUser.value) {
|
if (!currentUser.value) {
|
||||||
openSigninDialog()
|
openSigninDialog()
|
||||||
|
|
Loading…
Reference in a new issue