From 47b1e7cb894f95f477745ae14ec75893d016791e Mon Sep 17 00:00:00 2001 From: syui Date: Wed, 7 Feb 2024 11:34:12 +0900 Subject: [PATCH] add notify --- src/main.rs | 59 ++++++++++++++++++++++++++++++++++----------------- src/notify.rs | 31 +++++++++++++++++++++++++++ src/reply.rs | 8 +++---- src/token.rs | 3 ++- 4 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 src/notify.rs diff --git a/src/main.rs b/src/main.rs index 89742ed..5a8701c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use crate::data::w_cfg; pub mod data; pub mod refresh; pub mod token; +pub mod notify; pub mod ascii; fn main() { @@ -25,11 +26,6 @@ fn main() { .alias("t"), ) ) - .command( - Command::new("refresh") - .alias("r") - .action(c_refresh), - ) .command( Command::new("token") .alias("t") @@ -41,6 +37,16 @@ fn main() { .alias("p"), ) ) + .command( + Command::new("refresh") + .alias("r") + .action(c_refresh), + ) + .command( + Command::new("notify") + .alias("n") + .action(c_notify), + ) ; app.run(args); } @@ -49,20 +55,6 @@ fn c_ascii_art(c: &Context) { c_ascii(c.bool_flag("type")); } -fn refresh() { - let h = async { - let res = refresh::post_request().await; - println!("{}", res); - w_cfg("bsky.social", &res) - }; - let res = tokio::runtime::Runtime::new().unwrap().block_on(h); - return res -} - -fn c_refresh(_c: &Context) { - refresh(); -} - fn token(c: &Context) { let m = c.args[0].to_string(); let h = async { @@ -79,3 +71,32 @@ fn token(c: &Context) { fn c_token(c: &Context) { token(c); } + +fn refresh() { + let h = async { + let res = refresh::post_request().await; + println!("{}", res); + w_cfg("bsky.social", &res) + }; + let res = tokio::runtime::Runtime::new().unwrap().block_on(h); + return res +} + +fn c_refresh(_c: &Context) { + refresh(); +} + + + +fn notify() { + let h = async { + let j = notify::get_request(100).await; + println!("{}", j); + }; + let res = tokio::runtime::Runtime::new().unwrap().block_on(h); + return res +} + +fn c_notify(_c: &Context) { + notify(); +} diff --git a/src/notify.rs b/src/notify.rs new file mode 100644 index 0000000..51afa7e --- /dev/null +++ b/src/notify.rs @@ -0,0 +1,31 @@ +extern crate reqwest; +use crate::data_toml; +use crate::url; +//use serde_json::json; + +pub async fn get_request(limit: i32, ) -> String { + + let token = data_toml(&"access"); + let url = url(&"notify_list"); + + let client = reqwest::Client::new(); + let res = client + .get(url) + .query(&[("limit", limit)]) + .header("Authorization", "Bearer ".to_owned() + &token) + .send() + .await + .unwrap(); + + let status_ref = res.error_for_status_ref(); + + match status_ref { + Ok(_) => { + return res.text().await.unwrap(); + }, + Err(_e) => { + let e = "err".to_string(); + return e + } + } +} diff --git a/src/reply.rs b/src/reply.rs index fb3cd53..478fbce 100644 --- a/src/reply.rs +++ b/src/reply.rs @@ -1,14 +1,14 @@ extern crate reqwest; -use crate::token_toml; +use crate::data_toml; use crate::url; use serde_json::json; use iso8601_timestamp::Timestamp; pub async fn post_request(text: String, cid: String, uri: String, cid_p: String, uri_p: String) -> String { - let token = token_toml(&"access"); - let did = token_toml(&"did"); - let handle = token_toml(&"handle"); + let token = data_toml(&"access"); + let did = data_toml(&"did"); + let handle = data_toml(&"handle"); let url = url(&"record_create"); //let url = "https://bsky.social/xrpc/com.atproto.repo.createRecord"; diff --git a/src/token.rs b/src/token.rs index e5f5396..2b35127 100644 --- a/src/token.rs +++ b/src/token.rs @@ -1,9 +1,10 @@ extern crate reqwest; use std::collections::HashMap; +use crate::url; pub async fn post_request(handle: String, pass: String) -> String { - let url = "https://bsky.social/xrpc/com.atproto.server.createSession"; + let url = url(&"session_create"); let mut map = HashMap::new(); map.insert("identifier", &handle);