增加 Windows 与 Web 端到端加密群语音
This commit is contained in:
1 parent
9c84b38fd9
commit
de722039d6
8 files changed
+125
-16
No files matched your search
@@ -4,6 +4,8 @@ import { useSession } from '../session'
|
||||
import { downloadAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
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'
|
||||
@@ -16,9 +18,11 @@ const codes = ref<Record<string, string>>({})
|
||||
const selected = ref<Record<string, string>>({})
|
||||
const draft = ref('')
|
||||
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 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)))))
|
||||
@@ -104,6 +108,10 @@ function sendFile() {
|
||||
})
|
||||
}
|
||||
|
||||
function sendVoice(recording: File, epoch?: string) {
|
||||
if (epoch) pendingVoice.value = { file: recording, epoch }
|
||||
}
|
||||
|
||||
function download(attachment: Attachment) {
|
||||
void run(() => downloadAttachment(attachment, session.authorized, groupAttachmentPath(props.group, attachment.id)))
|
||||
}
|
||||
@@ -117,6 +125,21 @@ function discard() {
|
||||
}
|
||||
|
||||
watch(() => props.group.membershipVersion, () => void run(refresh))
|
||||
watch([pendingVoice, busy, recordingEpoch, () => view.value?.hasPending], ([voice, inProgress, epoch, hasPending]) => {
|
||||
const identity = session.identity
|
||||
if (!voice || !identity) return
|
||||
if (voice.epoch !== epoch) {
|
||||
pendingVoice.value = undefined
|
||||
error.value = '群加密会话已变更,请重新录音'
|
||||
return
|
||||
}
|
||||
if (inProgress || hasPending) return
|
||||
pendingVoice.value = undefined
|
||||
void run(async () => {
|
||||
await sendGroupMessage(identity, props.group, voice.file, session.authorized, voice.epoch)
|
||||
await refresh()
|
||||
})
|
||||
})
|
||||
onMounted(() => { timer = setInterval(() => { if (code.value) void run(refresh, false) }, 2500) })
|
||||
onBeforeUnmount(() => clearInterval(timer))
|
||||
</script>
|
||||
@@ -159,7 +182,8 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<ul v-if="view?.lines.length" class="group-chat-lines" aria-label="群消息记录">
|
||||
<li v-for="line in view.lines" :key="line.id">
|
||||
<small>{{ line.name }} · {{ new Date(line.time).toLocaleString() }}</small>
|
||||
<template v-if="line.attachment">
|
||||
<VoiceMessage v-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>
|
||||
@@ -176,6 +200,8 @@ onBeforeUnmount(() => clearInterval(timer))
|
||||
<button class="primary" :disabled="busy || view?.hasPending || !draft.trim()">发送群消息</button>
|
||||
</form>
|
||||
<div v-if="ready" class="group-chat-composer">
|
||||
<VoiceRecorder v-if="recordingEpoch" :key="recordingEpoch" :epoch="recordingEpoch" :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">
|
||||
<span>{{ file.name }}</span>
|
||||
|
||||
@@ -4,7 +4,7 @@ import { readAttachment } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
import { useSession } from '../session'
|
||||
|
||||
const props = defineProps<{ attachment: Attachment }>()
|
||||
const props = defineProps<{ attachment: Attachment; path?: string }>()
|
||||
const session = useSession()
|
||||
const src = ref('')
|
||||
const player = ref<HTMLAudioElement>()
|
||||
@@ -16,7 +16,7 @@ async function play() {
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const bytes = await readAttachment(props.attachment, session.authorized)
|
||||
const bytes = await readAttachment(props.attachment, session.authorized, props.path)
|
||||
if (!active) return
|
||||
src.value = URL.createObjectURL(new Blob([bytes], { type: 'audio/webm;codecs=opus' }))
|
||||
await nextTick()
|
||||
@@ -28,6 +28,12 @@ async function play() {
|
||||
}
|
||||
}
|
||||
|
||||
function playbackError() {
|
||||
URL.revokeObjectURL(src.value)
|
||||
src.value = ''
|
||||
error.value = '客户端无法播放此语音文件'
|
||||
}
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
if (src.value) URL.revokeObjectURL(src.value)
|
||||
@@ -35,7 +41,7 @@ onBeforeUnmount(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<audio v-if="src" ref="player" controls preload="metadata" :src="src" aria-label="语音消息" />
|
||||
<audio v-if="src" ref="player" controls preload="metadata" :src="src" aria-label="语音消息" @error="playbackError" />
|
||||
<button v-else type="button" class="plain" :disabled="busy" @click="play">{{ busy ? '正在解密语音…' : '播放语音' }}</button>
|
||||
<small v-if="error" class="error" role="alert">{{ error }}</small>
|
||||
</template>
|
||||
@@ -2,8 +2,8 @@
|
||||
import { onBeforeUnmount, ref } from 'vue'
|
||||
import { voiceFile } from './attachments'
|
||||
|
||||
defineProps<{ disabled?: boolean }>()
|
||||
const emit = defineEmits<{ send: [file: File]; error: [text: string] }>()
|
||||
const props = defineProps<{ disabled?: boolean; epoch?: string }>()
|
||||
const emit = defineEmits<{ send: [file: File, epoch?: string]; error: [text: string] }>()
|
||||
|
||||
const mimeType = 'audio/webm;codecs=opus'
|
||||
const recording = ref(false)
|
||||
@@ -33,6 +33,7 @@ async function start() {
|
||||
return
|
||||
}
|
||||
opening.value = true
|
||||
const epoch = props.epoch
|
||||
try {
|
||||
await window.xhchat?.allowVoiceRecording()
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
|
||||
@@ -46,7 +47,7 @@ async function start() {
|
||||
if (event.data.size) parts.push(event.data)
|
||||
})
|
||||
recorder.addEventListener('stop', () => {
|
||||
if (active && deliver && parts.length) emit('send', voiceFile(parts))
|
||||
if (active && deliver && parts.length) emit('send', voiceFile(parts), epoch)
|
||||
else if (active && deliver) emit('error', '录音没有采集到音频')
|
||||
recorder = undefined
|
||||
stopping.value = false
|
||||
|
||||
@@ -239,7 +239,7 @@ export function discardPendingGroupMessage(identity: ChatIdentity, group: GroupD
|
||||
})
|
||||
}
|
||||
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, message: string | File, call: Call) {
|
||||
export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, message: string | File, call: Call, expectedEpoch?: string) {
|
||||
return update(identity, async (state, save, factory) => {
|
||||
const text = typeof message === 'string' ? message.trim() : ''
|
||||
let content: Uint8Array = encoder.encode(text)
|
||||
@@ -248,6 +248,7 @@ export function sendGroupMessage(identity: ChatIdentity, group: GroupDetail, mes
|
||||
const epochs = await call<Epoch[]>(path(group) + '/epochs')
|
||||
const epoch = epochs.find((item) => item.current && item.membershipVersion === group.membershipVersion)
|
||||
if (!epoch || !matchesEpoch(chat.epochs[epoch.id], epoch, group)) throw new Error('群成员或设备已变更,请先建立新世代')
|
||||
if (expectedEpoch && epoch.id !== expectedEpoch) throw new Error('群加密会话已变更,请重新录音')
|
||||
if (chat.pendingMessage) throw new Error('请先刷新以重试待发消息')
|
||||
const id = crypto.randomUUID()
|
||||
const own = chat.epochs[epoch.id]!.roster[identity.device.id]
|
||||
|
||||
Reference in new issue
Block a user