Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main/kotlin/dev/typetype/server/routes/WatchLaterRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import dev.typetype.server.models.WatchLaterItem
import dev.typetype.server.services.AuthService
import dev.typetype.server.services.WatchLaterService
import io.ktor.http.HttpStatusCode
import io.ktor.server.request.queryString
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
Expand All @@ -26,9 +27,27 @@ fun Route.watchLaterRoutes(watchLaterService: WatchLaterService, authService: Au
}
delete("/watch-later/{videoUrl...}") {
call.withJwtAuth(authService) { userId ->
val videoUrl = call.parameters.getAll("videoUrl")?.joinToString("/") ?: return@withJwtAuth call.respond(HttpStatusCode.BadRequest, ErrorResponse("Missing videoUrl"))
val pathVideoUrl = call.parameters.getAll("videoUrl")?.joinToString("/")
?: return@withJwtAuth call.respond(HttpStatusCode.BadRequest, ErrorResponse("Missing videoUrl"))
val decodedPathVideoUrl = pathVideoUrl.withUrlSchemeSlashes()
val queryString = call.request.queryString()
val videoUrl = if (queryString.isBlank() || "?" in decodedPathVideoUrl) {
decodedPathVideoUrl
} else {
"$decodedPathVideoUrl?$queryString"
}
val deleted = watchLaterService.delete(userId, videoUrl)
if (deleted) call.respond(HttpStatusCode.NoContent) else call.respond(HttpStatusCode.NotFound, ErrorResponse("Not found"))
if (deleted) {
call.respond(HttpStatusCode.NoContent)
} else {
call.respond(HttpStatusCode.NotFound, ErrorResponse("Not found"))
}
}
}
}

private fun String.withUrlSchemeSlashes(): String = when {
startsWith("https:/") && !startsWith("https://") -> "https://" + removePrefix("https:/")
startsWith("http:/") && !startsWith("http://") -> "http://" + removePrefix("http:/")
else -> this
}
13 changes: 13 additions & 0 deletions src/test/kotlin/dev/typetype/server/WatchLaterRoutesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ class WatchLaterRoutesTest {
assertEquals(HttpStatusCode.NoContent, client.delete("/watch-later/https%3A%2F%2Fyt.com") { headers.append(HttpHeaders.Authorization, "Bearer test-jwt") }.status)
}

@Test
fun `DELETE watch-later keeps decoded youtube query in url`() = withApp {
val videoUrl = "https://www.youtube.com/watch?v=test123"
service.add(
TEST_USER_ID,
WatchLaterItem(url = videoUrl, title = "Test", thumbnail = "", duration = 100L),
)
val response = client.delete("/watch-later/https://www.youtube.com/watch?v=test123") {
headers.append(HttpHeaders.Authorization, "Bearer test-jwt")
}
assertEquals(HttpStatusCode.NoContent, response.status)
}

@Test
fun `DELETE watch-later returns 404 when not found`() = withApp {
assertEquals(HttpStatusCode.NotFound, client.delete("/watch-later/https%3A%2F%2Fyt.com") { headers.append(HttpHeaders.Authorization, "Bearer test-jwt") }.status)
Expand Down