aboutsummaryrefslogtreecommitdiffstats
path: root/src/abook.rs
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/abook.rs
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/abook.rs')
-rw-r--r--src/abook.rs73
1 files changed, 47 insertions, 26 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?)?)
}
}