aboutsummaryrefslogtreecommitdiff
path: root/server/src/routes/progress.rs
blob: c4eb182b304f9244caadc9a91b5df6de85882160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
    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 <metamuffin.org>
*/
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/<id>/watched?<state>")]
pub async fn r_player_watched(
    session: Session,
    db: &State<Database>,
    id: &str,
    state: bool,
) -> MyResult<Redirect> {
    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/<id>/progress?<t>")]
pub async fn r_player_progress(
    session: Session,
    db: &State<Database>,
    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(())
}