增加按设备统计的群消息未读角标
This commit is contained in:
1 parent
f54077ce46
commit
187923ca3c
16 files changed
+189
-23
No files matched your search
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
||||
import { useSession } from '../session'
|
||||
import { APIError } from '../api'
|
||||
import { downloadAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
import MediaPreview from './MediaPreview.vue'
|
||||
@@ -12,6 +13,7 @@ import { discardPendingGroupMessage, forwardGroupMessage, groupAttachmentPath, g
|
||||
import type { ForwardDestination, GroupDetail, GroupDevice, GroupLine } from './group-chat'
|
||||
|
||||
const props = defineProps<{ group: GroupDetail }>()
|
||||
const emit = defineEmits<{ seen: [] }>()
|
||||
const session = useSession()
|
||||
const view = ref<Awaited<ReturnType<typeof synchronizeGroup>>>()
|
||||
const code = ref('')
|
||||
@@ -43,6 +45,8 @@ const canPublish = computed(() => Boolean(view.value && !view.value.current && m
|
||||
let timer: ReturnType<typeof setInterval>
|
||||
let refreshTimer: ReturnType<typeof setTimeout> | undefined
|
||||
let refreshPending = false
|
||||
let lastSeen = '0'
|
||||
let active = true
|
||||
|
||||
function pinned(device: GroupDevice) {
|
||||
return view.value?.pinned[device.id] === device.publicKey
|
||||
@@ -52,12 +56,25 @@ async function refresh() {
|
||||
const identity = session.identity
|
||||
if (!code.value || !identity) return
|
||||
view.value = await synchronizeGroup(identity, props.group, session.authorized)
|
||||
if (!active) return
|
||||
for (const member of members.value) {
|
||||
const devices = view.value.devices.filter((device) => device.user === member.id)
|
||||
if (!devices.some((device) => device.id === selected.value[member.id] && pinned(device))) {
|
||||
selected.value[member.id] = devices.find(pinned)?.id ?? ''
|
||||
}
|
||||
}
|
||||
if (view.value.hasMore) queueRefresh()
|
||||
const { cursor } = view.value
|
||||
if (!ready.value || document.visibilityState !== 'visible' || cursor === lastSeen || cursor === '0') return
|
||||
try {
|
||||
await session.authorized('/groups/' + encodeURIComponent(props.group.id) + '/seen', 'POST', { sequence: cursor })
|
||||
} catch (cause) {
|
||||
// Retention or member changes can invalidate an already cached cursor.
|
||||
if (!(cause instanceof APIError) || cause.status !== 404) throw cause
|
||||
}
|
||||
if (!active) return
|
||||
lastSeen = cursor
|
||||
emit('seen')
|
||||
}
|
||||
|
||||
async function run(action: () => Promise<void>, manual = true) {
|
||||
@@ -75,7 +92,7 @@ async function run(action: () => Promise<void>, manual = true) {
|
||||
}
|
||||
|
||||
function queueRefresh() {
|
||||
if (!code.value) return
|
||||
if (!active || !code.value) return
|
||||
refreshPending = true
|
||||
if (busy.value || refreshTimer) return
|
||||
refreshTimer = setTimeout(() => {
|
||||
@@ -223,11 +240,16 @@ watch([pendingVoice, busy, currentEpoch, () => view.value?.hasPending], ([voice,
|
||||
await refresh()
|
||||
})
|
||||
})
|
||||
onMounted(() => { timer = setInterval(queueRefresh, 15_000) })
|
||||
onMounted(() => {
|
||||
timer = setInterval(queueRefresh, 15_000)
|
||||
document.addEventListener('visibilitychange', queueRefresh)
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
refreshPending = false
|
||||
clearTimeout(refreshTimer)
|
||||
clearInterval(timer)
|
||||
document.removeEventListener('visibilitychange', queueRefresh)
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ export type GroupMember = { id: string; username: string; state: 'active' | 'inv
|
||||
export type GroupDetail = { id: string; membershipVersion: string; members: GroupMember[]; role: GroupMember['role'] }
|
||||
export type GroupPreferences = { pinned: boolean; muted: boolean }
|
||||
export type GroupSummary = Omit<GroupDetail, 'members'> & GroupPreferences & { name: string; state: GroupMember['state'] }
|
||||
export type ListedGroup = GroupSummary & { unread: number }
|
||||
export type GroupDevice = Peer & { user: string }
|
||||
type Epoch = { id: string; creatorDevice: string; membershipVersion: string; welcome?: string; current: boolean; roster: string[] }
|
||||
type Publishing = { id: string; membershipVersion: string; devices: { device: string; wire?: string }[] }
|
||||
@@ -271,7 +272,8 @@ export function synchronizeGroup(identity: ChatIdentity, group: GroupDetail, cal
|
||||
const engine = factory.DeviceMls.restore(decode(state.engine))
|
||||
try { await replenishPackages(identity, call, state, save, engine) } catch { warning ||= '密钥包补充失败' } finally { engine.free() }
|
||||
}
|
||||
return { devices, lines: chat.lines, pinned: state.pinned, ready, current, warning,
|
||||
const hasMore = packets.length === 64 && packets.at(-1)?.sequence === chat.cursor
|
||||
return { devices, lines: chat.lines, pinned: state.pinned, ready, current, warning, cursor: chat.cursor, hasMore,
|
||||
hasPending: Boolean(chat.pendingMessage), stalePending: Boolean(chat.pendingMessage && chat.pendingMessage.epoch !== current?.id) }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ const recent = ref<Record<string, string>>({})
|
||||
const conversationQuery = ref('')
|
||||
const history = ref<HTMLOListElement>()
|
||||
const shareRead = ref(false)
|
||||
const pendingVoice = ref<File>()
|
||||
const pendingFile = ref<File>()
|
||||
const view = ref<Awaited<ReturnType<typeof synchronize>>>()
|
||||
const target = computed(() => typeof route.params.user === 'string' ? route.params.user : '')
|
||||
const peer = computed(() => view.value?.peers.find((item) => item.id === selected.value))
|
||||
@@ -213,7 +213,7 @@ function attach(event: Event) {
|
||||
const input = event.target as HTMLInputElement
|
||||
const file = input.files?.[0]
|
||||
input.value = ''
|
||||
if (file) sendFile(file)
|
||||
if (file) pendingFile.value = file
|
||||
}
|
||||
|
||||
function sendFile(file: File) {
|
||||
@@ -225,12 +225,12 @@ function sendFile(file: File) {
|
||||
}
|
||||
|
||||
function sendVoice(file: File) {
|
||||
pendingVoice.value = file
|
||||
pendingFile.value = file
|
||||
}
|
||||
|
||||
watch([pendingVoice, busy], ([file, inProgress]) => {
|
||||
watch([pendingFile, busy], ([file, inProgress]) => {
|
||||
if (!file || inProgress) return
|
||||
pendingVoice.value = undefined
|
||||
pendingFile.value = undefined
|
||||
sendFile(file)
|
||||
})
|
||||
|
||||
@@ -286,7 +286,7 @@ watch(selected, () => {
|
||||
recalling.value = undefined
|
||||
forwarding.value = undefined
|
||||
forwardStatus.value = ''
|
||||
pendingVoice.value = undefined
|
||||
pendingFile.value = undefined
|
||||
draft.value = ''
|
||||
scheduleReceipt()
|
||||
})
|
||||
|
||||
@@ -5,7 +5,7 @@ import { useSession } from '../session'
|
||||
import { APIError } from '../api'
|
||||
import type { Contact } from '../contacts'
|
||||
import GroupChat from '../chat/GroupChat.vue'
|
||||
import type { GroupPreferences, GroupSummary } from '../chat/group-chat'
|
||||
import type { GroupPreferences, GroupSummary, ListedGroup } from '../chat/group-chat'
|
||||
|
||||
type Group = GroupSummary
|
||||
type Detail = Group & { members: { id: string; username: string; state: Group['state']; role: Group['role'] }[] }
|
||||
@@ -13,7 +13,7 @@ type Detail = Group & { members: { id: string; username: string; state: Group['s
|
||||
const session = useSession()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const groups = ref<Group[]>([])
|
||||
const groups = ref<ListedGroup[]>([])
|
||||
const contacts = ref<Contact[]>([])
|
||||
const detail = ref<Detail>()
|
||||
const term = ref('')
|
||||
@@ -37,7 +37,7 @@ let revision = 0
|
||||
async function refresh() {
|
||||
const version = ++revision
|
||||
const selected = id.value
|
||||
let list = await session.authorized<Group[]>('/groups')
|
||||
let list = await session.authorized<ListedGroup[]>('/groups')
|
||||
let next: Detail | undefined
|
||||
if (list.some((group) => group.id === selected)) {
|
||||
try {
|
||||
@@ -129,6 +129,7 @@ watch(id, () => {
|
||||
else reload()
|
||||
})
|
||||
watch(() => session.groupListChanges, queueRefresh)
|
||||
watch(() => session.groupChange, queueRefresh)
|
||||
watch(() => session.connection, (connection) => { if (connection === '实时连接已建立') queueRefresh() })
|
||||
onMounted(() => {
|
||||
reload()
|
||||
@@ -157,6 +158,10 @@ onBeforeUnmount(() => {
|
||||
<strong>{{ group.name }}</strong>
|
||||
<small>{{ group.state === 'invited' ? '待接受邀请' : roles[group.role] }}<template v-if="group.pinned"> · 已置顶</template><template v-if="group.muted"> · 免打扰</template></small>
|
||||
</span>
|
||||
<span v-if="group.unread" class="group-unread" :class="{ muted: group.muted }">
|
||||
<span aria-hidden="true">{{ group.unread >= 100 ? '99+' : group.unread }}</span>
|
||||
<span class="unread-label">{{ group.unread >= 100 ? '99 条以上未读' : group.unread + ' 条未读' }}</span>
|
||||
</span>
|
||||
</RouterLink>
|
||||
</aside>
|
||||
<div class="device-content chat-page">
|
||||
@@ -175,7 +180,7 @@ onBeforeUnmount(() => {
|
||||
<button type="button" class="plain" :aria-pressed="detail.pinned" :disabled="busy" @click="setPreference('pinned')">置顶群聊</button>
|
||||
<button type="button" class="plain" :aria-pressed="detail.muted" :disabled="busy" @click="setPreference('muted')">群聊免打扰</button>
|
||||
</div>
|
||||
<GroupChat v-if="session.identity" :key="detail.id" :group="detail" />
|
||||
<GroupChat v-if="session.identity" :key="detail.id" :group="detail" @seen="queueRefresh" />
|
||||
<form v-if="manage" class="contact-preferences" @submit.prevent="change('rename', undefined, nextName)">
|
||||
<label>修改群名称<input v-model="nextName" required maxlength="64"></label>
|
||||
<button class="plain" :disabled="busy || !nextName.trim() || nextName.trim() === detail.name">保存群名称</button>
|
||||
@@ -219,3 +224,18 @@ onBeforeUnmount(() => {
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.group-unread {
|
||||
margin-inline-start: auto;
|
||||
min-width: 1.5rem;
|
||||
padding-inline: .35rem;
|
||||
border-radius: 1rem;
|
||||
background: var(--green-soft);
|
||||
color: var(--green);
|
||||
font-size: .8rem;
|
||||
text-align: center;
|
||||
}
|
||||
.group-unread.muted { background: var(--base); color: var(--muted); }
|
||||
.unread-label { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
|
||||
</style>
|
||||
Reference in new issue
Block a user