接入私聊消息通知和联系人免打扰
This commit is contained in:
1 parent
5c882ac855
commit
7e165ba9b4
23 files changed
+286
-97
No files matched your search
+29
-19
@@ -9,7 +9,8 @@ const router = useRouter()
|
||||
const error = ref('')
|
||||
const busy = ref(false)
|
||||
const browser = window.xhchat?.openBrowser
|
||||
const notice = ref<string>()
|
||||
type NoticeKind = 'group' | 'private'
|
||||
const notice = ref<{ href: string; label: string }>()
|
||||
let noticeTimer: ReturnType<typeof setTimeout> | undefined
|
||||
const systemNotices = ref(false)
|
||||
const noticeSettingStatus = ref('')
|
||||
@@ -23,7 +24,7 @@ function noticeSettingKey() {
|
||||
function closeSystemNotice() {
|
||||
webNotice?.close()
|
||||
webNotice = undefined
|
||||
void window.xhchat?.closeGroupNotification().catch(() => {})
|
||||
void window.xhchat?.closeMessageNotification().catch(() => {})
|
||||
}
|
||||
|
||||
function readNoticeSetting() {
|
||||
@@ -41,7 +42,7 @@ async function toggleSystemNotices() {
|
||||
const enabled = !systemNotices.value
|
||||
if (enabled) {
|
||||
if (window.xhchat) {
|
||||
if (!await window.xhchat.groupNotificationsSupported()) throw new Error('当前系统不支持桌面通知')
|
||||
if (!await window.xhchat.messageNotificationsSupported()) throw new Error('当前系统不支持桌面通知')
|
||||
} else {
|
||||
if (!('Notification' in window)) throw new Error('当前浏览器不支持系统通知')
|
||||
if (await Notification.requestPermission() !== 'granted') throw new Error('请在浏览器站点设置中允许通知')
|
||||
@@ -51,13 +52,13 @@ async function toggleSystemNotices() {
|
||||
localStorage.setItem(noticeSettingKey(), enabled ? 'enabled' : 'disabled')
|
||||
systemNotices.value = enabled
|
||||
if (!enabled) closeSystemNotice()
|
||||
noticeSettingStatus.value = enabled ? '已在本设备启用群消息系统通知' : '已在本设备关闭群消息系统通知'
|
||||
noticeSettingStatus.value = enabled ? '已在本设备启用消息系统通知' : '已在本设备关闭消息系统通知'
|
||||
}
|
||||
|
||||
function showSystemNotice(group: string) {
|
||||
function showSystemNotice(kind: NoticeKind, href: string, label: string) {
|
||||
if (!systemNotices.value) return
|
||||
if (window.xhchat) {
|
||||
void window.xhchat.showGroupNotification().catch(() => {})
|
||||
void window.xhchat.showMessageNotification(kind).catch(() => {})
|
||||
return
|
||||
}
|
||||
if (!('Notification' in window) || Notification.permission !== 'granted' ||
|
||||
@@ -65,11 +66,11 @@ function showSystemNotice(group: string) {
|
||||
lastSystemNotice = Date.now()
|
||||
try {
|
||||
webNotice?.close()
|
||||
webNotice = new Notification('XHChat', { body: '有新的群消息', icon: '/brand/xhchat.png', tag: 'xhchat-group' })
|
||||
webNotice = new Notification('XHChat', { body: label, icon: '/brand/xhchat.png', tag: 'xhchat-' + kind })
|
||||
webNotice.onclick = () => {
|
||||
if (!session.identity) return
|
||||
window.focus()
|
||||
void router.push('/groups/' + group)
|
||||
void router.push(href)
|
||||
closeSystemNotice()
|
||||
}
|
||||
} catch {
|
||||
@@ -82,15 +83,24 @@ function dismissNotice() {
|
||||
notice.value = undefined
|
||||
}
|
||||
|
||||
watch(() => session.groupChange, (event) => {
|
||||
if (!event?.notify) return
|
||||
showSystemNotice(event.groupId)
|
||||
if (route.params.id === event.groupId) return
|
||||
function showNotice(kind: NoticeKind, id: string) {
|
||||
if (!/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/.test(id)) return
|
||||
const href = (kind === 'group' ? '/groups/' : '/chat/') + id
|
||||
const label = kind === 'group' ? '有新的群消息' : '有新的私聊消息'
|
||||
showSystemNotice(kind, href, label)
|
||||
if (route.path === href) return
|
||||
dismissNotice()
|
||||
notice.value = event.groupId
|
||||
notice.value = { href, label }
|
||||
noticeTimer = setTimeout(dismissNotice, 8000)
|
||||
}
|
||||
|
||||
watch(() => session.groupChange, (event) => {
|
||||
if (event?.notify) showNotice('group', event.groupId)
|
||||
})
|
||||
watch(() => route.params.id, (id) => { if (id === notice.value) dismissNotice() })
|
||||
watch(() => session.privateNotice, (event) => {
|
||||
if (event) showNotice('private', event.contactId)
|
||||
})
|
||||
watch(() => route.path, (path) => { if (path === notice.value?.href) dismissNotice() })
|
||||
onMounted(() => {
|
||||
readNoticeSetting()
|
||||
window.addEventListener('storage', storageChanged)
|
||||
@@ -138,21 +148,21 @@ async function act(action: () => Promise<void>) {
|
||||
<div class="account-footer">
|
||||
<p aria-live="polite"><span class="dot" :class="{ online: session.connection === '实时连接已建立' }"></span>{{ session.connection }}</p>
|
||||
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||||
<button class="plain" type="button" :aria-pressed="systemNotices" :disabled="busy" @click="act(toggleSystemNotices)">群消息系统通知</button>
|
||||
<button class="plain" type="button" :aria-pressed="systemNotices" :disabled="busy" @click="act(toggleSystemNotices)">消息系统通知</button>
|
||||
<small v-if="noticeSettingStatus" role="status">{{ noticeSettingStatus }}</small>
|
||||
<button class="plain" :disabled="busy" @click="act(session.logout)">退出登录</button>
|
||||
</div>
|
||||
</aside>
|
||||
<RouterView />
|
||||
<div v-if="notice" class="group-notice" role="status">
|
||||
<RouterLink :to="'/groups/' + notice" @click="dismissNotice">有新的群消息</RouterLink>
|
||||
<button type="button" class="plain" aria-label="关闭群消息提醒" @click="dismissNotice">关闭</button>
|
||||
<div v-if="notice" class="message-notice" role="status">
|
||||
<RouterLink :to="notice.href" @click="dismissNotice">{{ notice.label }}</RouterLink>
|
||||
<button type="button" class="plain" aria-label="关闭消息提醒" @click="dismissNotice">关闭</button>
|
||||
</div>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.group-notice {
|
||||
.message-notice {
|
||||
position: fixed; inset-inline-end: 1rem; inset-block-end: 1rem; z-index: 6;
|
||||
display: flex; align-items: center; gap: .7rem; padding: .6rem .85rem;
|
||||
background: var(--surface); border: 1px solid var(--line); border-radius: .5rem;
|
||||
|
||||
Reference in new issue
Block a user