Unwrap the segments for the video ID endpoint

This commit is contained in:
Adam Novak 2022-10-30 13:06:44 -04:00
parent a9683b08a0
commit c1ef37ad77

View file

@ -48,11 +48,9 @@ pub async fn skip_segments(
return content::RawJson("Hash prefix does not match format requirements.".to_string()); return content::RawJson("Hash prefix does not match format requirements.".to_string());
} }
let search_result = find_skip_segments(VideoName::ByHashPrefix(hash.clone()), categories, db).await; let sponsors = find_skip_segments(VideoName::ByHashPrefix(hash.clone()), categories, db).await;
match search_result { if sponsors.is_empty() {
Some(segments) => return segments,
None => {
// Fall back to central Sponsorblock server // Fall back to central Sponsorblock server
let resp = reqwest::get(format!( let resp = reqwest::get(format!(
"https://sponsor.ajay.app/api/skipSegments/{}?categories={}", "https://sponsor.ajay.app/api/skipSegments/{}?categories={}",
@ -67,7 +65,8 @@ pub async fn skip_segments(
return content::RawJson(resp); return content::RawJson(resp);
} }
}
return content::RawJson(serde_json::to_string(&sponsors).unwrap());
} }
#[get("/api/skipSegments?<videoID>&<categories>")] #[get("/api/skipSegments?<videoID>&<categories>")]
@ -82,11 +81,9 @@ pub async fn skip_segments_by_id(
return content::RawJson("videoID is missing".to_string()); return content::RawJson("videoID is missing".to_string());
} }
let search_result = find_skip_segments(VideoName::ByID(videoID.clone()), categories, db).await; let sponsors = find_skip_segments(VideoName::ByID(videoID.clone()), categories, db).await;
match search_result { if sponsors.is_empty() {
Some(segments) => return segments,
None => {
// Fall back to central Sponsorblock server // Fall back to central Sponsorblock server
let resp = reqwest::get(format!( let resp = reqwest::get(format!(
"https://sponsor.ajay.app/api/skipSegments?videoID={}&categories={}", "https://sponsor.ajay.app/api/skipSegments?videoID={}&categories={}",
@ -101,21 +98,22 @@ pub async fn skip_segments_by_id(
return content::RawJson(resp); return content::RawJson(resp);
} }
}
// Doing a lookup by video ID should return only one Sponsor object with
// one list of segments. We need to return just the list of segments.
return content::RawJson(serde_json::to_string(&sponsors[0].segments).unwrap());
} }
async fn find_skip_segments( async fn find_skip_segments(
name: VideoName, name: VideoName,
categories: Option<&str>, categories: Option<&str>,
db: Db, db: Db,
) -> Option<content::RawJson<String>> { ) -> Vec<Sponsor> {
let cat: Vec<String> = serde_json::from_str(categories.unwrap_or("[\"sponsor\"]")).unwrap(); let cat: Vec<String> = serde_json::from_str(categories.unwrap_or("[\"sponsor\"]")).unwrap();
if cat.is_empty() { if cat.is_empty() {
return Some(content::RawJson( return Vec::new();
"[]".to_string(),
));
} }
let results: Vec<SponsorTime> = db.run(move |conn| { let results: Vec<SponsorTime> = db.run(move |conn| {
@ -197,12 +195,7 @@ async fn find_skip_segments(
sponsor.segments.sort_by(|a, b| a.partial_cmp(b).unwrap()); sponsor.segments.sort_by(|a, b| a.partial_cmp(b).unwrap());
} }
if !sponsors.is_empty() { return sponsors.into_values().collect();
let sponsors: Vec<&Sponsor> = sponsors.values().collect();
return Some(content::RawJson(serde_json::to_string(&sponsors).unwrap()));
}
return None;
} }
fn similar_segments(segment: &Segment, hash: &str, segments: &Vec<SponsorTime>) -> Vec<Segment> { fn similar_segments(segment: &Segment, hash: &str, segments: &Vec<SponsorTime>) -> Vec<Segment> {
@ -272,9 +265,9 @@ fn best_segment(segments: &Vec<Segment>) -> Segment {
best_segment best_segment
} }
// We might need some more routes to support ReVanced. These are faked for now. // These additional routes are faked to protect ReVanced from seeing errors. We
// Sadly not even these are sufficient to make it work for me, maybe it is just // don't *need* to do this to support ReVanced, but it gets rid of the
// broken? // perpetual "Loading..." in the settings.
// This would take a userID // This would take a userID
#[get("/api/isUserVIP")] #[get("/api/isUserVIP")]