added watchparties

This commit is contained in:
2026-01-04 16:49:59 +01:00
parent cde09c6ffa
commit d9c1ba3d27
48 changed files with 7585 additions and 167 deletions

View File

@@ -0,0 +1,141 @@
import { FastifyReply, FastifyRequest } from 'fastify';
import * as roomService from './rooms.service';
import { getUserById } from '../user/user.service';
interface CreateRoomBody {
name: string;
password?: string;
}
interface JoinRoomBody {
password?: string;
guestName?: string;
}
export async function createRoom(req: any, reply: FastifyReply) {
try {
const { name, password } = req.body as CreateRoomBody;
const userId = req.user?.id;
if (!userId) {
return reply.code(401).send({ error: "Authentication required to create room" });
}
if (!name || name.trim().length === 0) {
return reply.code(400).send({ error: "Room name is required" });
}
const user = await getUserById(userId);
if (!user) {
return reply.code(404).send({ error: "User not found" });
}
const host = {
id: `user_${userId}`,
username: user.username,
avatar: user.profile_picture_url || undefined,
isHost: true,
isGuest: false,
userId
};
const room = roomService.createRoom(name, host, password);
return reply.send({
success: true,
room: {
id: room.id,
name: room.name,
hasPassword: !!room.password,
userCount: room.users.size
}
});
} catch (err) {
console.error("Create Room Error:", err);
return reply.code(500).send({ error: "Failed to create room" });
}
}
export async function getRooms(req: FastifyRequest, reply: FastifyReply) {
try {
const rooms = roomService.getAllRooms();
const roomList = rooms.map((room) => ({
id: room.id,
name: room.name,
host: room.host.username,
userCount: room.users.size,
hasPassword: !!room.password,
currentlyWatching: room.currentVideo ? {
animeId: room.currentVideo.animeId,
episode: room.currentVideo.episode
} : null
}));
return reply.send({ rooms: roomList });
} catch (err) {
console.error("Get Rooms Error:", err);
return reply.code(500).send({ error: "Failed to retrieve rooms" });
}
}
export async function getRoom(req: FastifyRequest, reply: FastifyReply) {
try {
const { id } = req.params as { id: string };
const room = roomService.getRoom(id);
if (!room) {
return reply.code(404).send({ error: "Room not found" });
}
return reply.send({
room: {
id: room.id,
name: room.name,
host: {
username: room.host.username,
avatar: room.host.avatar
},
users: Array.from(room.users.values()).map(u => ({
id: u.id,
username: u.username,
avatar: u.avatar,
isHost: u.isHost,
isGuest: u.isGuest
})),
hasPassword: !!room.password,
currentVideo: room.currentVideo
}
});
} catch (err) {
console.error("Get Room Error:", err);
return reply.code(500).send({ error: "Failed to retrieve room" });
}
}
export async function deleteRoom(req: any, reply: FastifyReply) {
try {
const { id } = req.params as { id: string };
const userId = req.user?.id;
if (!userId) {
return reply.code(401).send({ error: "Authentication required" });
}
const room = roomService.getRoom(id);
if (!room) {
return reply.code(404).send({ error: "Room not found" });
}
if (room.host.userId !== userId) {
return reply.code(403).send({ error: "Only the host can delete the room" });
}
roomService.deleteRoom(id);
return reply.send({ success: true });
} catch (err) {
console.error("Delete Room Error:", err);
return reply.code(500).send({ error: "Failed to delete room" });
}
}