From 58ced289d323a29331cf2e65d4450bd4dbb944ad Mon Sep 17 00:00:00 2001 From: NorkzYT Date: Mon, 23 Mar 2026 00:11:25 +0000 Subject: [PATCH] fix(playlists): prevent duplicate videos in the same playlist Check if the video already exists in the playlist before inserting. Uses the existing select_index() query. Returns 409 via the API and silently redirects back on the web UI. Applied to both the web route (playlist_ajax) and the API v1 route (POST /api/v1/auth/playlists/:plid/videos). --- src/invidious/routes/api/v1/authenticated.cr | 5 +++++ src/invidious/routes/playlists.cr | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/invidious/routes/api/v1/authenticated.cr b/src/invidious/routes/api/v1/authenticated.cr index a35d2f2b..c469eb6c 100644 --- a/src/invidious/routes/api/v1/authenticated.cr +++ b/src/invidious/routes/api/v1/authenticated.cr @@ -313,6 +313,11 @@ module Invidious::Routes::API::V1::Authenticated return error_json(403, "Invalid videoId") end + # Prevent duplicate videos in the same playlist + if Invidious::Database::PlaylistVideos.select_index(plid, video_id) + return error_json(409, "Video already exists in this playlist") + end + begin video = get_video(video_id) rescue ex : NotFoundException diff --git a/src/invidious/routes/playlists.cr b/src/invidious/routes/playlists.cr index 56e529b2..7fd0caf3 100644 --- a/src/invidious/routes/playlists.cr +++ b/src/invidious/routes/playlists.cr @@ -330,6 +330,15 @@ module Invidious::Routes::Playlists video_id = env.params.query["video_id"] + # Prevent duplicate videos in the same playlist + if Invidious::Database::PlaylistVideos.select_index(playlist_id, video_id) + if redirect + return env.redirect referer + else + return error_json(409, "Video already exists in this playlist") + end + end + begin video = get_video(video_id) rescue ex : NotFoundException