aboutsummaryrefslogtreecommitdiffstats
path: root/src/xchan.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/xchan.rs')
-rw-r--r--src/xchan.rs124
1 files changed, 96 insertions, 28 deletions
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())?)
}
}