Skip to content

Commit 54ef43a

Browse files
committed
feat(post option)!: rename search to search-self
1 parent 7d0ab85 commit 54ef43a

File tree

9 files changed

+119
-25
lines changed

9 files changed

+119
-25
lines changed

src/api/post/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod get_count;
55
pub mod get_meta_list;
66
pub mod get_one;
77
pub mod get_one_raw;
8-
pub mod search;
8+
pub mod search_self;
99
pub mod update;
1010

1111
pub struct Post {

src/api/post/search_self.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

src/args/cmd/post.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ pub struct Opt {
4747
pub delete: bool,
4848

4949
#[arg(verbatim_doc_comment)]
50-
/// Search post by keyword and output the post id list that matches
51-
/// Example: cnb post --search 'Hello world'
50+
/// Search self post by keyword and output the post id list that matches
51+
/// Example: cnb post --search-self 'Hello world'
5252
/// *
5353
#[arg(long)]
54-
#[arg(short = 'f')]
55-
#[arg(visible_alias = "find")]
54+
#[arg(visible_alias = "f-self")]
55+
#[arg(visible_alias = "find-self")]
5656
#[arg(value_name = "KEYWORD")]
57-
pub search: Option<String>,
57+
pub search_self: Option<String>,
5858

5959
#[command(subcommand)]
6060
pub cmd: Option<Cmd>,

src/args/parser/post.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> {
1313
show_comment: false,
1414
list: true,
1515
delete: false,
16-
search: None,
16+
search_self: None,
1717
cmd: None,
1818
})),
1919
id: None,
@@ -41,7 +41,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
4141
show_comment: false,
4242
list: false,
4343
delete: false,
44-
search: None,
44+
search_self: None,
4545
cmd: None,
4646
})),
4747
id: Some(id),
@@ -65,7 +65,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
6565
show_comment: false,
6666
list: false,
6767
delete: false,
68-
search: None,
68+
search_self: None,
6969
cmd: None,
7070
})),
7171
id: Some(id),
@@ -89,7 +89,7 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
8989
show_comment: true,
9090
list: false,
9191
delete: false,
92-
search: None,
92+
search_self: None,
9393
cmd: None,
9494
})),
9595
id: Some(id),
@@ -103,7 +103,7 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
103103
.wrap_some()
104104
}
105105

106-
pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
106+
pub fn search_self_post(args: &Args) -> Option<(&String, usize, usize)> {
107107
match args {
108108
Args {
109109
cmd:
@@ -113,7 +113,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
113113
show_comment: false,
114114
list: false,
115115
delete: false,
116-
search: Some(keyword),
116+
search_self: Some(keyword),
117117
cmd: None,
118118
})),
119119
id: None,
@@ -141,7 +141,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
141141
show_comment: false,
142142
list: false,
143143
delete: true,
144-
search: None,
144+
search_self: None,
145145
cmd: None,
146146
})),
147147
id: Some(id),
@@ -165,7 +165,7 @@ pub fn create_post(args: &Args) -> Option<&CreateCmd> {
165165
show_comment: false,
166166
list: false,
167167
delete: false,
168-
search: None,
168+
search_self: None,
169169
cmd: Some(cmd::post::Cmd::Create(cmd)),
170170
})),
171171
id: None,
@@ -189,7 +189,7 @@ pub fn update_post(args: &Args) -> Option<(usize, &UpdateCmd)> {
189189
show_comment: false,
190190
list: false,
191191
delete: false,
192-
search: None,
192+
search_self: None,
193193
cmd: Some(cmd::post::Cmd::Update(cmd)),
194194
})),
195195
id: Some(id),

src/display/colorful/post.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub fn show_post_comment(
120120
})
121121
}
122122

123-
pub fn search_post(
123+
pub fn search_self_post(
124124
result: Result<(impl ExactSizeIterator<Item = usize>, usize)>,
125125
) -> Result<String> {
126126
let (mut id_iter, total_count) = match result {

src/display/json/post.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn show_post_comment(
4545
fmt_ok(comment_vec)
4646
}
4747

48-
pub fn search_post(result: Result<(impl ExactSizeIterator<Item = usize>, usize)>) -> String {
48+
pub fn search_self_post(result: Result<(impl ExactSizeIterator<Item = usize>, usize)>) -> String {
4949
let (id_iter, total_count) = match result {
5050
Ok(o) => o,
5151
Err(e) => return fmt_err(&e),

src/display/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ pub fn delete_post(style: &Style, result: &Result<usize>) -> String {
118118
}
119119
}
120120

121-
pub fn search_post(
121+
pub fn search_self_post(
122122
style: &Style,
123123
result: Result<(impl ExactSizeIterator<Item = usize>, usize)>,
124124
) -> Result<String> {
125125
match style {
126-
Style::Colorful => colorful::post::search_post(result),
127-
Style::Normal => normal::post::search_post(result),
128-
Style::Json => json::post::search_post(result).wrap_ok(),
126+
Style::Colorful => colorful::post::search_self_post(result),
127+
Style::Normal => normal::post::search_self_post(result),
128+
Style::Json => json::post::search_self_post(result).wrap_ok(),
129129
}
130130
}
131131

src/display/normal/post.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn show_post_comment(
118118
})
119119
}
120120

121-
pub fn search_post(
121+
pub fn search_self_post(
122122
result: Result<(impl ExactSizeIterator<Item = usize>, usize)>,
123123
) -> Result<String> {
124124
let (mut id_iter, total_count) = match result {

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ async fn main() -> Result<()> {
152152
foe.then(|| panic_if_err(&id));
153153
display::delete_post(style, &id)
154154
}
155-
_ if let Some((kw, skip, take)) = parser::post::search_post(&args) => {
155+
_ if let Some((kw, skip, take)) = parser::post::search_self_post(&args) => {
156156
let result = Post::new(pat?)
157-
.search(skip, take, kw)
157+
.search_self(skip, take, kw)
158158
.await
159159
.map(|(vec, count)| (vec.into_iter().dyn_rev(rev), count));
160160
foe.then(|| panic_if_err(&result));
161-
display::search_post(style, result)?
161+
display::search_self_post(style, result)?
162162
}
163163
_ if let Some(create_cmd) = parser::post::create_post(&args) => {
164164
let CreateCmd { title, body, publish } = create_cmd;

0 commit comments

Comments
 (0)