aboutsummaryrefslogtreecommitdiff
path: root/server/src/compat/youtube.rs
blob: 1df2751b954ed652fa76a732503443101021afb3 (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
/*
    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) 2025 metamuffin <metamuffin.org>
*/
use crate::{
    logic::session::Session,
    ui::{
        error::MyResult,
        node::rocket_uri_macro_r_library_node,
        player::{rocket_uri_macro_r_player, PlayerConfig},
    },
};
use anyhow::anyhow;
use jellybase::database::Database;
use jellycommon::NodeID;
use rocket::{get, response::Redirect, State};

#[get("/watch?<v>")]
pub fn r_youtube_watch(_session: Session, db: &State<Database>, v: &str) -> MyResult<Redirect> {
    if v.len() != 11 {
        Err(anyhow!("video id length incorrect"))?
    }
    let Some(id) = db.get_node_external_id("youtube:video", v)? else {
        Err(anyhow!("element not found"))?
    };
    let node = db.get_node(id)?.ok_or(anyhow!("node missing"))?;
    Ok(Redirect::to(rocket::uri!(r_player(
        &node.slug,
        PlayerConfig::default()
    ))))
}

#[get("/channel/<id>")]
pub fn r_youtube_channel(_session: Session, db: &State<Database>, id: &str) -> MyResult<Redirect> {
    let Some(id) = (if id.starts_with("UC") {
        db.get_node_external_id("youtube:channel", id)?
    } else if id.starts_with("@") {
        db.get_node_external_id("youtube:channel-name", id)?
    } else {
        Err(anyhow!("unknown channel id format"))?
    }) else {
        Err(anyhow!("channel not found"))?
    };
    let node = db.get_node(id)?.ok_or(anyhow!("node missing"))?;
    Ok(Redirect::to(rocket::uri!(r_library_node(&node.slug))))
}

#[get("/embed/<v>")]
pub fn r_youtube_embed(_session: Session, db: &State<Database>, v: &str) -> MyResult<Redirect> {
    if v.len() != 11 {
        Err(anyhow!("video id length incorrect"))?
    }
    let Some(id) = db.get_node_external_id("youtube:video", v)? else {
        Err(anyhow!("element not found"))?
    };
    let node = db.get_node(id)?.ok_or(anyhow!("node missing"))?;
    Ok(Redirect::to(rocket::uri!(r_player(
        &node.slug,
        PlayerConfig::default()
    ))))
}