fixes lol

This commit is contained in:
doomtube 2026-01-09 02:37:31 -05:00
parent a206a606f7
commit 07b8e12197
10 changed files with 211 additions and 57 deletions

View file

@ -1109,11 +1109,67 @@ void WatchController::nextVideo(const HttpRequestPtr &req,
return;
}
// Lock the room state row to prevent concurrent modifications
*trans << "SELECT current_video_id FROM watch_room_state WHERE realm_id = $1 FOR UPDATE"
// Lock the room state row and get current video info to check if locked
*trans << "SELECT wrs.current_video_id, wp.is_locked, wp.youtube_video_id, wp.title, "
"wp.duration_seconds, wp.thumbnail_url "
"FROM watch_room_state wrs "
"LEFT JOIN watch_playlist wp ON wrs.current_video_id = wp.id "
"WHERE wrs.realm_id = $1 FOR UPDATE"
<< id
>> [callback, trans, id](const Result&) {
// Mark current video as played
>> [callback, trans, id](const Result& currentResult) {
// Check if current video is locked - if so, restart it instead of advancing
bool currentIsLocked = false;
int64_t currentVideoId = 0;
if (!currentResult.empty() && !currentResult[0]["current_video_id"].isNull()) {
currentVideoId = currentResult[0]["current_video_id"].as<int64_t>();
currentIsLocked = !currentResult[0]["is_locked"].isNull() &&
currentResult[0]["is_locked"].as<bool>();
}
if (currentIsLocked && currentVideoId > 0) {
// Locked video - restart it instead of advancing
std::string youtubeVideoId = currentResult[0]["youtube_video_id"].as<std::string>();
std::string title = currentResult[0]["title"].as<std::string>();
int durationSeconds = currentResult[0]["duration_seconds"].as<int>();
std::string thumbnailUrl = currentResult[0]["thumbnail_url"].isNull() ? "" :
currentResult[0]["thumbnail_url"].as<std::string>();
// Reset time to 0 and update started_at
*trans << "UPDATE watch_playlist SET started_at = CURRENT_TIMESTAMP "
"WHERE id = $1"
<< currentVideoId
>> [callback, trans, id, currentVideoId, youtubeVideoId, title, durationSeconds, thumbnailUrl](const Result&) {
// Update room state to reset time
*trans << "UPDATE watch_room_state SET current_time_seconds = 0, "
"playback_state = 'playing', last_sync_at = CURRENT_TIMESTAMP "
"WHERE realm_id = $1"
<< id
>> [callback, currentVideoId, youtubeVideoId, title, durationSeconds, thumbnailUrl](const Result&) {
Json::Value resp;
resp["success"] = true;
resp["playbackState"] = "playing";
resp["currentTime"] = 0.0;
resp["serverTime"] = getCurrentTimestampMs();
resp["event"] = "locked_restart";
resp["leadIn"] = true;
Json::Value video;
video["id"] = static_cast<Json::Int64>(currentVideoId);
video["youtubeVideoId"] = youtubeVideoId;
video["title"] = title;
video["durationSeconds"] = durationSeconds;
video["thumbnailUrl"] = thumbnailUrl;
video["isLocked"] = true;
resp["currentVideo"] = video;
callback(jsonResp(resp));
}
>> DB_ERROR(callback, "update room state for locked restart");
}
>> DB_ERROR(callback, "update playlist for locked restart");
return;
}
// Not locked - mark current video as played and advance to next
*trans << "UPDATE watch_playlist SET status = 'played' "
"WHERE realm_id = $1 AND status = 'playing'"
<< id