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).
This commit is contained in:
NorkzYT 2026-03-23 00:11:25 +00:00
parent 0eb941f498
commit 58ced289d3
2 changed files with 14 additions and 0 deletions

View File

@ -313,6 +313,11 @@ module Invidious::Routes::API::V1::Authenticated
return error_json(403, "Invalid videoId") return error_json(403, "Invalid videoId")
end 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 begin
video = get_video(video_id) video = get_video(video_id)
rescue ex : NotFoundException rescue ex : NotFoundException

View File

@ -330,6 +330,15 @@ module Invidious::Routes::Playlists
video_id = env.params.query["video_id"] 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 begin
video = get_video(video_id) video = get_video(video_id)
rescue ex : NotFoundException rescue ex : NotFoundException