/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ use super::ui::{account::session::Session, error::MyResult}; use crate::routes::ui::node::rocket_uri_macro_r_library_node; use anyhow::anyhow; use jellybase::database::Database; use jellycommon::user::WatchedState; use rocket::{post, response::Redirect, State}; #[post("/n//watched?")] pub async fn r_player_watched( session: Session, db: &State, id: &str, state: bool, ) -> MyResult { db.node .get(&id.to_string())? .ok_or(anyhow!("node does not exist"))?; let key = (session.user.name.clone(), id.to_owned()); db.user_node.fetch_and_update(&key, |t| { let mut t = t.unwrap_or_default(); if state { t.watched = WatchedState::Watched } else { t.watched = WatchedState::None } Some(t) })?; Ok(Redirect::found(rocket::uri!(r_library_node(id)))) } #[post("/n//progress?")] pub async fn r_player_progress( session: Session, db: &State, id: &str, t: f64, ) -> MyResult<()> { db.node .get(&id.to_string())? .ok_or(anyhow!("node does not exist"))?; let key = (session.user.name.clone(), id.to_owned()); db.user_node.fetch_and_update(&key, |d| { let mut d = d.unwrap_or_default(); d.watched = match d.watched { WatchedState::None | WatchedState::Progress(_) => WatchedState::Progress(t), WatchedState::Watched => WatchedState::Watched, }; Some(d) })?; // db.user_progess.fetch_and_update(&session.user.name, |p| { // let mut m = p.unwrap_or_else(|| HashMap::new()); // if let Some(t) = t { // if m.len() < 16 { // m.insert(id.to_string(), t); // } // } else { // m.remove(&id.to_string()); // } // if m.is_empty() { // None // } else { // Some(m) // } // })?; Ok(()) }