A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 467f86fa118c1852771109fd6f8e946dc715d224 (5594 bytes)[doc][blame][raw]
1
use async_std::io::ReadExt;2
use regex::Regex;3
use sqlx::Acquire;4
use sqlx::Postgres;5
use tide_sqlx::SQLxRequestExt;7
use crate::model;8
use crate::unwrap_option_or_throw;9
use crate::unwrap_result_or_throw;10
use crate::Response;11
use crate::ToSql;13
pub async fn do_prefetch(_: tide::Request<()>) -> tide::Result {14
Ok(tide::Response::new(200))15
}17
pub async fn get_account(req: tide::Request<()>) -> tide::Result {18
let account =19
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");20
Response::with(account)21
}23
pub async fn do_search(req: tide::Request<()>) -> tide::Result {24
let query = req.query::<model::Query>()?;25
let account =26
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");27
let sql = format!(28
r#"29
SELECT30
station.*,31
station_status.is_restricted,32
station_status.is_broken,33
station_status.is_no_track_info,34
CASE35
WHEN favorite.id IS NULL OR favorite.account_id != {account_id}36
THEN false37
ELSE true38
END AS is_favorite39
FROM station40
LEFT JOIN favorite41
ON station.id = favorite.station_id42
LEFT JOIN station_status43
ON station.id = station_status.station_id44
{conditions}45
OFFSET {offset}46
LIMIT 1047
"#,48
account_id = account.id,49
offset = query.page.unwrap_or_default() * 10,50
conditions = unwrap_result_or_throw!(query.to_sql(), "cannot parse sql statement!"),51
);52
let mut conn = req.sqlx_conn::<Postgres>().await;53
let result = sqlx::query_as::<_, model::Station>(&sql)54
.fetch_all(conn.acquire().await?)55
.await?;56
Response::with(result)57
}59
pub async fn get_song(req: tide::Request<()>) -> tide::Result {60
let station = req.query::<model::Station>()?;61
let mut response = surf::get(&station.url).header("icy-metadata", "1").await?;62
let meta_int = match response.header("icy-metaint") {63
Some(header) => header.as_str(),64
None => return Response::throw("no icy metadata header found!"),65
};66
let interval = match meta_int.parse::<usize>() {67
Ok(interval) => interval * 2,68
Err(_) => return Response::throw("icy metadata interval is not a number!"),69
};70
let mut total = 0;71
let mut bytes = Vec::new();72
let mut buf = [0; 10000];73
let mut title = None;74
loop {75
let len =76
unwrap_result_or_throw!(response.read(&mut buf).await, "cannot read stream data!");77
bytes.extend_from_slice(&buf);78
if total + len > interval {79
let metadata = String::from_utf8_lossy(&bytes);80
if let Some(cap) =81
unwrap_result_or_throw!(Regex::new("StreamTitle='([^;]*)';"), "cannot parse regex!")82
.captures_iter(&metadata)83
.next()84
{85
title = Some(cap[1].to_string());86
}87
break;88
}89
if total > interval * 2 {90
warn!("too many iterations while acquiring song title!");91
break;92
}93
total += len;94
}95
Response::with(title)96
}98
pub async fn get_favorites(req: tide::Request<()>) -> tide::Result {99
let account =100
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");101
let sql = format!(102
r#"103
SELECT104
station.*,105
station_status.is_restricted,106
station_status.is_broken,107
station_status.is_no_track_info,108
true as is_favorite109
FROM station110
LEFT JOIN favorite111
ON station.id = favorite.station_id112
LEFT JOIN station_status113
ON station.id = station_status.station_id114
WHERE favorite.account_id = {account_id}115
"#,116
account_id = account.id,117
);118
let mut conn = req.sqlx_conn::<Postgres>().await;119
let result = sqlx::query_as::<_, model::Station>(&sql)120
.fetch_all(conn.acquire().await?)121
.await?;122
Response::with(result)123
}125
pub async fn add_favorite(mut req: tide::Request<()>) -> tide::Result {126
let station_id = req.body_json::<i32>().await?;127
let account =128
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");129
let sql = format!(130
r#"131
INSERT INTO132
favorite133
(account_id, station_id)134
VALUES135
({account_id}, {station_id})136
"#,137
account_id = account.id,138
station_id = station_id,139
);140
let mut conn = req.sqlx_conn::<Postgres>().await;141
sqlx::query(&sql).execute(conn.acquire().await?).await?;142
Response::with(())143
}145
pub async fn delete_favorite(mut req: tide::Request<()>) -> tide::Result {146
let station_id = req.body_json::<i32>().await?;147
let account =148
unwrap_option_or_throw!(req.ext::<model::Account>(), "no account in request found!");149
let sql = format!(150
r#"151
DELETE FROM152
favorite153
WHERE account_id = {account_id}154
AND station_id = {station_id}155
"#,156
account_id = account.id,157
station_id = station_id,158
);159
let mut conn = req.sqlx_conn::<Postgres>().await;160
sqlx::query(&sql).execute(conn.acquire().await?).await?;161
Response::with(())162
}