|
| 1 | +use crate::api::post::Post; |
| 2 | +use crate::blog_backend; |
| 3 | +use crate::infra::http::{body_or_err, RequestBuilderExt}; |
| 4 | +use crate::infra::iter::IntoIteratorExt; |
| 5 | +use crate::infra::json; |
| 6 | +use crate::infra::result::WrapResult; |
| 7 | +use anyhow::Result; |
| 8 | +use serde_json::Value; |
| 9 | +use std::collections::HashSet; |
| 10 | +use std::iter; |
| 11 | + |
| 12 | +impl Post { |
| 13 | + pub async fn search_self( |
| 14 | + &self, |
| 15 | + skip: usize, |
| 16 | + take: usize, |
| 17 | + keyword: &str, |
| 18 | + ) -> Result<(Vec<usize>, usize)> { |
| 19 | + let client = &reqwest::Client::new(); |
| 20 | + |
| 21 | + // total_count is used for patch the buggy blog backend API |
| 22 | + // If index is greater than the max page index, API will still return the last page |
| 23 | + let total_count = { |
| 24 | + let req = { |
| 25 | + let url = blog_backend!("/posts/list"); |
| 26 | + let query = [ |
| 27 | + ("t", 1.to_string()), |
| 28 | + ("p", 1.to_string()), |
| 29 | + ("s", 1.to_string()), |
| 30 | + ("search", keyword.to_string()), |
| 31 | + ]; |
| 32 | + client.get(url).query(&query).pat_auth(&self.pat) |
| 33 | + }; |
| 34 | + let resp = req.send().await?; |
| 35 | + |
| 36 | + // total_count |
| 37 | + { |
| 38 | + let body = body_or_err(resp).await?; |
| 39 | + let json = json::deserialize::<Value>(&body)?; |
| 40 | + json["postsCount"] |
| 41 | + .as_u64() |
| 42 | + .expect("as_u64 failed for `postsCount`") as usize |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + let range = (skip + 1)..=(skip + take).min(total_count); |
| 47 | + let id_list = range |
| 48 | + .map(|i| async move { |
| 49 | + let req = { |
| 50 | + let url = blog_backend!("/posts/list"); |
| 51 | + let query = [ |
| 52 | + ("t", 1.to_string()), |
| 53 | + ("p", i.to_string()), |
| 54 | + ("s", 1.to_string()), |
| 55 | + ("search", keyword.to_string()), |
| 56 | + ]; |
| 57 | + client.get(url).query(&query).pat_auth(&self.pat) |
| 58 | + }; |
| 59 | + let resp = req.send().await?; |
| 60 | + |
| 61 | + let id_list = { |
| 62 | + let body = body_or_err(resp).await?; |
| 63 | + let mut json = json::deserialize::<Value>(&body)?; |
| 64 | + let post_id = { |
| 65 | + let json = json["postList"].take(); |
| 66 | + let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?; |
| 67 | + post["id"].as_u64().expect("as_u64 failed for `id`") as usize |
| 68 | + }; |
| 69 | + let zzk_post_id_list = { |
| 70 | + let json = json["zzkSearchResult"]["postIds"].take(); |
| 71 | + serde_json::from_value::<Vec<usize>>(json) |
| 72 | + }?; |
| 73 | + |
| 74 | + zzk_post_id_list |
| 75 | + .into_iter() |
| 76 | + .chain(iter::once(post_id)) |
| 77 | + .collect::<Vec<usize>>() |
| 78 | + }; |
| 79 | + |
| 80 | + id_list.wrap_ok::<anyhow::Error>() |
| 81 | + }) |
| 82 | + .join_all() |
| 83 | + .await |
| 84 | + .into_iter() |
| 85 | + .collect::<Result<Vec<_>>>()? |
| 86 | + .into_iter() |
| 87 | + .flatten() |
| 88 | + .collect::<HashSet<_>>() |
| 89 | + .into_iter() |
| 90 | + .collect::<Vec<_>>(); |
| 91 | + |
| 92 | + (id_list, total_count).wrap_ok() |
| 93 | + } |
| 94 | +} |
0 commit comments