接入群消息实时提醒和重连补拉

This commit is contained in:
xhgit committed 2026-11-14 14:59:24 +08:00
1 parent 5ace82b0d7
commit b2e04e8174
10 files changed
+122 -33

No files matched your search

+24 -3
View File
@@ -41,6 +41,8 @@ const members = computed(() => props.group.members.filter((member) => member.sta
const canPublish = computed(() => Boolean(view.value && !view.value.current && members.value.every((member) =>
view.value?.devices.some((device) => device.user === member.id && device.id === selected.value[member.id] && pinned(device)))))
let timer: ReturnType<typeof setInterval>
let refreshTimer: ReturnType<typeof setTimeout> | undefined
let refreshPending = false
function pinned(device: GroupDevice) {
return view.value?.pinned[device.id] === device.publicKey
@@ -68,9 +70,22 @@ async function run(action: () => Promise<void>, manual = true) {
error.value = cause instanceof Error ? cause.message : '群消息操作失败'
} finally {
busy.value = false
if (refreshPending) queueRefresh()
}
}
function queueRefresh() {
if (!code.value) return
refreshPending = true
if (busy.value || refreshTimer) return
refreshTimer = setTimeout(() => {
refreshTimer = undefined
if (busy.value) return
refreshPending = false
void run(refresh, false)
}, 350)
}
function enable() {
const identity = session.identity
if (identity) void run(async () => {
@@ -183,7 +198,9 @@ function discard() {
})
}
watch(() => props.group.membershipVersion, () => void run(refresh))
watch(() => props.group.membershipVersion, queueRefresh)
watch(() => session.groupChange, (event) => { if (event?.groupId === props.group.id) queueRefresh() })
watch(() => session.connection, (connection) => { if (connection === '实时连接已建立') queueRefresh() })
watch(currentEpoch, (epoch, previous) => {
if (!previous || epoch === previous) return
if (editing.value || replying.value) draft.value = ''
@@ -206,8 +223,12 @@ watch([pendingVoice, busy, currentEpoch, () => view.value?.hasPending], ([voice,
await refresh()
})
})
onMounted(() => { timer = setInterval(() => { if (code.value) void run(refresh, false) }, 2500) })
onBeforeUnmount(() => clearInterval(timer))
onMounted(() => { timer = setInterval(queueRefresh, 15_000) })
onBeforeUnmount(() => {
refreshPending = false
clearTimeout(refreshTimer)
clearInterval(timer)
})
</script>
<template>
+5 -2
View File
@@ -26,6 +26,7 @@ export const useSession = defineStore('session', () => {
const connection = ref('离线')
const contactChanges = ref(0)
const inboxChanges = ref(0)
const groupChange = shallowRef<{ groupId: string }>()
let token = ''
let socket: WebSocket | undefined
let retry: ReturnType<typeof setTimeout> | undefined
@@ -34,6 +35,7 @@ export const useSession = defineStore('session', () => {
function clear() {
token = ''
identity.value = undefined
groupChange.value = undefined
connection.value = '离线'
clearTimeout(retry)
const previous = socket
@@ -51,13 +53,14 @@ export const useSession = defineStore('session', () => {
socket = active
active.onmessage = (event) => {
if (socket !== active) return
const { type } = JSON.parse(event.data)
const { type, groupId } = JSON.parse(event.data)
if (type === 'ready') {
connection.value = '实时连接已建立'
attempts = 0
}
if (type === 'contacts_changed') contactChanges.value++
if (type === 'inbox_changed') inboxChanges.value++
if (type === 'group_changed' && typeof groupId === 'string') groupChange.value = { groupId }
}
active.onclose = (event) => {
if (socket !== active) return
@@ -109,7 +112,7 @@ export const useSession = defineStore('session', () => {
}
return {
identity, connection, contactChanges, inboxChanges, authenticate, logout, authorized,
identity, connection, contactChanges, inboxChanges, groupChange, authenticate, logout, authorized,
devices: () => authorized<Device[]>('/devices'),
revoke: (id: string) => authorized<void>('/devices/' + encodeURIComponent(id), 'DELETE'),
contacts: () => authorized<Contact[]>('/contacts'),