watchdogs/src/pages/watch.rs

80 lines
2.5 KiB
Rust
Raw Normal View History

2024-12-18 20:09:07 +01:00
use based::{
auth::MaybeUser,
format::format_date,
request::{RequestContext, StringResponse},
2024-12-14 00:24:10 +01:00
};
2024-12-18 20:09:07 +01:00
use maud::html;
use rocket::{get, State};
2024-12-14 00:24:10 +01:00
2024-12-22 20:19:52 +01:00
use crate::{
library::{history::VideoHistory, Library},
pages::components::video_element_wide,
};
2024-12-14 00:24:10 +01:00
2024-12-18 19:24:38 +01:00
use super::components::render_page;
2024-12-14 00:24:10 +01:00
#[get("/watch?<v>")]
pub async fn watch_page(
2024-12-18 19:24:38 +01:00
ctx: RequestContext,
2024-12-14 00:24:10 +01:00
library: &State<Library>,
v: String,
2024-12-18 20:09:07 +01:00
user: MaybeUser,
) -> StringResponse {
2024-12-14 00:24:10 +01:00
let video = if let Some(video) = library.get_video_by_id(&v).await {
video
} else {
// TODO : Error handling
library.get_video_by_youtube_id(&v).await.unwrap()
};
2024-12-22 20:19:52 +01:00
if let Some(user) = user.user() {
user.insert_history(video.id).await;
}
2024-12-14 00:24:10 +01:00
let content = html!(
main class="container mx-auto mt-6 flex flex-col lg:flex-row gap-6" {
2024-12-16 22:00:08 +01:00
div class="lg:w-10/12 mt-10" {
2024-12-14 00:24:10 +01:00
div class="bg-black aspect-video rounded-lg overflow-hidden" {
video
controls
autoplay
class="w-full h-full" {
2024-12-16 22:21:09 +01:00
source src=(format!("/video/raw?v={}", video.id)) type="video/mp4" {
2024-12-14 00:24:10 +01:00
"Your browser does not support the video"
};
};
};
div class="p-4 bg-stone-900 rounded-lg shadow-lg mt-8" {
h2 class="text-2xl font-semibold" { (video.title) };
2024-12-14 23:33:47 +01:00
@if let Some(meta) = video.youtube_meta().await {
div class="flex justify-between mt-2" {
p class="mb-4 text-gray-300" { (meta.uploader_name) };
p class="mb-4 text-gray-300" { (format!("{} Views ﹣ {}", meta.views, format_date(&meta.upload_date))) };
};
a href=(format!("https://www.youtube.com/watch?v={}", meta.id)) class="text-blue-400" {"Watch on YouTube" };
p class="mb-2 text-gray-300 text-bold mt-2" { "Description: " } span { (meta.description) };
};
2024-12-14 00:24:10 +01:00
};
};
2024-12-29 21:39:54 +01:00
div id="recommendations" class="mt-8 w-1/3" {
2024-12-16 22:00:08 +01:00
h3 class="text-center text-4xl font-extrabold leading-tight mb-2" { "In " a class="text-blue-500" href=(format!("/d/{}", video.directory)) { (video.directory) }; }
@for video in library.get_directory_videos(&video.directory).await {
(video_element_wide(&video).await);
};
};
2024-12-14 00:24:10 +01:00
};
);
2024-12-18 19:24:38 +01:00
render_page(
ctx,
content,
&format!("{} - WatchDogs", video.title),
2024-12-18 20:09:07 +01:00
user.into(),
2024-12-18 19:24:38 +01:00
)
.await
2024-12-14 00:24:10 +01:00
}