From 27bd2e5f5699fd9101aa298993c3a9045caf02f4 Mon Sep 17 00:00:00 2001 From: alyx Date: Wed, 3 Apr 2024 12:11:34 -0400 Subject: Remove ResponseCtx --- src/ctx.rs | 117 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 56 insertions(+), 61 deletions(-) (limited to 'src/ctx.rs') 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, (StatusCode, &'static str)>, font_query: Option, query: BTreeMap) -> 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, (StatusCode, &'static str)>, font_query: Option, query: BTreeMap) -> (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) } } } -- cgit v1.2.3-54-g00ecf