支持私聊消息加密转发到群聊

This commit is contained in:
xhgit committed 2026-11-14 14:52:22 +08:00
1 parent 20e179a877
commit 9028ba0eed
8 files changed
+99 -62

No files matched your search

@@ -6,7 +6,7 @@ import type { Peer } from './chat'
import type { Contact } from '../contacts'
import type { ForwardDestination, GroupSummary } from './group-chat'
const props = defineProps<{ groupId: string; pinned: Record<string, string>; busy: boolean }>()
const props = defineProps<{ sourceGroup?: string; pinned: Record<string, string>; busy: boolean }>()
const emit = defineEmits<{ cancel: []; confirm: [destination: ForwardDestination] }>()
const session = useSession()
const groups = ref<GroupSummary[]>([])
@@ -28,7 +28,7 @@ const target = computed<ForwardDestination | undefined>(() => {
onMounted(async () => {
try {
const [allGroups, allContacts] = await Promise.all([session.authorized<GroupSummary[]>('/groups'), session.contacts()])
groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.groupId)
groups.value = allGroups.filter((group) => group.state === 'active' && group.id !== props.sourceGroup)
contacts.value = allContacts.filter((contact) => contact.state === 'friend')
} catch (cause) {
error.value = cause instanceof Error ? cause.message : '无法读取转发目标'
@@ -57,7 +57,7 @@ watch(destination, async (value, _previous, onCleanup) => {
</script>
<template>
<form class="confirmation group-forward" aria-label="转发群消息" @submit.prevent="target && emit('confirm', target)">
<form class="confirmation forward-dialog" :aria-label="sourceGroup ? '转发群消息' : '转发私聊消息'" @submit.prevent="target && emit('confirm', target)">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<label>转发给
<select v-model="destination" :disabled="busy || loading">
@@ -83,5 +83,5 @@ watch(destination, async (value, _previous, onCleanup) => {
</template>
<style scoped>
.group-forward { display: grid; gap: .65rem; }
.forward-dialog { display: grid; gap: .65rem; }
</style>
+3 -3
View File
@@ -4,7 +4,7 @@ import { useSession } from '../session'
import { downloadAttachment } from './attachments'
import type { Attachment } from './attachments'
import MediaPreview from './MediaPreview.vue'
import GroupForward from './GroupForward.vue'
import ForwardDialog from './ForwardDialog.vue'
import VoiceMessage from './VoiceMessage.vue'
import VoiceRecorder from './VoiceRecorder.vue'
import { enableChat, trust } from './chat'
@@ -277,8 +277,8 @@ onBeforeUnmount(() => clearInterval(timer))
</div>
</li>
</ul>
<GroupForward v-if="forwarding && view" :key="[forwarding.epoch, forwarding.sender, forwarding.id].join('/')"
:group-id="group.id" :pinned="view.pinned" :busy="busy" @confirm="forward" @cancel="forwarding = undefined" />
<ForwardDialog v-if="forwarding && view" :key="[forwarding.epoch, forwarding.sender, forwarding.id].join('/')"
:source-group="group.id" :pinned="view.pinned" :busy="busy" @confirm="forward" @cancel="forwarding = undefined" />
<div v-if="ready && recalling" class="confirmation">
<p>撤回后,群成员的聊天界面会更新。成员可能已保存副本。</p>
<button type="button" class="plain" :disabled="busy || view?.hasPending" @click="recall">确认撤回</button>
+6 -4
View File
@@ -276,10 +276,12 @@ export function localMessageAction(identity: ChatIdentity, line: ChatLine, actio
})
}
export async function forwardMessage(identity: ChatIdentity, line: ChatLine, recipient: string, device: Peer, call: Call) {
if (line.recalled) throw new Error('撤回的消息无法转发')
const content = line.attachment ? await readForwardedFile(line.attachment, call) : line.text
await send(identity, recipient, device, content, call)
export function readPrivateForward(identity: ChatIdentity, message: Pick<ChatLine, 'id' | 'peer' | 'own'>, call: Call) {
return update(identity, async (state) => {
const line = state.lines.find((item) => item.id === message.id && item.peer === message.peer && item.own === message.own)
if (!line || line.recalled) throw new Error('此私聊消息无法转发')
return line.attachment ? readForwardedFile(line.attachment, call) : line.text
})
}
export function recentConversations(identity: ChatIdentity) {
+7 -4
View File
@@ -284,14 +284,17 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD
}
export async function forwardGroupMessage(identity: ChatIdentity, source: GroupDetail, message: LineRef, destination: ForwardDestination, call: Call) {
const content = await update(identity, async (state) => {
return forwardContent(identity, destination, call, () => update(identity, async (state) => {
const item = state.groupChats?.[source.id]?.lines.find((entry) => entry.id === message.id &&
entry.epoch === message.epoch && entry.sender === message.sender)
if (!item || item.recalled) throw new Error('此群消息无法转发')
return item.attachment ? readForwardedFile(item.attachment, call, groupAttachmentPath(source, item.attachment.id)) : item.text
})
}))
}
export async function forwardContent(identity: ChatIdentity, destination: ForwardDestination, call: Call, readContent: () => Promise<string | File>) {
if ('user' in destination) {
await send(identity, destination.user, destination.device, content, call)
await send(identity, destination.user, destination.device, await readContent(), call)
return '已提交转发'
}
@@ -300,7 +303,7 @@ export async function forwardGroupMessage(identity: ChatIdentity, source: GroupD
const view = await synchronizeGroup(identity, target, call)
if (!view.ready || !view.current) throw new Error('请先在目标群加入加密会话')
if (view.hasPending) throw new Error('目标群还有待处理的消息,请先打开目标群')
await sendGroupMessage(identity, target, content, call, view.current.id)
await sendGroupMessage(identity, target, await readContent(), call, view.current.id)
// At this point the outbox is durable. The target can retry after a network failure.
try {
const result = await synchronizeGroup(identity, target, call)
+20 -45
View File
@@ -2,8 +2,11 @@
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useSession } from '../session'
import { directory, enableChat, forwardMessage, localMessageAction, markRead, mutateMessage, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat'
import type { ChatLine, Peer } from '../chat/chat'
import { enableChat, localMessageAction, markRead, mutateMessage, readPrivateForward, recentConversations, send, setReadReceipts, synchronize, trust } from '../chat/chat'
import type { ChatLine } from '../chat/chat'
import { forwardContent } from '../chat/group-chat'
import type { ForwardDestination } from '../chat/group-chat'
import ForwardDialog from '../chat/ForwardDialog.vue'
import type { Contact } from '../contacts'
import { downloadAttachment } from '../chat/attachments'
import type { Attachment } from '../chat/attachments'
@@ -24,10 +27,7 @@ const deleting = ref<ChatLine>()
const editing = ref<ChatLine>()
const recalling = ref<ChatLine>()
const forwarding = ref<ChatLine>()
const destinations = ref<Contact[]>([])
const destination = ref('')
const destinationDevice = ref('')
const forwardDevices = ref<Peer[]>([])
const forwardStatus = ref('')
const error = ref('')
const busy = ref(false)
const contacts = ref<Contact[]>([])
@@ -249,31 +249,18 @@ function localAction(line: ChatLine, action: 'favorite' | 'remove') {
}
function beginForward(line: ChatLine) {
void execute(async () => {
destinations.value = (await session.contacts()).filter((contact) => contact.state === 'friend')
destination.value = ''
destinationDevice.value = ''
forwardDevices.value = []
forwarding.value = line
})
forwardStatus.value = ''
forwarding.value = line
}
function loadForwardDevices() {
destinationDevice.value = ''
forwardDevices.value = []
if (!destination.value) return
void execute(async () => {
const devices = await directory(session.authorized, destination.value)
forwardDevices.value = devices.filter((device) => device.mls && view.value?.pinned[device.id] === device.publicKey)
destinationDevice.value = forwardDevices.value[0]?.id ?? ''
})
}
function submitForward() {
void execute(async () => {
const device = forwardDevices.value.find((candidate) => candidate.id === destinationDevice.value)
if (!session.identity || !forwarding.value || !device) return
await forwardMessage(session.identity, forwarding.value, destination.value, device, session.authorized)
function submitForward(destination: ForwardDestination) {
const identity = session.identity
const message = forwarding.value
if (identity && message) void execute(async () => {
const status = await forwardContent(identity, destination, session.authorized,
() => readPrivateForward(identity, message, session.authorized))
if (forwarding.value !== message) return
forwardStatus.value = status
forwarding.value = undefined
await refresh()
})
@@ -298,6 +285,7 @@ watch(selected, () => {
editing.value = undefined
recalling.value = undefined
forwarding.value = undefined
forwardStatus.value = ''
pendingVoice.value = undefined
draft.value = ''
scheduleReceipt()
@@ -336,6 +324,7 @@ onBeforeUnmount(() => {
</aside>
<div class="device-content chat-page">
<p v-if="error" class="error" role="alert">{{ error }}</p>
<p v-if="forwardStatus" role="status">{{ forwardStatus }}</p>
<button v-if="!ownCode" type="button" class="primary" :disabled="busy" @click="enable">启用本机加密聊天</button>
<template v-else>
<details open>
@@ -405,22 +394,8 @@ onBeforeUnmount(() => {
<button type="button" class="plain" :disabled="busy" @click="recall">确认撤回</button>
<button type="button" class="plain" @click="recalling = undefined">取消</button>
</div>
<form v-if="forwarding" class="contact-preferences confirmation" @submit.prevent="submitForward">
<label>转发给
<select v-model="destination" :disabled="busy" @change="loadForwardDevices">
<option value="">选择联系人</option>
<option v-for="contact in destinations" :key="contact.id" :value="contact.id">{{ contact.alias || contact.username }}</option>
</select>
</label>
<label v-if="forwardDevices.length">接收设备
<select v-model="destinationDevice" :disabled="busy">
<option v-for="device in forwardDevices" :key="device.id" :value="device.id">{{ device.name }}</option>
</select>
</label>
<p v-else-if="destination && !busy" class="muted">请先在对方聊天中核验接收设备。</p>
<button class="primary" :disabled="busy || !destinationDevice">确认转发</button>
<button type="button" class="plain" :disabled="busy" @click="forwarding = undefined">取消</button>
</form>
<ForwardDialog v-if="forwarding && view" :key="[forwarding.peer, forwarding.id, forwarding.own].join('/')"
:pinned="view.pinned" :busy="busy" @confirm="submitForward" @cancel="forwarding = undefined" />
<form class="chat-compose" @submit.prevent="submit">
<div v-if="editing" class="chat-quote">
<span>正在编辑消息</span>