支持本机删除群消息和打开群链接
This commit is contained in:
1 parent
5ec9c81f90
commit
fef421bd4b
7 files changed
+134
-40
No files matched your search
+36
-10
@@ -5,11 +5,12 @@ import { APIError } from '../api'
|
||||
import { downloadAttachment, maxAttachmentSize } from './attachments'
|
||||
import type { Attachment } from './attachments'
|
||||
import MediaPreview from './MediaPreview.vue'
|
||||
import MessageText from './MessageText.vue'
|
||||
import ForwardDialog from './ForwardDialog.vue'
|
||||
import VoiceMessage from './VoiceMessage.vue'
|
||||
import VoiceRecorder from './VoiceRecorder.vue'
|
||||
import { enableChat, trust } from './chat'
|
||||
import { discardPendingGroupMessage, forwardGroupMessage, groupAttachmentPath, groupQuote, publishGroup, sendGroupMessage, setGroupFavorite, synchronizeGroup } from './group-chat'
|
||||
import { discardPendingGroupMessage, forwardGroupMessage, groupAttachmentPath, groupQuote, publishGroup, removeLocalGroupMessage, sendGroupMessage, setGroupFavorite, synchronizeGroup } from './group-chat'
|
||||
import type { ForwardDestination, GroupDetail, GroupDevice, GroupLine } from './group-chat'
|
||||
|
||||
const props = defineProps<{ group: GroupDetail }>()
|
||||
@@ -35,6 +36,7 @@ const mediaLimit = ref(mediaPageSize)
|
||||
const replying = ref<GroupLine>()
|
||||
const editing = ref<GroupLine>()
|
||||
const recalling = ref<GroupLine>()
|
||||
const deleting = ref<GroupLine>()
|
||||
const forwarding = ref<GroupLine>()
|
||||
const forwardStatus = ref('')
|
||||
const files = ref<File[]>([])
|
||||
@@ -275,6 +277,22 @@ function favorite(line: GroupLine) {
|
||||
})
|
||||
}
|
||||
|
||||
function removeLocal() {
|
||||
const identity = session.identity
|
||||
const message = deleting.value
|
||||
if (!identity || !message) return
|
||||
const matches = (line?: GroupLine) => line?.id === message.id && line.epoch === message.epoch && line.sender === message.sender
|
||||
void run(async () => {
|
||||
const updated = await removeLocalGroupMessage(identity, props.group, message)
|
||||
if (view.value) view.value.lines = updated
|
||||
if (matches(replying.value)) replying.value = undefined
|
||||
if (matches(editing.value)) { editing.value = undefined; draft.value = '' }
|
||||
if (matches(recalling.value)) recalling.value = undefined
|
||||
if (matches(forwarding.value)) forwarding.value = undefined
|
||||
deleting.value = undefined
|
||||
})
|
||||
}
|
||||
|
||||
function beginForward(line: GroupLine) {
|
||||
forwardStatus.value = ''
|
||||
forwarding.value = line
|
||||
@@ -413,21 +431,29 @@ onBeforeUnmount(() => {
|
||||
<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>
|
||||
<p v-else><MessageText :text="line.text" @error="error = $event" /></p>
|
||||
<small v-if="line.edited && !line.recalled" class="muted">已编辑</small>
|
||||
<div v-if="!line.recalled" class="message-actions">
|
||||
<button type="button" class="plain" :disabled="busy" @click="favorite(line)">{{ line.favorite ? '取消收藏' : '收藏' }}</button>
|
||||
<button type="button" class="plain" :disabled="busy" @click="beginForward(line)">转发</button>
|
||||
<template v-if="currentEpoch && line.epoch === currentEpoch">
|
||||
<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>
|
||||
<div class="message-actions">
|
||||
<template v-if="!line.recalled">
|
||||
<button type="button" class="plain" :disabled="busy" @click="favorite(line)">{{ line.favorite ? '取消收藏' : '收藏' }}</button>
|
||||
<button type="button" class="plain" :disabled="busy" @click="beginForward(line)">转发</button>
|
||||
<template v-if="currentEpoch && line.epoch === currentEpoch">
|
||||
<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>
|
||||
</template>
|
||||
</template>
|
||||
<button type="button" class="plain" :disabled="busy" @click="deleting = line">从本机删除</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
<div v-if="deleting" class="confirmation">
|
||||
<p>从本机群历史删除这条消息和它的收藏?</p>
|
||||
<button type="button" class="plain" :disabled="busy" @click="removeLocal">确认删除群消息</button>
|
||||
<button type="button" class="plain" @click="deleting = undefined">取消</button>
|
||||
</div>
|
||||
<button v-if="visibleLines.length < lines.length" type="button" class="plain" @click="mediaLimit += mediaPageSize">加载更多本机附件</button>
|
||||
<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" />
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
|
||||
const props = defineProps<{ text: string }>()
|
||||
const emit = defineEmits<{ error: [message: string] }>()
|
||||
const internalBrowser = window.xhchat?.openBrowser
|
||||
|
||||
const parts = computed(() => {
|
||||
const text = props.text
|
||||
const segments: { text: string; url?: string }[] = []
|
||||
let offset = 0
|
||||
for (const match of text.matchAll(/https?:\/\/[^\s<>"']+/giu)) {
|
||||
const label = match[0].replace(/[.,!?;:,。!?;:、))\]】]+$/u, '')
|
||||
if (label.length > 4096) continue
|
||||
try {
|
||||
const url = new URL(label)
|
||||
if (url.username || url.password ||
|
||||
(url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname))) continue
|
||||
if (match.index > offset) segments.push({ text: text.slice(offset, match.index) })
|
||||
segments.push({ text: label, url: url.href })
|
||||
offset = match.index + label.length
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (offset < text.length) segments.push({ text: text.slice(offset) })
|
||||
return segments
|
||||
})
|
||||
|
||||
async function open(url: string) {
|
||||
try {
|
||||
await internalBrowser?.(url)
|
||||
} catch (cause) {
|
||||
emit('error', cause instanceof Error ? cause.message : '无法打开网页链接')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<span><template v-for="(part, index) in parts" :key="index"><button v-if="part.url && internalBrowser"
|
||||
type="button" class="chat-link" @click="open(part.url)">{{ part.text }}</button><a v-else-if="part.url"
|
||||
class="chat-link" :href="part.url" target="_blank" rel="noopener noreferrer">{{ part.text }}</a><template v-else>{{ part.text }}</template></template></span>
|
||||
</template>
|
||||
@@ -83,6 +83,20 @@ export function setGroupFavorite(identity: ChatIdentity, group: GroupDetail, mes
|
||||
})
|
||||
}
|
||||
|
||||
export function removeLocalGroupMessage(identity: ChatIdentity, group: GroupDetail, message: LineRef) {
|
||||
return update(identity, async (state, save) => {
|
||||
const chat = state.groupChats?.[group.id]
|
||||
if (!chat) return []
|
||||
const index = chat.lines.findIndex((line) => line.id === message.id && line.epoch === message.epoch && line.sender === message.sender)
|
||||
if (index !== -1) {
|
||||
chat.lines.splice(index, 1)
|
||||
await save(state)
|
||||
}
|
||||
// The cursor remains advanced so the removed message is not fetched again.
|
||||
return chat.lines
|
||||
})
|
||||
}
|
||||
|
||||
export function groupQuote(line: GroupLine) {
|
||||
return (line.text || line.attachment?.name || '').slice(0, 240)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import type { Attachment } from '../chat/attachments'
|
||||
import VoiceRecorder from '../chat/VoiceRecorder.vue'
|
||||
import VoiceMessage from '../chat/VoiceMessage.vue'
|
||||
import MediaPreview from '../chat/MediaPreview.vue'
|
||||
import MessageText from '../chat/MessageText.vue'
|
||||
|
||||
const session = useSession()
|
||||
const route = useRoute()
|
||||
@@ -54,31 +55,6 @@ const lines = computed(() => {
|
||||
(!favoritesOnly.value || line.favorite) &&
|
||||
(!query || [line.text, line.attachment?.name, line.quote].some((part) => part?.toLowerCase().includes(query))))
|
||||
})
|
||||
const internalBrowser = window.xhchat?.openBrowser
|
||||
|
||||
function messageParts(text: string) {
|
||||
const parts: { text: string; url?: string }[] = []
|
||||
let offset = 0
|
||||
for (const match of text.matchAll(/https?:\/\/[^\s<>"']+/giu)) {
|
||||
const label = match[0].replace(/[.,!?;:,。!?;:、))\]】]+$/u, '')
|
||||
try {
|
||||
const url = new URL(label)
|
||||
if (url.username || url.password || label.length > 4096 ||
|
||||
(url.protocol !== 'https:' && !['127.0.0.1', 'localhost', '[::1]'].includes(url.hostname))) continue
|
||||
if (match.index > offset) parts.push({ text: text.slice(offset, match.index) })
|
||||
parts.push({ text: label, url: url.href })
|
||||
offset = match.index + label.length
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (offset < text.length) parts.push({ text: text.slice(offset) })
|
||||
return parts
|
||||
}
|
||||
|
||||
function openLink(url: string) {
|
||||
if (internalBrowser) void execute(() => internalBrowser(url))
|
||||
}
|
||||
let timer: ReturnType<typeof setInterval>
|
||||
let receiptTimer: ReturnType<typeof setTimeout> | undefined
|
||||
|
||||
@@ -365,9 +341,7 @@ onBeforeUnmount(() => {
|
||||
<button type="button" class="plain" :disabled="busy"
|
||||
@click="saveAttachment(line.attachment)">下载 {{ line.attachment.name }}</button>
|
||||
</template>
|
||||
<span v-else><template v-for="(part, index) in messageParts(line.text)" :key="index"><button v-if="part.url && internalBrowser"
|
||||
type="button" class="chat-link" @click="openLink(part.url)">{{ part.text }}</button><a v-else-if="part.url"
|
||||
class="chat-link" :href="part.url" target="_blank" rel="noopener noreferrer">{{ part.text }}</a><template v-else>{{ part.text }}</template></template></span>
|
||||
<MessageText v-else :text="line.text" @error="error = $event" />
|
||||
<small v-if="view?.unsent.includes(line.id)">待发送</small>
|
||||
<small v-else-if="line.own && line.read && !line.recalled">已读</small>
|
||||
<small v-if="line.edited && !line.recalled">已编辑</small>
|
||||
|
||||
Reference in new issue
Block a user