增加加密群消息引用、编辑和撤回

This commit is contained in:
xhgit committed 2026-11-14 09:25:45 +08:00
1 parent de722039d6
commit 7beb0cf795
7 files changed
+177 -36

No files matched your search

+67 -10
View File
@@ -7,8 +7,8 @@ import MediaPreview from './MediaPreview.vue'
import VoiceMessage from './VoiceMessage.vue'
import VoiceRecorder from './VoiceRecorder.vue'
import { enableChat, trust } from './chat'
import { discardPendingGroupMessage, groupAttachmentPath, publishGroup, sendGroupMessage, synchronizeGroup } from './group-chat'
import type { GroupDetail, GroupDevice } from './group-chat'
import { discardPendingGroupMessage, groupAttachmentPath, groupQuote, publishGroup, sendGroupMessage, synchronizeGroup } from './group-chat'
import type { GroupDetail, GroupDevice, GroupLine } from './group-chat'
const props = defineProps<{ group: GroupDetail }>()
const session = useSession()
@@ -17,12 +17,15 @@ const code = ref('')
const codes = ref<Record<string, string>>({})
const selected = ref<Record<string, string>>({})
const draft = ref('')
const replying = ref<GroupLine>()
const editing = ref<GroupLine>()
const recalling = ref<GroupLine>()
const file = ref<File>()
const pendingVoice = ref<{ file: File; epoch: string }>()
const busy = ref(false)
const error = ref('')
const ready = computed(() => view.value?.ready && view.value.current?.membershipVersion === props.group.membershipVersion)
const recordingEpoch = computed(() => ready.value ? view.value?.current?.id : undefined)
const currentEpoch = computed(() => ready.value ? view.value?.current?.id : undefined)
const members = computed(() => props.group.members.filter((member) => member.state === 'active' && member.id !== session.identity?.user.id))
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)))))
@@ -86,8 +89,31 @@ function send() {
const identity = session.identity
if (identity) void run(async () => {
const text = draft.value
await sendGroupMessage(identity, props.group, text, session.authorized)
if (editing.value?.epoch) {
await sendGroupMessage(identity, props.group, { action: 'edit', id: editing.value.id, epoch: editing.value.epoch, text }, session.authorized)
} else {
await sendGroupMessage(identity, props.group, replying.value ? { text, reply: replying.value } : text, session.authorized)
}
if (draft.value === text) draft.value = ''
editing.value = undefined
replying.value = undefined
await refresh()
})
}
function edit(line: GroupLine) {
editing.value = line
replying.value = undefined
draft.value = line.text
}
function recall() {
const identity = session.identity
const { id, epoch } = recalling.value ?? {}
if (!identity || !id || !epoch) return
void run(async () => {
await sendGroupMessage(identity, props.group, { action: 'recall', id, epoch }, session.authorized)
recalling.value = undefined
await refresh()
})
}
@@ -125,7 +151,14 @@ function discard() {
}
watch(() => props.group.membershipVersion, () => void run(refresh))
watch([pendingVoice, busy, recordingEpoch, () => view.value?.hasPending], ([voice, inProgress, epoch, hasPending]) => {
watch(currentEpoch, (epoch, previous) => {
if (!previous || epoch === previous) return
if (editing.value || replying.value) draft.value = ''
editing.value = undefined
replying.value = undefined
recalling.value = undefined
})
watch([pendingVoice, busy, currentEpoch, () => view.value?.hasPending], ([voice, inProgress, epoch, hasPending]) => {
const identity = session.identity
if (!voice || !identity) return
if (voice.epoch !== epoch) {
@@ -180,27 +213,50 @@ onBeforeUnmount(() => clearInterval(timer))
<button v-if="group.role === 'owner' && !view?.current" class="primary" :disabled="busy || !canPublish" @click="establish">建立群加密会话</button>
</details>
<ul v-if="view?.lines.length" class="group-chat-lines" aria-label="群消息记录">
<li v-for="line in view.lines" :key="line.id">
<li v-for="line in view.lines" :key="[line.epoch, line.sender, line.id].join('/')">
<small>{{ line.name }} · {{ new Date(line.time).toLocaleString() }}</small>
<VoiceMessage v-if="line.attachment?.voice" :attachment="line.attachment" :path="groupAttachmentPath(group, line.attachment.id)" />
<blockquote v-if="line.quote">{{ line.quote }}</blockquote>
<p v-if="line.recalled" class="muted">消息已撤回</p>
<VoiceMessage v-else-if="line.attachment?.voice" :attachment="line.attachment" :path="groupAttachmentPath(group, line.attachment.id)" />
<template v-else-if="line.attachment">
<p>{{ line.attachment.name }} · {{ new Intl.NumberFormat().format(line.attachment.size) }} 字节</p>
<MediaPreview v-if="line.attachment.media" :attachment="line.attachment" :path="groupAttachmentPath(group, line.attachment.id)" />
<button type="button" class="plain" :disabled="busy" @click="download(line.attachment)">下载 {{ line.attachment.name }}</button>
</template>
<p v-else>{{ line.text }}</p>
<small v-if="line.edited && !line.recalled" class="muted">已编辑</small>
<div v-if="currentEpoch && line.epoch === currentEpoch && !line.recalled" class="message-actions">
<button type="button" class="plain" :disabled="busy || view.hasPending || !!editing" @click="replying = line">引用</button>
<template v-if="line.sender === session.identity?.device.id">
<button v-if="!line.attachment" type="button" class="plain" :disabled="busy || view.hasPending" @click="edit(line)">编辑</button>
<button type="button" class="plain" :disabled="busy || view.hasPending" @click="recalling = line">撤回</button>
</template>
</div>
</li>
</ul>
<div v-if="ready && recalling" class="confirmation">
<p>撤回后,群成员的聊天界面会更新。成员可能已保存副本。</p>
<button type="button" class="plain" :disabled="busy || view?.hasPending" @click="recall">确认撤回</button>
<button type="button" class="plain" @click="recalling = undefined">取消</button>
</div>
<p v-if="view?.hasPending" role="status">
{{ view.stalePending ? '待发消息属于旧会话。' : '消息待投递,将自动重试。' }}
<button v-if="view.stalePending" class="plain" :disabled="busy" @click="discard">清除待发消息</button>
</p>
<form v-if="ready" class="group-chat-composer" @submit.prevent="send">
<div v-if="editing" class="chat-quote">
<span>编辑消息</span>
<button type="button" class="plain" @click="editing = undefined; draft = ''">取消编辑</button>
</div>
<div v-else-if="replying" class="chat-quote">
<span>引用 {{ groupQuote(replying) }}</span>
<button type="button" class="plain" @click="replying = undefined">取消引用</button>
</div>
<label>群消息<textarea v-model="draft" rows="3" maxlength="8000" required /></label>
<button class="primary" :disabled="busy || view?.hasPending || !draft.trim()">发送群消息</button>
<button class="primary" :disabled="busy || view?.hasPending || !draft.trim()">{{ editing ? '保存编辑' : '发送群消息' }}</button>
</form>
<div v-if="ready" class="group-chat-composer">
<VoiceRecorder v-if="recordingEpoch" :key="recordingEpoch" :epoch="recordingEpoch" :disabled="busy || view?.hasPending || !!pendingVoice"
<div v-if="ready && !editing" class="group-chat-composer">
<VoiceRecorder v-if="currentEpoch" :key="currentEpoch" :epoch="currentEpoch" :disabled="busy || view?.hasPending || !!pendingVoice"
@send="sendVoice" @error="error = $event" />
<label>选择群附件 <input type="file" :disabled="busy || view?.hasPending" @change="chooseFile"></label>
<div v-if="file" class="message-actions">
@@ -220,6 +276,7 @@ onBeforeUnmount(() => clearInterval(timer))
.group-device-member { margin-block: .75rem; }
.group-chat-lines { display: grid; gap: .75rem; max-height: 24rem; overflow-y: auto; margin: 0; padding: .7rem 0; list-style: none; }
.group-chat-lines p { white-space: pre-wrap; overflow-wrap: anywhere; }
.group-chat-lines blockquote { margin: .25rem 0; padding-inline-start: .5rem; border-inline-start: 2px solid currentColor; overflow-wrap: anywhere; }
.group-chat-composer { display: grid; gap: .55rem; }
.group-chat-composer textarea { display: block; width: 100%; resize: vertical; font: inherit; }
.group-chat button { justify-self: start; }