增加群组创建、邀请与成员权限管理
This commit is contained in:
1 parent
b1f55df263
commit
99a741e6da
13 files changed
+620
-5
No files matched your search
@@ -0,0 +1,167 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { useSession } from '../session'
|
||||
import type { Contact } from '../contacts'
|
||||
|
||||
type Group = { id: string; name: string; state: 'active' | 'invited'; role: 'owner' | 'admin' | 'member'; membershipVersion: string }
|
||||
type Detail = Group & { members: { id: string; username: string; state: Group['state']; role: Group['role'] }[] }
|
||||
|
||||
const session = useSession()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const groups = ref<Group[]>([])
|
||||
const contacts = ref<Contact[]>([])
|
||||
const detail = ref<Detail>()
|
||||
const term = ref('')
|
||||
const name = ref('')
|
||||
const nextName = ref('')
|
||||
const invite = ref('')
|
||||
const busy = ref(false)
|
||||
const error = ref('')
|
||||
const confirmation = ref<{ action: string; target?: string; label: string }>()
|
||||
const id = computed(() => typeof route.params.id === 'string' ? route.params.id : '')
|
||||
const visible = computed(() => groups.value.filter((group) => group.name.toLowerCase().includes(term.value.trim().toLowerCase())))
|
||||
const manage = computed(() => detail.value?.role === 'owner' || detail.value?.role === 'admin')
|
||||
const eligible = computed(() => contacts.value.filter((item) => item.state === 'friend' &&
|
||||
!detail.value?.members.some((member) => member.id === item.id)))
|
||||
const roles = { owner: '群主', admin: '管理员', member: '成员' }
|
||||
let timer: ReturnType<typeof setInterval>
|
||||
let revision = 0
|
||||
|
||||
async function refresh() {
|
||||
const version = ++revision
|
||||
const selected = id.value
|
||||
const list = await session.authorized<Group[]>('/groups')
|
||||
const next = list.some((group) => group.id === selected)
|
||||
? await session.authorized<Detail>('/groups/' + encodeURIComponent(selected))
|
||||
: undefined
|
||||
const friends = next?.role === 'owner' || next?.role === 'admin' ? await session.contacts() : []
|
||||
if (revision !== version || id.value !== selected) return
|
||||
groups.value = list
|
||||
if (detail.value?.id !== next?.id || detail.value?.name !== next?.name) nextName.value = next?.name ?? ''
|
||||
detail.value = next
|
||||
contacts.value = friends
|
||||
}
|
||||
|
||||
async function run(action: () => Promise<void>) {
|
||||
if (busy.value) return
|
||||
busy.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await action()
|
||||
} catch (cause) {
|
||||
error.value = cause instanceof Error ? cause.message : '群操作失败'
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function reload() {
|
||||
if (!busy.value) void run(refresh)
|
||||
}
|
||||
|
||||
function create() {
|
||||
void run(async () => {
|
||||
const created = await session.authorized<Group>('/groups', 'POST', { name: name.value })
|
||||
name.value = ''
|
||||
await router.push('/groups/' + created.id)
|
||||
await refresh()
|
||||
})
|
||||
}
|
||||
|
||||
function change(action: string, target?: string, value?: string) {
|
||||
const selected = detail.value?.id
|
||||
if (!selected) return
|
||||
void run(async () => {
|
||||
await session.authorized('/groups/' + selected, 'POST', { action, target, name: value })
|
||||
confirmation.value = undefined
|
||||
if (action === 'invite') invite.value = ''
|
||||
await refresh()
|
||||
})
|
||||
}
|
||||
|
||||
watch(id, () => {
|
||||
detail.value = undefined
|
||||
confirmation.value = undefined
|
||||
invite.value = ''
|
||||
if (busy.value) void refresh().catch(() => {})
|
||||
else reload()
|
||||
})
|
||||
onMounted(() => {
|
||||
reload()
|
||||
timer = setInterval(reload, 5000)
|
||||
})
|
||||
onBeforeUnmount(() => clearInterval(timer))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="device-page">
|
||||
<header class="page-header"><h1>群组</h1><button class="plain" :disabled="busy" @click="reload">刷新</button></header>
|
||||
<div class="conversation-layout">
|
||||
<aside class="conversation-list" aria-label="群组列表">
|
||||
<form class="contact-search" @submit.prevent="create">
|
||||
<label>群名称<input v-model="name" required maxlength="64"></label>
|
||||
<button class="plain" :disabled="busy || !name.trim()">创建群组</button>
|
||||
</form>
|
||||
<label>查找群组<input v-model="term" type="search" maxlength="64"></label>
|
||||
<RouterLink v-for="group in visible" :key="group.id" class="contact-item" :to="'/groups/' + group.id">
|
||||
<span><strong>{{ group.name }}</strong><small>{{ group.state === 'invited' ? '待接受邀请' : roles[group.role] }}</small></span>
|
||||
</RouterLink>
|
||||
</aside>
|
||||
<div class="device-content chat-page">
|
||||
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||||
<template v-if="detail">
|
||||
<h2>{{ detail.name }}</h2>
|
||||
<template v-if="detail.state === 'invited'">
|
||||
<p>你收到一个群组邀请。</p>
|
||||
<div class="contact-actions">
|
||||
<button class="primary" :disabled="busy" @click="change('accept')">接受邀请</button>
|
||||
<button class="plain" :disabled="busy" @click="change('decline')">拒绝邀请</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else>
|
||||
<p class="muted">群消息尚未开放。</p>
|
||||
<form v-if="manage" class="contact-preferences" @submit.prevent="change('rename', undefined, nextName)">
|
||||
<label>修改群名称<input v-model="nextName" required maxlength="64"></label>
|
||||
<button class="plain" :disabled="busy || !nextName.trim() || nextName.trim() === detail.name">保存群名称</button>
|
||||
</form>
|
||||
<form v-if="manage" class="contact-preferences" @submit.prevent="change('invite', invite)">
|
||||
<label>邀请联系人
|
||||
<select v-model="invite" required>
|
||||
<option value="">选择联系人</option>
|
||||
<option v-for="item in eligible" :key="item.id" :value="item.id">{{ item.alias || item.username }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<button class="plain" :disabled="busy || !invite">发送邀请</button>
|
||||
</form>
|
||||
<h3>群成员</h3>
|
||||
<ul class="group-members" aria-label="群成员">
|
||||
<li v-for="member in detail.members" :key="member.id">
|
||||
<span>{{ member.username }} · {{ member.state === 'invited' ? '待接受邀请' : roles[member.role] }}</span>
|
||||
<div class="message-actions" v-if="manage && member.role !== 'owner' && member.id !== session.identity?.user.id">
|
||||
<button v-if="detail.role === 'owner' && member.state === 'active'" class="plain" :disabled="busy"
|
||||
@click="change(member.role === 'admin' ? 'demote' : 'promote', member.id)">
|
||||
{{ member.role === 'admin' ? '取消管理员' : '设为管理员' }}
|
||||
</button>
|
||||
<button v-if="detail.role === 'owner' || member.role === 'member'" class="plain" :disabled="busy"
|
||||
@click="confirmation = { action: 'remove', target: member.id, label: member.state === 'invited' ? '撤销此邀请' : '移除此成员' }">
|
||||
{{ member.state === 'invited' ? '撤销邀请' : '移除成员' }}
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
<button v-if="detail.role === 'owner'" class="plain" :disabled="busy" @click="confirmation = { action: 'disband', label: '解散群组' }">解散群组</button>
|
||||
<button v-else class="plain" :disabled="busy" @click="confirmation = { action: 'leave', label: '退出群组' }">退出群组</button>
|
||||
<div v-if="confirmation" class="confirmation">
|
||||
<p>确认{{ confirmation.label }}?</p>
|
||||
<button class="plain" :disabled="busy" @click="change(confirmation.action, confirmation.target)">确认操作</button>
|
||||
<button class="plain" @click="confirmation = undefined">取消</button>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
<p v-else-if="!error" class="muted">选择或创建群组。</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
@@ -37,6 +37,7 @@ async function act(action: () => Promise<void>) {
|
||||
<nav class="panel-links" aria-label="工作区">
|
||||
<RouterLink to="/chat">消息</RouterLink>
|
||||
<RouterLink to="/contacts">联系人</RouterLink>
|
||||
<RouterLink to="/groups">群组</RouterLink>
|
||||
<RouterLink to="/devices">设备管理</RouterLink>
|
||||
<button v-if="browser" class="plain mobile-browser" @click="act(browser)">浏览器</button>
|
||||
</nav>
|
||||
|
||||
Reference in new issue
Block a user