aboutsummaryrefslogtreecommitdiffstats
path: root/src/ctx.rs
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2024-04-03 12:11:34 -0400
committeralyx <alyx@aleteoryx.me>2024-04-03 12:11:34 -0400
commit27bd2e5f5699fd9101aa298993c3a9045caf02f4 (patch)
tree71ddd699984f7ed75e15bc40fb6026addb59d058 /src/ctx.rs
parent5a97731c6996847e335daf1fe9a5d265540de3d1 (diff)
downloadlfm_embed-27bd2e5f5699fd9101aa298993c3a9045caf02f4.tar.gz
lfm_embed-27bd2e5f5699fd9101aa298993c3a9045caf02f4.tar.bz2
lfm_embed-27bd2e5f5699fd9101aa298993c3a9045caf02f4.zip
Remove ResponseCtx
Diffstat (limited to 'src/ctx.rs')
-rw-r--r--src/ctx.rs117
1 files changed, 56 insertions, 61 deletions
diff --git a/src/ctx.rs b/src/ctx.rs
index 79bf985..6d0118e 100644
--- a/src/ctx.rs
+++ b/src/ctx.rs
@@ -107,69 +107,64 @@ pub mod model {
}
pub use model::Root as Ctx;
-#[derive(Debug)]
-pub struct ResponseCtx(pub model::Root, pub StatusCode);
-
-impl ResponseCtx {
- pub async fn create(api_result: Result<Arc<(de::User, de::Track, de::TrackStub)>, (StatusCode, &'static str)>, font_query: Option<FontQuery>, query: BTreeMap<String, String>) -> ResponseCtx {
- match api_result {
- Ok(a) => {
- let (user, track, trackstub) = a.as_ref();
- ResponseCtx((model::Data {
- user: model::User {
- name: user.name.clone(),
- realname: user.realname.clone(),
-
- scrobble_count: user.playcount,
- artist_count: user.artist_count,
- track_count: user.track_count,
- album_count: user.track_count,
-
- image_url: user.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()),
-
- url: user.url.clone()
+pub async fn get_ctx(api_result: Result<Arc<(de::User, de::Track, de::TrackStub)>, (StatusCode, &'static str)>, font_query: Option<FontQuery>, query: BTreeMap<String, String>) -> (Ctx, StatusCode) {
+ match api_result {
+ Ok(a) => {
+ let (user, track, trackstub) = a.as_ref();
+ ((model::Data {
+ user: model::User {
+ name: user.name.clone(),
+ realname: user.realname.clone(),
+
+ scrobble_count: user.playcount,
+ artist_count: user.artist_count,
+ track_count: user.track_count,
+ album_count: user.track_count,
+
+ image_url: user.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()),
+
+ url: user.url.clone()
+ },
+ scrobble: model::Scrobble {
+ name: track.name.clone(),
+ album: track.album.title.clone(),
+ artist: model::Artist {
+ name: track.artist.name.clone(),
+// image_url: track.artist.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()),
+ url: track.artist.url.clone().unwrap_or_else(|| "".into())
},
- scrobble: model::Scrobble {
- name: track.name.clone(),
- album: track.album.title.clone(),
- artist: model::Artist {
- name: track.artist.name.clone(),
-// image_url: track.artist.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()),
- url: track.artist.url.clone().unwrap_or_else(|| "".into())
- },
- image_url: track.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).filter(|s| !s.is_empty()).unwrap_or_else(|| "https://lastfm.freetls.fastly.net/i/u/128s/4128a6eb29f94943c9d206c08e625904.jpg".into()),
- now_playing: trackstub.attr.nowplaying,
- url: track.url.clone(),
- loved: track.loved.unwrap_or(false)
+ image_url: track.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).filter(|s| !s.is_empty()).unwrap_or_else(|| "https://lastfm.freetls.fastly.net/i/u/128s/4128a6eb29f94943c9d206c08e625904.jpg".into()),
+ now_playing: trackstub.attr.nowplaying,
+ url: track.url.clone(),
+ loved: track.loved.unwrap_or(false)
+ },
+ font: match font_query {
+ Some(FontQuery { google_font: Some(f), .. }) if CONFIG.has_google_api_key() => {
+ let css = match crate::cache::font::get_fontinfo(&f.to_string()).await {
+ Ok(css) => css,
+ Err((status, error)) => { return (model::Root::Error {error}, status); }
+ };
+ Some(model::Font::External {
+ css,
+ name: f
+ })
},
- font: match font_query {
- Some(FontQuery { google_font: Some(f), .. }) if CONFIG.has_google_api_key() => {
- let css = match crate::cache::font::get_fontinfo(&f.to_string()).await {
- Ok(css) => css,
- Err((status, error)) => { return ResponseCtx(model::Root::Error {error}, status); }
- };
- Some(model::Font::External {
- css,
- name: f
- })
- },
- Some(FontQuery { include_font: Some(f), .. }) => Some(
- model::Font::External {
- css: format!(
- "@font-face {{ font-family: 'included_font'; src: url('{}'); }}",
- f.replace('\\', "\\\\")
- .replace('\'', "\\'")).into(),
- name: "included_font".into()
- }),
- Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }),
- _ => None,
- },
- query
- }).into(), StatusCode::OK)
- },
- Err((status, error)) => {
- ResponseCtx(model::Root::Error {error}, status)
- }
+ Some(FontQuery { include_font: Some(f), .. }) => Some(
+ model::Font::External {
+ css: format!(
+ "@font-face {{ font-family: 'included_font'; src: url('{}'); }}",
+ f.replace('\\', "\\\\")
+ .replace('\'', "\\'")).into(),
+ name: "included_font".into()
+ }),
+ Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }),
+ _ => None,
+ },
+ query
+ }).into(), StatusCode::OK)
+ },
+ Err((status, error)) => {
+ (model::Root::Error {error}, status)
}
}
}