feat: 房间创建者管理权限——锁定房间、踢人、结束投票
This commit is contained in:
@@ -15,6 +15,12 @@ export async function POST(
|
||||
}
|
||||
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
if (data.kickedUsers.includes(userId)) {
|
||||
throw new Error("KICKED");
|
||||
}
|
||||
if (data.locked && !data.users.includes(userId)) {
|
||||
throw new Error("LOCKED");
|
||||
}
|
||||
if (!data.users.includes(userId)) {
|
||||
data.users.push(userId);
|
||||
}
|
||||
@@ -35,6 +41,20 @@ export async function POST(
|
||||
userCount: updated.users.length,
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
if (e.message === "LOCKED") {
|
||||
return NextResponse.json(
|
||||
{ error: "房间已锁定,无法加入" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
if (e.message === "KICKED") {
|
||||
return NextResponse.json(
|
||||
{ error: "你已被移出该房间" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
}
|
||||
console.error("Failed to join room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "加入房间失败" },
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { atomicUpdateRoom } from "@/lib/store";
|
||||
import { notify } from "@/lib/roomEvents";
|
||||
|
||||
export async function POST(
|
||||
req: Request,
|
||||
{ params }: { params: Promise<{ id: string }> },
|
||||
) {
|
||||
const { id } = await params;
|
||||
|
||||
try {
|
||||
const { userId, action, targetUserId } = await req.json();
|
||||
if (!userId || !action) {
|
||||
return NextResponse.json(
|
||||
{ error: "userId and action required" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const updated = await atomicUpdateRoom(id, (data) => {
|
||||
if (data.creatorId !== userId) {
|
||||
throw new Error("FORBIDDEN");
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case "lock":
|
||||
data.locked = true;
|
||||
break;
|
||||
|
||||
case "unlock":
|
||||
data.locked = false;
|
||||
break;
|
||||
|
||||
case "kick":
|
||||
if (!targetUserId || targetUserId === userId) {
|
||||
throw new Error("INVALID_TARGET");
|
||||
}
|
||||
data.users = data.users.filter((u) => u !== targetUserId);
|
||||
if (!data.kickedUsers.includes(targetUserId)) {
|
||||
data.kickedUsers.push(targetUserId);
|
||||
}
|
||||
delete data.swipeCounts[targetUserId];
|
||||
for (const rid of Object.keys(data.likes)) {
|
||||
data.likes[rid] = data.likes[rid].filter(
|
||||
(u) => u !== targetUserId,
|
||||
);
|
||||
}
|
||||
if (
|
||||
data.match &&
|
||||
data.likes[data.match]?.length !== data.users.length
|
||||
) {
|
||||
data.match = null;
|
||||
}
|
||||
break;
|
||||
|
||||
case "end_voting":
|
||||
for (const u of data.users) {
|
||||
data.swipeCounts[u] = data.restaurants.length;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error("UNKNOWN_ACTION");
|
||||
}
|
||||
|
||||
return data;
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
return NextResponse.json(
|
||||
{ error: "房间不存在或已过期" },
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
notify(id);
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (e) {
|
||||
if (e instanceof Error) {
|
||||
if (e.message === "FORBIDDEN") {
|
||||
return NextResponse.json(
|
||||
{ error: "只有房主可以执行此操作" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
if (e.message === "INVALID_TARGET") {
|
||||
return NextResponse.json(
|
||||
{ error: "无效的操作对象" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
if (e.message === "UNKNOWN_ACTION") {
|
||||
return NextResponse.json(
|
||||
{ error: "未知操作" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
console.error("Failed to manage room:", e);
|
||||
return NextResponse.json(
|
||||
{ error: "操作失败" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user