a762a5bb4c
- Add prominent pending join request badge on GroupView header with pulse animation for admins/owners - Clicking badge smoothly scrolls to request list with highlight - Add "My Join Requests" section on Profile page - Show status (pending/approved/rejected), timestamp, and reject reason - Add API: getMyJoinRequests() to fetch user's full request history Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
306 lines
8.7 KiB
TypeScript
306 lines
8.7 KiB
TypeScript
// src/api/groups.ts
|
|
import pb from './pocketbase'
|
|
import type { Group, JoinRequest } from '@/types'
|
|
|
|
// 创建群组
|
|
export async function createGroup(data: {
|
|
name: string
|
|
description: string
|
|
maxMembers: number
|
|
}) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
return pb.collection('groups').create({
|
|
...data,
|
|
owner: user.id,
|
|
members: [user.id],
|
|
requireApproval: true
|
|
})
|
|
}
|
|
|
|
// 获取用户的群组列表
|
|
export async function getUserGroups(): Promise<Group[]> {
|
|
const user = pb.authStore.model
|
|
if (!user) return []
|
|
|
|
return pb.collection('groups').getList(1, 50, {
|
|
filter: `members ~ "${user.id}"`,
|
|
$autoCancel: false
|
|
}).then(res => res.items as unknown as Group[])
|
|
}
|
|
|
|
// 获取群组详情
|
|
export async function getGroup(groupId: string): Promise<Group> {
|
|
return pb.collection('groups').getOne(groupId, {
|
|
expand: 'members',
|
|
$autoCancel: false
|
|
}) as unknown as Group
|
|
}
|
|
|
|
// 按名称搜索群组
|
|
export async function searchGroups(keyword: string): Promise<Group[]> {
|
|
if (!keyword.trim()) return []
|
|
|
|
const user = pb.authStore.model
|
|
const filter = `name ~ "${keyword.trim()}" && id != "${user?.id}"`
|
|
|
|
const result = await pb.collection('groups').getList(1, 20, {
|
|
filter,
|
|
$autoCancel: false
|
|
})
|
|
return result.items as unknown as Group[]
|
|
}
|
|
|
|
// 直接加入群组(无需审核时调用)
|
|
export async function joinGroup(groupId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
const members = group.members as string[]
|
|
|
|
if (members.length >= group.maxMembers) {
|
|
throw new Error('群组已满')
|
|
}
|
|
|
|
if (members.includes(user.id)) {
|
|
throw new Error('已是群组成员')
|
|
}
|
|
|
|
return pb.collection('groups').update(groupId, {
|
|
members: [...members, user.id]
|
|
})
|
|
}
|
|
|
|
// 提交加入申请
|
|
export async function createJoinRequest(groupId: string): Promise<JoinRequest> {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
// 检查是否已有待审核的申请
|
|
const existing = await pb.collection('join_requests').getList(1, 1, {
|
|
filter: `group="${groupId}" && user="${user.id}" && status="pending"`
|
|
})
|
|
if (existing.items.length > 0) {
|
|
throw new Error('已提交过申请,请等待审核')
|
|
}
|
|
|
|
return pb.collection('join_requests').create({
|
|
group: groupId,
|
|
user: user.id,
|
|
status: 'pending'
|
|
}) as unknown as JoinRequest
|
|
}
|
|
|
|
// 获取群组的待审核申请(群主用)
|
|
export async function getGroupJoinRequests(groupId: string): Promise<JoinRequest[]> {
|
|
const result = await pb.collection('join_requests').getList(1, 50, {
|
|
filter: `group="${groupId}" && status="pending"`,
|
|
sort: '-created',
|
|
expand: 'user',
|
|
$autoCancel: false
|
|
})
|
|
return result.items as unknown as JoinRequest[]
|
|
}
|
|
|
|
// 获取我作为群主的所有群组的待审核申请
|
|
export async function getMyGroupsJoinRequests(): Promise<JoinRequest[]> {
|
|
const user = pb.authStore.model
|
|
if (!user) return []
|
|
|
|
const result = await pb.collection('join_requests').getList(1, 50, {
|
|
filter: `group.owner="${user.id}" && status="pending"`,
|
|
sort: '-created',
|
|
expand: 'user,group',
|
|
$autoCancel: false
|
|
})
|
|
return result.items as unknown as JoinRequest[]
|
|
}
|
|
|
|
// 获取当前用户的所有入群申请(包括所有状态)
|
|
export async function getMyJoinRequests(): Promise<JoinRequest[]> {
|
|
const user = pb.authStore.model
|
|
if (!user) return []
|
|
|
|
const result = await pb.collection('join_requests').getList(1, 50, {
|
|
filter: `user="${user.id}"`,
|
|
sort: '-created',
|
|
expand: 'group',
|
|
$autoCancel: false
|
|
})
|
|
return result.items as unknown as JoinRequest[]
|
|
}
|
|
|
|
// 审批加入申请
|
|
export async function respondJoinRequest(
|
|
requestId: string,
|
|
status: 'approved' | 'rejected',
|
|
rejectReason?: string
|
|
) {
|
|
const updateData: Record<string, unknown> = { status }
|
|
if (status === 'rejected' && rejectReason) {
|
|
updateData.rejectReason = rejectReason
|
|
}
|
|
|
|
const request = await pb.collection('join_requests').update(requestId, updateData) as any
|
|
|
|
// 如果同意,将用户加入群组
|
|
if (status === 'approved') {
|
|
const group = await pb.collection('groups').getOne(request.group) as any
|
|
const members: string[] = group.members || []
|
|
if (!members.includes(request.user)) {
|
|
members.push(request.user)
|
|
await pb.collection('groups').update(request.group, { members })
|
|
}
|
|
}
|
|
|
|
return request
|
|
}
|
|
|
|
// 更新群组审核设置
|
|
export async function updateGroupApproval(groupId: string, requireApproval: boolean) {
|
|
return pb.collection('groups').update(groupId, { requireApproval })
|
|
}
|
|
|
|
// 退出群组
|
|
export async function leaveGroup(groupId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
|
|
if (group.owner === user.id) {
|
|
throw new Error('群主不能退出群组,请先转让群主或解散群组')
|
|
}
|
|
|
|
const members = group.members as string[]
|
|
return pb.collection('groups').update(groupId, {
|
|
members: members.filter(id => id !== user.id)
|
|
})
|
|
}
|
|
|
|
// 解散群组
|
|
export async function dissolveGroup(groupId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
|
|
if (group.owner !== user.id) {
|
|
throw new Error('只有群主可以解散群组')
|
|
}
|
|
|
|
return pb.collection('groups').delete(groupId)
|
|
}
|
|
|
|
// 订阅群组变更
|
|
export function subscribeGroup(groupId: string, callback: (group: Group) => void) {
|
|
return pb.collection('groups').subscribe('*', (payload) => {
|
|
if (payload.record.id === groupId) {
|
|
callback(payload.record as unknown as Group)
|
|
}
|
|
})
|
|
}
|
|
|
|
// 转让群主
|
|
// 安全提示:PB groups updateRule 允许认证用户更新,前端校验是唯一防线。
|
|
// 如需服务端强制保护,需添加 PocketBase JS hooks 或改用更严格的 updateRule。
|
|
export async function transferGroupOwnership(groupId: string, newOwnerId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
if (group.owner !== user.id) {
|
|
throw new Error('只有群主可以转让群组')
|
|
}
|
|
|
|
const admins = (group.admins as string[]) || []
|
|
|
|
// 将原群主加入 admins,新群主从 admins 中移除
|
|
const newAdmins = [...new Set([...admins, user.id])].filter(id => id !== newOwnerId)
|
|
|
|
return pb.collection('groups').update(groupId, {
|
|
owner: newOwnerId,
|
|
admins: newAdmins
|
|
})
|
|
}
|
|
|
|
// 添加管理员
|
|
export async function addGroupAdmin(groupId: string, userId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
if (group.owner !== user.id) {
|
|
throw new Error('只有群主可以设置管理员')
|
|
}
|
|
|
|
const admins = (group.admins as string[]) || []
|
|
if (admins.includes(userId)) {
|
|
throw new Error('该用户已是管理员')
|
|
}
|
|
|
|
return pb.collection('groups').update(groupId, {
|
|
admins: [...admins, userId]
|
|
})
|
|
}
|
|
|
|
// 移除管理员
|
|
export async function removeGroupAdmin(groupId: string, userId: string) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
if (group.owner !== user.id) {
|
|
throw new Error('只有群主可以移除管理员')
|
|
}
|
|
|
|
const admins = (group.admins as string[]) || []
|
|
return pb.collection('groups').update(groupId, {
|
|
admins: admins.filter(id => id !== userId)
|
|
})
|
|
}
|
|
|
|
// 更新全员管理设置
|
|
export async function updateGroupMemberManage(groupId: string, allow: boolean) {
|
|
const user = pb.authStore.model
|
|
if (!user) throw new Error('未登录')
|
|
|
|
const group = await pb.collection('groups').getOne(groupId)
|
|
if (group.owner !== user.id) {
|
|
throw new Error('只有群主可以修改此设置')
|
|
}
|
|
|
|
return pb.collection('groups').update(groupId, { allowMemberManage: allow })
|
|
}
|
|
|
|
// 订阅加入申请变更
|
|
export function subscribeJoinRequests(groupId: string, callback: (request: JoinRequest) => void) {
|
|
return pb.collection('join_requests').subscribe('*', (payload) => {
|
|
const record = payload.record as any
|
|
if (record.group === groupId) {
|
|
callback(record as unknown as JoinRequest)
|
|
}
|
|
})
|
|
}
|
|
|
|
// 获取群组成员
|
|
export async function getGroupMembers(groupId: string) {
|
|
const group = await getGroup(groupId)
|
|
|
|
if (group.expand?.members) {
|
|
return group.expand.members
|
|
}
|
|
|
|
const members = group.members as string[]
|
|
if (!members || members.length === 0) return []
|
|
|
|
const users = await pb.collection('users').getList(1, 50, {
|
|
filter: members.map(id => `id="${id}"`).join(' || '),
|
|
$autoCancel: false
|
|
})
|
|
|
|
return users.items
|
|
}
|