Add shared YouTube playlist resizing
This commit is contained in:
@@ -38,6 +38,26 @@ function normalizeName(name) {
|
||||
return normalized.slice(0, 48) || "Guest";
|
||||
}
|
||||
|
||||
function normalizePlaylist(playlist) {
|
||||
if (!Array.isArray(playlist)) return [];
|
||||
|
||||
const seen = new Set();
|
||||
return playlist
|
||||
.map((item) => ({
|
||||
videoId: String(item?.videoId || "").trim(),
|
||||
title: String(item?.title || "").trim()
|
||||
}))
|
||||
.filter((item) => {
|
||||
if (!/^[a-zA-Z0-9_-]{11}$/.test(item.videoId) || seen.has(item.videoId)) {
|
||||
return false;
|
||||
}
|
||||
seen.add(item.videoId);
|
||||
item.title ||= `YouTube video ${item.videoId}`;
|
||||
return true;
|
||||
})
|
||||
.slice(0, 50);
|
||||
}
|
||||
|
||||
function broadcast(roomId, senderId, event) {
|
||||
const room = rooms.get(roomId);
|
||||
if (!room) return;
|
||||
@@ -157,16 +177,19 @@ const server = http.createServer(async (req, res) => {
|
||||
const roomId = url.pathname.split("/").at(-1) || defaultRoomId;
|
||||
const payload = await parseJson(req);
|
||||
|
||||
if (!payload.from || payload.type !== "watch-state" || !payload.videoId) {
|
||||
sendJson(res, 400, { error: "Watch messages require from, type=watch-state, and videoId." });
|
||||
if (!payload.from || payload.type !== "watch-state") {
|
||||
sendJson(res, 400, { error: "Watch messages require from and type=watch-state." });
|
||||
return;
|
||||
}
|
||||
|
||||
const playlist = normalizePlaylist(payload.playlist);
|
||||
const videoId = String(payload.videoId || "").trim();
|
||||
const watchState = {
|
||||
type: "watch-state",
|
||||
from: payload.from,
|
||||
name: normalizeName(payload.name),
|
||||
videoId: String(payload.videoId).trim(),
|
||||
videoId: playlist.some((item) => item.videoId === videoId) ? videoId : "",
|
||||
playlist,
|
||||
position: Number(payload.position) || 0,
|
||||
playing: Boolean(payload.playing),
|
||||
updatedAt: Number(payload.updatedAt) || Date.now()
|
||||
|
||||
Reference in New Issue
Block a user