Skip to content

Commit

Permalink
refactor-ing: refactor ing command, add query, create, delete subcomm…
Browse files Browse the repository at this point in the history
…ands
  • Loading branch information
RocsSun committed Jan 23, 2024
1 parent 04ca15e commit 338a3c7
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 122 deletions.
15 changes: 10 additions & 5 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ cnb posts [comment] [list,create,query,delete,update] --[id/file/quertset] --[pa
闪存cli设计如下:

```sh
cnb ing query # 默认10条
cnb ing query --id 123456
cnb ing query --page 1 --count 10
cnb ing query --type All --page 1 --count 10 --tag Linux
cnb ing create --conent hello --private false --lucky false
cnb ing query # 默认10条s
cnb ing query --id 123456 --id 123
cnb ing query -n 1 -s 10
cnb ing query --type f -n 2 -s 10
# 根据tag查找,-g为tag名称 -n 2 -s 10 分页
cnb ing query -t t -g Linux
cnb ing create hello --private --lucky
cnb ing create hello --private --lucky --tag hello
cnb ing delete --id 123456
```

TODO: “提到我”存在解析问题。待完善。
18 changes: 1 addition & 17 deletions src/apis/ing/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{Ok, Result};
use reqwest::{Client, Response};
use serde::{Deserialize, Serialize};

use crate::{infra::http::RequestBuilderExt, openapi};
use crate::{api::ing::get_comment_list::IngCommentEntry, infra::http::RequestBuilderExt, openapi};

/// 闪存评论及评论回复
///
Expand All @@ -24,22 +24,6 @@ pub struct StatusComment {
pub content: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct IngCommentEntry {
pub id: usize,
pub content: String,
#[serde(rename = "DateAdded")]
pub create_time: String,
pub status_id: usize,
pub user_alias: String,
#[serde(rename = "UserDisplayName")]
pub user_name: String,
pub user_icon_url: String,
pub user_id: usize,
pub user_guid: String,
}

/// 根据闪存ID发表一个评论
pub async fn post(token: String, sc: StatusComment) -> Result<Response> {
let r = Client::new()
Expand Down
58 changes: 16 additions & 42 deletions src/apis/ing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
pub mod comment;

use anyhow::{Ok, Result};
use clap::{ValueEnum, Parser};
use clap::{Parser, ValueEnum};
use reqwest::{Client, Response};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

use crate::{infra::http::RequestBuilderExt, openapi};
use crate::{
api::ing::{get_list::IngEntry, IngSendFrom},
infra::http::RequestBuilderExt,
openapi,
};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
Expand All @@ -43,20 +46,6 @@ impl Default for IngContent {
}
}

#[derive(Clone, Debug, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum IngSendFrom {
None = 0,
Ms = 1,
GTalk = 2,
Qq = 3,
Sms = 5,
CellPhone = 6,
Web = 8,
VsCode = 9,
Cli = 13,
}

impl Default for IngSendFrom {
fn default() -> Self {
return IngSendFrom::Cli;
Expand Down Expand Up @@ -90,12 +79,12 @@ impl Default for QeurySet {
///
/// Follow = 1, 关注
/// Myself = 4, 我的
/// Public = 5,
/// RecentComment = 6, //新回应
/// Public = 5, 全站
/// RecentComment = 6, 新回应
/// MyComment = 7, 我回应
/// Tag = 10, tag 必填
/// Comment = 13 回复我
/// Mention = 14,
/// Mention = 14, @我
#[derive(Debug, Clone, ValueEnum, Parser)]
pub enum QueryIngType {
Following = 1,
Expand All @@ -118,6 +107,12 @@ impl From<u8> for QueryIngType {
fn from(value: u8) -> Self {
match value {
1 => Self::Following,
4 => Self::My,
6 => Self::RecentComment,
7 => Self::MyComment,
10 => Self::Tag,
13 => Self::Comment,
14 => Self::Mention,
_ => Self::All,
}
}
Expand All @@ -138,27 +133,6 @@ impl QueryIngType {
}
}

/// 闪存详细内容。
///
/// 用于根据ID查询闪存的结果解析。
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct IngEntry {
pub id: u64,
pub content: String,
pub is_private: bool,
pub is_lucky: bool,
pub comment_count: u64,
pub date_added: String,
pub user_alias: String,
pub user_display_name: String,
pub user_icon_url: String,
pub user_id: u64,
pub user_guid: String,
pub send_from: u8,
pub icons: String,
}

pub async fn lastest(token: &str) -> Result<Response> {
let c = Client::new()
.get(openapi!("/statuses/recent"))
Expand Down Expand Up @@ -211,7 +185,7 @@ pub async fn post(token: &str, c: &IngContent) -> Result<Response> {
}

/// 删除一条闪存
pub async fn delete(token: &str, id: String) -> Result<Response> {
pub async fn delete(token: &str, id: u64) -> Result<Response> {
let r = Client::new()
.post(openapi!("/statuses/{}", id))
.pat_auth(token)
Expand Down
52 changes: 27 additions & 25 deletions src/args/cmd/ing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
api::ing::IngType,
apis::{self, ing::QeurySet},
api::ing::{IngType, IngSendFrom},
apis::{
self,
ing::{IngContent, QeurySet},
},
};
use clap::{Args, Parser, Subcommand, ValueEnum};

Expand Down Expand Up @@ -59,49 +62,48 @@ pub enum Cmd {
},

/// 根据条件查询闪存。
///
Query(QueryIng),

/// 创建闪存
Create(CreateIng),

/// 根据ID删除闪存
Delete {
#[arg(long)]
id: Vec<u64>,
},
Delete { id: Vec<u64> },
}

#[derive(Debug, Args)]
pub struct CreateIng {
/// 闪存内容
// #[arg(short, long)]
content: Option<String>,
#[arg(short, long, default_value_t = true)]
private: bool,

#[arg(short, long, default_value_t = false)]
lucky: bool,

#[arg(short, long)]
tag: Option<String>,
}

#[derive(Args, Debug)]
struct Create {
/// 闪存内容
content: String,
pub content: String,

/// 是否私有,默认是全站
#[arg(short, long, default_value_t = true)]
private: bool,
pub private: bool,

/// 是否发布为幸运
#[arg(short, long, default_value_t = false)]
lucky: bool,
pub lucky: bool,

/// 是否发布在某个标签下,默认不发布标签。
#[arg(short, long, default_value = "")]
tag: String,
pub tag: String,
}

impl From<CreateIng> for IngContent {
fn from(value: CreateIng) -> Self {
let mut cont = String::new();
if !value.tag.is_empty() {
cont.push_str(format!("[{}]", value.tag).as_str())
}
cont.push_str(value.content.as_str());
Self {
content: cont,
is_private: value.private,
lucky: value.lucky,
client_type: IngSendFrom::Cli
}
}
}

/// 查询参数
Expand Down
58 changes: 57 additions & 1 deletion src/args/parser/ing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::api::ing::IngType;
use crate::args::cmd::ing::QueryIng;
use crate::args::cmd::ing::{QueryIng, CreateIng};
use crate::args::parser::{get_skip, get_take};
use crate::args::{cmd, Args, Cmd};
use crate::infra::option::WrapOption;
Expand Down Expand Up @@ -102,3 +102,59 @@ pub fn query(args: &Args) -> Option<QueryIng> {
}
.wrap_some()
}


#[allow(unused)]
pub fn create_ing(args: &Args) -> Option<CreateIng> {
match args {
Args {
cmd:
Some(Cmd::Ing(cmd::ing::Opt {
cmd:
Some(cmd::ing::Cmd::Create(CreateIng {
content,
private,
lucky,
tag,
})),
publish: None,
comment: None,
})),
id: None,
rev: _,
skip,
take,
global_opt: _,
} => CreateIng {
content: content.clone(),
private:private.clone(),
lucky: lucky.clone(),
tag: tag.clone(),
},
_ => return None,
}
.wrap_some()
}


#[allow(unused)]
pub fn delete(args: &Args) -> Option<Vec<u64>> {
match args {
Args {
cmd:
Some(Cmd::Ing(cmd::ing::Opt {
cmd:
Some(cmd::ing::Cmd::Delete{id}),
publish: None,
comment: None,
})),
id: None,
rev: _,
skip,
take,
global_opt: _,
} => id.clone(),
_ => return None,
}
.wrap_some()
}
20 changes: 17 additions & 3 deletions src/bin/cnb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ use cnblogs_lib::api::user::User;
use cnblogs_lib::args::cmd::post::{CreateCmd, UpdateCmd};
use cnblogs_lib::args::parser::no_operation;
use cnblogs_lib::args::{parser, Args};
use cnblogs_lib::display;
use cnblogs_lib::{display, logic};
use cnblogs_lib::infra::fp::currying::eq;
use cnblogs_lib::infra::infer::infer;
use cnblogs_lib::infra::iter::{ExactSizeIteratorExt, IntoIteratorExt};
use cnblogs_lib::infra::option::OptionExt;
use cnblogs_lib::infra::result::WrapResult;
use cnblogs_lib::logic::ing::get_ings_and_comments;
use colored::Colorize;
use std::env;

Expand Down Expand Up @@ -87,8 +86,23 @@ async fn main() -> Result<()> {
}

_ if let Some(q) = parser::ing::query(&args) => {
let ing_with_comment_iter = logic::ing::get_ings_and_comments(pat.unwrap().as_str(), &q)
.await
.map(|vec| vec.into_iter().dyn_rev(rev));

foe.then(|| panic_if_err(&ing_with_comment_iter));
display::list_ing(style, time_style, ing_with_comment_iter, true)?
}

_ if let Some(ids) = parser::ing::delete(&args) => {
let a = pat.as_ref().unwrap().as_str();
logic::ing::delete_by_ing_id(a, ids).await;
"".to_string()
}

get_ings_and_comments(pat.unwrap().as_str(), &q).await;
_ if let Some(ci) = parser::ing::create_ing(&args) => {
let a = pat.as_ref().unwrap().as_str();
logic::ing::create_ing_with_arg(a, ci).await;
"".to_string()
}

Expand Down
Loading

0 comments on commit 338a3c7

Please sign in to comment.