From a2dd0a60086330c2d2d2e70354c8580cf5f76bb3 Mon Sep 17 00:00:00 2001 From: syui Date: Tue, 6 Feb 2024 20:09:27 +0900 Subject: [PATCH] add config --- Cargo.toml | 1 + src/data.rs | 13 +++++++++++++ src/main.rs | 9 +++++++++ src/reply.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) create mode 100644 src/data.rs create mode 100644 src/reply.rs diff --git a/Cargo.toml b/Cargo.toml index 2af4ffe..2f638bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" seahorse = "*" reqwest = { version = "*", features = ["blocking", "json"] } tokio = { version = "1", features = ["full"] } +shellexpand = "*" diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..a961fbe --- /dev/null +++ b/src/data.rs @@ -0,0 +1,13 @@ +//use config::{Config, ConfigError, File}; +//use serde_derive::{Deserialize, Serialize}; + +pub fn token_file(s: &str) -> String { + let file = "/.config/ai/token"; + let mut f = shellexpand::tilde("~").to_string(); + f.push_str(&file); + match &*s { + "toml" => f + &".toml", + "json" => f + &".json", + _ => f + &"." + &s, + } +} diff --git a/src/main.rs b/src/main.rs index 4e94b81..7a22bc3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,14 @@ pub mod refresh; pub mod token; pub mod ascii; +pub mod data; use seahorse::{App, Command, Context, Flag, FlagType}; use std::env; +use std::fs; +use std::io::Write; use crate::ascii::c_ascii; +use crate::data::token_file; fn main() { let args: Vec = env::args().collect(); @@ -59,10 +63,15 @@ fn c_refresh(c: &Context) { fn token(c: &Context) { let m = c.args[0].to_string(); + let f = token_file(&"json"); + let mut f = fs::File::create(f.clone()).unwrap(); + let h = async { if let Ok(p) = c.string_flag("password") { let str = token::post_request(m.to_string(), p.to_string()).await; println!("{}",str); + + f.write_all(&str.as_bytes()).unwrap(); } }; let res = tokio::runtime::Runtime::new().unwrap().block_on(h); diff --git a/src/reply.rs b/src/reply.rs new file mode 100644 index 0000000..fb3cd53 --- /dev/null +++ b/src/reply.rs @@ -0,0 +1,53 @@ +extern crate reqwest; +use crate::token_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 url = url(&"record_create"); + //let url = "https://bsky.social/xrpc/com.atproto.repo.createRecord"; + let col = "app.bsky.feed.post".to_string(); + + let d = Timestamp::now_utc(); + let d = d.to_string(); + + let post = Some(json!({ + "repo": handle.to_string(), + "did": did.to_string(), + "collection": col.to_string(), + "record": { + "text": text.to_string(), + "createdAt": d.to_string(), + "reply": { + "root": { + "cid": cid.to_string(), + "uri": uri.to_string() + }, + "parent": { + "cid": cid_p.to_string(), + "uri": uri_p.to_string() + } + } + }, + })); + + let client = reqwest::Client::new(); + let res = client + .post(url) + .json(&post) + .header("Authorization", "Bearer ".to_owned() + &token) + .send() + .await + .unwrap() + .text() + .await + .unwrap(); + + return res +}