forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1use actix_web::{get, post, web, HttpResponse, Responder};
2use anyhow::Error;
3use scrobble::handle_scrobble;
4use sqlx::{Pool, Postgres};
5use std::collections::BTreeMap;
6use std::sync::Arc;
7use v1::authenticate::authenticate;
8use v1::nowplaying::nowplaying;
9use v1::submission::submission;
10
11use crate::cache::Cache;
12use crate::musicbrainz::client::MusicbrainzClient;
13use crate::BANNER;
14
15pub mod scrobble;
16pub mod v1;
17
18#[get("/")]
19pub async fn index(
20 data: web::Data<Arc<Pool<Postgres>>>,
21 cache: web::Data<Cache>,
22 params: web::Query<BTreeMap<String, String>>,
23) -> impl Responder {
24 if params.is_empty() {
25 return Ok(HttpResponse::Ok().body(BANNER));
26 }
27
28 authenticate(params.into_inner(), cache.get_ref(), data.get_ref())
29 .await
30 .map_err(actix_web::error::ErrorInternalServerError)
31}
32
33#[post("/nowplaying")]
34pub async fn handle_nowplaying(
35 data: web::Data<Arc<Pool<Postgres>>>,
36 cache: web::Data<Cache>,
37 form: web::Form<BTreeMap<String, String>>,
38) -> impl Responder {
39 nowplaying(form.into_inner(), cache.get_ref(), data.get_ref())
40 .map_err(actix_web::error::ErrorInternalServerError)
41}
42
43#[post("/submission")]
44pub async fn handle_submission(
45 data: web::Data<Arc<Pool<Postgres>>>,
46 cache: web::Data<Cache>,
47 mb_client: web::Data<Arc<MusicbrainzClient>>,
48 form: web::Form<BTreeMap<String, String>>,
49) -> impl Responder {
50 submission(
51 form.into_inner(),
52 cache.get_ref(),
53 data.get_ref(),
54 mb_client.get_ref(),
55 )
56 .await
57 .map_err(actix_web::error::ErrorInternalServerError)
58}
59
60#[get("/2.0")]
61pub async fn handle_get() -> impl Responder {
62 HttpResponse::Ok().body(BANNER)
63}
64
65#[post("/2.0")]
66pub async fn handle_methods(
67 data: web::Data<Arc<Pool<Postgres>>>,
68 cache: web::Data<Cache>,
69 form: web::Form<BTreeMap<String, String>>,
70 mb_client: web::Data<Arc<MusicbrainzClient>>,
71) -> impl Responder {
72 let conn = data.get_ref();
73 let cache = cache.get_ref();
74 let mb_client = mb_client.get_ref();
75
76 let method = form.get("method").unwrap_or(&"".to_string()).to_string();
77 call_method(&method, conn, cache, mb_client, form.into_inner())
78 .await
79 .map_err(actix_web::error::ErrorInternalServerError)
80}
81
82pub async fn call_method(
83 method: &str,
84 pool: &Arc<Pool<Postgres>>,
85 cache: &Cache,
86 mb_client: &Arc<MusicbrainzClient>,
87 form: BTreeMap<String, String>,
88) -> Result<HttpResponse, Error> {
89 match method {
90 "track.scrobble" => handle_scrobble(form, pool, cache, mb_client).await,
91 _ => Err(Error::msg(format!("Unsupported method: {}", method))),
92 }
93}