feat: 房间创建者管理权限——锁定房间、踢人、结束投票

This commit is contained in:
2026-02-24 21:01:24 +08:00
parent fc0a2a018b
commit 07ffe42176
11 changed files with 507 additions and 9 deletions
+20
View File
@@ -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: "加入房间失败" },
+106
View File
@@ -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 },
);
}
}
+4 -1
View File
@@ -96,6 +96,7 @@ function filterByPrice(
export async function POST(req: Request) {
let restaurants: Restaurant[] = fallbackRestaurants;
let creatorId = "";
try {
const body = await req.json();
@@ -105,7 +106,9 @@ export async function POST(req: Request) {
radius = 3000,
priceRange = "any",
cuisine = "不限",
userId = "",
} = body;
creatorId = userId;
if (lat && lng) {
const apiKey = process.env.AMAP_API_KEY;
@@ -142,7 +145,7 @@ export async function POST(req: Request) {
}
try {
const roomId = await createRoom(restaurants);
const roomId = await createRoom(restaurants, creatorId);
return NextResponse.json({ roomId, restaurants });
} catch (e) {
console.error("Failed to create room:", e);
+1 -1
View File
@@ -150,7 +150,7 @@ export default function LandingPage() {
const res = await fetch("/api/room/create", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...coords, radius, priceRange, cuisine }),
body: JSON.stringify({ ...coords, radius, priceRange, cuisine, userId: getUserId() }),
});
if (!res.ok) {
+13 -2
View File
@@ -20,7 +20,8 @@ export default function RoomPage() {
const leavingRef = useRef(false);
const {
userCount, match, matchType, matchLikes, runnerUps, likeCounts, swipeCounts, restaurants, notFound, mutate,
userCount, match, matchType, matchLikes, runnerUps, likeCounts, swipeCounts,
restaurants, notFound, mutate, creatorId, locked, users,
} = useRoomPolling(roomId);
useEffect(() => {
@@ -117,7 +118,17 @@ export default function RoomPage() {
return (
<div className="flex h-dvh flex-col bg-background">
<TopNav roomId={roomId} userCount={userCount} onExit={handleExitRequest} />
<TopNav
roomId={roomId}
userCount={userCount}
onExit={handleExitRequest}
isCreator={userId === creatorId}
userId={userId}
users={users}
locked={locked}
swipeCounts={swipeCounts}
totalCards={restaurants.length}
/>
<SwipeDeck
restaurants={restaurants}
roomId={roomId}