aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2023-03-26 14:14:52 +0200
committerHarald Eilertsen <haraldei@anduin.net>2023-03-26 14:14:52 +0200
commit3cc87b45f70ae32fe8ca69b3cd185dd8d56d94dd (patch)
treecbc0b84d6668de545c623834727665cbb08902ba /src
parentd08138ea633da76d9ca390e4f9e3c5489aee23d1 (diff)
downloadrust-zotapi-3cc87b45f70ae32fe8ca69b3cd185dd8d56d94dd.tar.gz
rust-zotapi-3cc87b45f70ae32fe8ca69b3cd185dd8d56d94dd.tar.bz2
rust-zotapi-3cc87b45f70ae32fe8ca69b3cd185dd8d56d94dd.zip
Fetch abook and xchan directly from api.
We don't really need the intermediate layer in the binary module.
Diffstat (limited to 'src')
-rw-r--r--src/abook.rs73
-rw-r--r--src/bin/zot/main.rs27
-rw-r--r--src/bin/zot/zot/abook.rs29
-rw-r--r--src/bin/zot/zot/mod.rs2
-rw-r--r--src/bin/zot/zot/xchan.rs41
-rw-r--r--src/xchan.rs124
6 files changed, 162 insertions, 134 deletions
diff --git a/src/abook.rs b/src/abook.rs
index 938a48f..b6cd59b 100644
--- a/src/abook.rs
+++ b/src/abook.rs
@@ -16,90 +16,106 @@
use crate::{client::Client, error::Error};
use serde::Deserialize;
+use std::convert::TryFrom;
#[derive(Debug, Default, Deserialize)]
pub struct Abook {
#[serde(rename = "abook_id")]
- id: u32,
+ pub id: u32,
#[serde(rename = "abook_account")]
- account: u32,
+ pub account: u32,
#[serde(rename = "abook_channel")]
- channel: u32,
+ pub channel: u32,
#[serde(rename = "abook_xchan")]
- xchan: String,
+ pub xchan: String,
#[serde(rename = "abook_my_perms")]
- my_perms: u16,
+ pub my_perms: u16,
#[serde(rename = "abook_their_perms")]
- their_perms: u16,
+ pub their_perms: u16,
// Closeness is a number between 0 and 99, but is somehow
// returned as a string.
#[serde(rename = "abook_closeness")]
- closeness: String,
+ pub closeness: String,
#[serde(rename = "abook_created")]
- created: String,
+ pub created: String,
#[serde(rename = "abook_updated")]
- updated: String,
+ pub updated: String,
#[serde(rename = "abook_connected")]
- connected: String,
+ pub connected: String,
#[serde(rename = "abook_dob")]
- dob: String,
+ pub dob: String,
#[serde(rename = "abook_flags")]
- flags: u16, // No longer used
+ pub flags: u16, // No longer used
#[serde(rename = "abook_profile")]
- profile: String,
+ pub profile: String,
#[serde(rename = "abook_blocked")]
- blocked: u8,
+ pub blocked: u8,
#[serde(rename = "abook_ignored")]
- ignored: u8,
+ pub ignored: u8,
#[serde(rename = "abook_hidden")]
- hidden: u8,
+ pub hidden: u8,
#[serde(rename = "abook_archived")]
- archived: u8,
+ pub archived: u8,
#[serde(rename = "abook_pending")]
- pending: u8,
+ pub pending: u8,
#[serde(rename = "abook_unconnected")]
- unconnected: u8, // Currently unused
+ pub unconnected: u8, // Currently unused
#[serde(rename = "abook_self")]
- myself: u8, // original name `self`
+ pub myself: u8, // original name `self`
#[serde(rename = "abook_feed")]
- feed: u8,
+ pub feed: u8,
+
+ #[serde(rename = "abook_not_here")]
+ pub not_here: u8,
#[serde(rename = "abook_incl")]
- incl: String,
+ pub incl: String,
#[serde(rename = "abook_excl")]
- excl: String,
+ pub excl: String,
#[serde(rename = "abook_instance")]
- instance: String,
+ pub instance: String,
+
+ #[serde(rename = "abook_role")]
+ pub role: String,
}
+
impl Abook {
pub fn z() -> AbookRequest {
AbookRequest::default()
}
}
+impl<'a> TryFrom<&'a str> for Abook {
+ type Error = Error;
+
+ fn try_from(s: &'a str) -> Result<Self, Self::Error> {
+ Ok(serde_json::from_str(s)?)
+ }
+}
+
#[derive(Debug, Default)]
pub struct AbookRequest {
abook_id: Option<u32>,
@@ -114,13 +130,18 @@ impl AbookRequest {
self
}
- pub async fn fetch(&self, client: &Client) -> Result<Vec<Abook>, Error> {
+ pub async fn fetch_raw(&self, client: &Client) -> Result<String, Error> {
let mut req = client.get("abook");
if let Some(id) = self.abook_id {
req = req.query(&[("abook_id", id.to_string())]);
}
- Ok(serde_json::from_str(&req.send().await?.text().await?)?)
+ Ok(req.send().await?
+ .text().await?)
+ }
+
+ pub async fn fetch(&self, client: &Client) -> Result<Vec<Abook>, Error> {
+ Ok(serde_json::from_str(&self.fetch_raw(&client).await?)?)
}
}
diff --git a/src/bin/zot/main.rs b/src/bin/zot/main.rs
index 09fdd61..f2caac4 100644
--- a/src/bin/zot/main.rs
+++ b/src/bin/zot/main.rs
@@ -90,8 +90,13 @@ async fn main() {
zot::abconfig::fetch(&client).await;
}
("abook", Some(m)) => {
- let raw = m.is_present("raw");
- zot::abook::fetch(&client, raw).await;
+ let r = zotapi::Abook::z();
+
+ if m.is_present("raw") {
+ println!("{}", r.fetch_raw(&client).await.unwrap());
+ } else {
+ println!("{:?}", r.fetch(&client).await);
+ }
}
("group", Some(m)) => {
if let Some(id) = m.value_of("ID") {
@@ -129,16 +134,22 @@ async fn main() {
}
}
("xchan", Some(m)) => {
- let raw = m.is_present("raw");
- let t = if m.is_present("guid") {
- zot::xchan::Type::GUID
+ let mut r = zotapi::XChan::z();
+ let id = m.value_of("ID").expect("No xchan provided.");
+
+ if m.is_present("guid") {
+ r.by_guid(id)
} else if m.is_present("hash") {
- zot::xchan::Type::Hash
+ r.by_hash(id)
} else {
- zot::xchan::Type::Addr
+ r.by_address(id)
};
- zot::xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()).await;
+ if m.is_present("raw") {
+ println!("{}", r.fetch_raw(&client).await.unwrap());
+ } else {
+ println!("{:?}", r.fetch(&client).await);
+ }
}
("post", Some(m)) => {
zot::item::post(&client, m).await;
diff --git a/src/bin/zot/zot/abook.rs b/src/bin/zot/zot/abook.rs
deleted file mode 100644
index 6c33757..0000000
--- a/src/bin/zot/zot/abook.rs
+++ /dev/null
@@ -1,29 +0,0 @@
-/* Example Zot API command line utility, part of zotapi.
- * Copyright (C) 2018 Harald Eilertsen <haraldei@anduin.net>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-pub async fn fetch(client: &zotapi::Client, _raw: bool) {
- match zotapi::Abook::z().fetch(&client).await {
- Ok(abooks) => {
- for b in abooks {
- println!("{:?}", b);
- }
- }
- Err(e) => {
- println!("{:?}", e);
- }
- }
-}
diff --git a/src/bin/zot/zot/mod.rs b/src/bin/zot/zot/mod.rs
index 706f496..23e25ec 100644
--- a/src/bin/zot/zot/mod.rs
+++ b/src/bin/zot/zot/mod.rs
@@ -1,7 +1,5 @@
pub mod abconfig;
-pub mod abook;
pub mod channel_stream;
pub mod group;
pub mod item;
pub mod network_stream;
-pub mod xchan;
diff --git a/src/bin/zot/zot/xchan.rs b/src/bin/zot/zot/xchan.rs
deleted file mode 100644
index 146b2ab..0000000
--- a/src/bin/zot/zot/xchan.rs
+++ /dev/null
@@ -1,41 +0,0 @@
-/* Example Zot API command line utility, part of zotapi.
- * Copyright (C) 2018 <name of copyright holder>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-use zotapi;
-
-pub enum Type {
- Addr,
- Hash,
- GUID,
-}
-
-pub async fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) {
- let res = match t {
- Type::Addr => zotapi::XChan::z().by_address(&id).fetch(&client).await,
- Type::Hash => zotapi::XChan::z().by_hash(&id).fetch(&client).await,
- Type::GUID => zotapi::XChan::z().by_guid(&id).fetch(&client).await,
- };
-
- match res {
- Ok(payload) => {
- println!("{:?}", payload);
- }
- Err(e) => {
- println!("{:?}", e);
- }
- }
-}
diff --git a/src/xchan.rs b/src/xchan.rs
index 6fbe7a2..4544f08 100644
--- a/src/xchan.rs
+++ b/src/xchan.rs
@@ -16,35 +16,91 @@
use crate::{client::Client, error::Error};
use serde::Deserialize;
+use std::convert::TryFrom;
#[derive(Debug, Default, Deserialize)]
pub struct XChan {
- hash: String,
- guid: String,
- guid_sig: String,
- pubkey: String,
- photo_mimetype: String,
- photo_l: String,
- photo_m: String,
- photo_s: String,
- addr: String,
- url: String,
- connurl: String,
- follow: String,
- connpage: String,
- name: String,
- network: String,
- instance_url: String,
- flags: u32,
- photo_date: String,
- name_date: String,
- hidden: u32,
- orphan: u32,
- censored: u32,
- selfcensored: u32,
- system: u32,
- pubforum: u32,
- deleted: u32,
+ #[serde(alias = "xchan_hash")]
+ pub hash: String,
+
+ #[serde(alias = "xchan_guid")]
+ pub guid: String,
+
+ #[serde(alias = "xchan_guid_sig")]
+ pub guid_sig: String,
+
+ #[serde(alias = "xchan_pubkey")]
+ pub pubkey: String,
+
+ #[serde(alias = "xchan_photo_mimetype")]
+ pub photo_mimetype: String,
+
+ #[serde(alias = "xchan_photo_l")]
+ pub photo_l: String,
+
+ #[serde(alias = "xchan_photo_m")]
+ pub photo_m: String,
+
+ #[serde(alias = "xchan_photo_s")]
+ pub photo_s: String,
+
+ // For some reason the address field will be exported as `addr`
+ // when part of an aboox entry, but `address` when just exporting
+ // the xchan. `addr` is the name used in the database table.
+
+ #[serde(alias = "xchan_addr")]
+ pub address: String,
+
+ #[serde(alias = "xchan_url")]
+ pub url: String,
+
+ #[serde(alias = "xchan_connurl")]
+ pub connurl: String,
+
+ #[serde(alias = "xchan_follow")]
+ pub follow: String,
+
+ #[serde(alias = "xchan_connpage")]
+ pub connpage: String,
+
+ #[serde(alias = "xchan_name")]
+ pub name: String,
+
+ #[serde(alias = "xchan_network")]
+ pub network: String,
+
+ #[serde(alias = "xchan_instance_url")]
+ pub instance_url: String,
+
+ #[serde(alias = "xchan_flags")]
+ pub flags: u32,
+
+ #[serde(alias = "xchan_photo_date")]
+ pub photo_date: String,
+
+ #[serde(alias = "xchan_name_date")]
+ pub name_date: String,
+
+ #[serde(alias = "xchan_hidden")]
+ pub hidden: u32,
+
+ #[serde(alias = "xchan_orphan")]
+ pub orphan: u32,
+
+ #[serde(alias = "xchan_censored")]
+ pub censored: u32,
+
+ #[serde(alias = "xchan_selfcensored")]
+ pub selfcensored: u32,
+
+ #[serde(alias = "xchan_system")]
+ pub system: u32,
+
+ #[serde(alias = "xchan_pubforum")]
+ pub pubforum: u32,
+
+ #[serde(alias = "deleted", default)]
+ pub deleted: u32,
}
impl XChan {
@@ -53,6 +109,14 @@ impl XChan {
}
}
+impl<'a> TryFrom<&'a str> for XChan {
+ type Error = Error;
+
+ fn try_from(s: &'a str) -> Result<Self, Self::Error> {
+ Ok(serde_json::from_str(s)?)
+ }
+}
+
enum XChanRequestSelector<'a> {
Address(&'a str),
Hash(&'a str),
@@ -80,7 +144,7 @@ impl<'a> XChanRequest<'a> {
self
}
- pub async fn fetch(&self, client: &Client) -> Result<XChan, Error> {
+ pub async fn fetch_raw(&self, client: &Client) -> Result<String, Error> {
let mut req = client.get("xchan");
if let Some(sel) = &self.data {
@@ -91,6 +155,10 @@ impl<'a> XChanRequest<'a> {
};
}
- Ok(serde_json::from_str(&dbg!(req.send().await?.text().await?))?)
+ Ok(req.send().await?.text().await?)
+ }
+
+ pub async fn fetch(&self, client: &Client) -> Result<XChan, Error> {
+ Ok(XChan::try_from(self.fetch_raw(&client).await?.as_str())?)
}
}