88 lines
3.8 KiB
Vue
88 lines
3.8 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref, watch } from 'vue'
|
|
import { useSession } from '../session'
|
|
import { directory } from './chat'
|
|
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 emit = defineEmits<{ cancel: []; confirm: [destination: ForwardDestination] }>()
|
|
const session = useSession()
|
|
const groups = ref<GroupSummary[]>([])
|
|
const contacts = ref<Contact[]>([])
|
|
const destination = ref('')
|
|
const devices = ref<Peer[]>([])
|
|
const deviceId = ref('')
|
|
const loading = ref(true)
|
|
const error = ref('')
|
|
const isContact = computed(() => destination.value.startsWith('user:'))
|
|
const selectedDevice = computed(() => devices.value.find((item) => item.id === deviceId.value && props.pinned[item.id] === item.publicKey))
|
|
const target = computed<ForwardDestination | undefined>(() => {
|
|
const id = destination.value.split(':')[1]
|
|
if (!id) return
|
|
if (isContact.value && selectedDevice.value) return { user: id, device: selectedDevice.value }
|
|
if (groups.value.some((group) => group.id === id && destination.value === 'group:' + id)) return { group: id }
|
|
})
|
|
|
|
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)
|
|
contacts.value = allContacts.filter((contact) => contact.state === 'friend')
|
|
} catch (cause) {
|
|
error.value = cause instanceof Error ? cause.message : '无法读取转发目标'
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
})
|
|
|
|
watch(destination, async (value, _previous, onCleanup) => {
|
|
devices.value = []
|
|
deviceId.value = ''
|
|
error.value = ''
|
|
if (!value.startsWith('user:')) return
|
|
let current = true
|
|
onCleanup(() => { current = false })
|
|
try {
|
|
const candidates = await directory(session.authorized, value.slice('user:'.length))
|
|
if (!current) return
|
|
devices.value = candidates.filter((item) => item.mls && props.pinned[item.id] === item.publicKey)
|
|
deviceId.value = devices.value[0]?.id ?? ''
|
|
if (!devices.value.length) error.value = '请先在私聊中核验该联系人的设备'
|
|
} catch (cause) {
|
|
if (current) error.value = cause instanceof Error ? cause.message : '无法读取联系人设备'
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<form class="confirmation group-forward" aria-label="转发群消息" @submit.prevent="target && emit('confirm', target)">
|
|
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
|
<label>转发给
|
|
<select v-model="destination" :disabled="busy || loading">
|
|
<option value="">选择群聊或联系人</option>
|
|
<optgroup v-if="groups.length" label="群聊">
|
|
<option v-for="group in groups" :key="group.id" :value="'group:' + group.id">{{ group.name }}</option>
|
|
</optgroup>
|
|
<optgroup v-if="contacts.length" label="联系人">
|
|
<option v-for="contact in contacts" :key="contact.id" :value="'user:' + contact.id">{{ contact.alias || contact.username }}</option>
|
|
</optgroup>
|
|
</select>
|
|
</label>
|
|
<label v-if="devices.length">目标设备
|
|
<select v-model="deviceId" :disabled="busy">
|
|
<option v-for="item in devices" :key="item.id" :value="item.id">{{ item.name }} · {{ item.id.slice(0, 8) }}</option>
|
|
</select>
|
|
</label>
|
|
<div class="message-actions">
|
|
<button type="submit" class="primary" :disabled="busy || !target">确认转发</button>
|
|
<button type="button" class="plain" :disabled="busy" @click="emit('cancel')">取消</button>
|
|
</div>
|
|
</form>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.group-forward { display: grid; gap: .65rem; }
|
|
</style>
|