// zotapi - Rust wrapper for Sot API as implemented by Hubzilla // Copyright (C) 2018 Harald Eilertsen // // 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 . use crate::{client::Client, error::Error}; use serde::Deserialize; use std::convert::TryFrom; #[derive(Debug, Default, Deserialize)] pub struct XChan { #[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 { pub fn z<'a>() -> XChanRequest<'a> { XChanRequest::default() } } impl<'a> TryFrom<&'a str> for XChan { type Error = Error; fn try_from(s: &'a str) -> Result { Ok(serde_json::from_str(s)?) } } enum XChanRequestSelector<'a> { Address(&'a str), Hash(&'a str), GUID(&'a str), } #[derive(Default)] pub struct XChanRequest<'a> { data: Option>, } impl<'a> XChanRequest<'a> { pub fn by_address(&mut self, addr: &'a str) -> &mut XChanRequest<'a> { self.data = Some(XChanRequestSelector::Address(addr)); self } pub fn by_hash(&mut self, hash: &'a str) -> &mut XChanRequest<'a> { self.data = Some(XChanRequestSelector::Hash(hash)); self } pub fn by_guid(&mut self, guid: &'a str) -> &mut XChanRequest<'a> { self.data = Some(XChanRequestSelector::GUID(guid)); self } pub async fn fetch_raw(&self, client: &Client) -> Result { let mut req = client.get("xchan"); if let Some(sel) = &self.data { req = match sel { XChanRequestSelector::Address(s) => req.query(&[("address", s.to_string())]), XChanRequestSelector::Hash(s) => req.query(&[("hash", s.to_string())]), XChanRequestSelector::GUID(s) => req.query(&[("guid", s.to_string())]), }; } Ok(req.send().await?.text().await?) } pub async fn fetch(&self, client: &Client) -> Result { Ok(XChan::try_from(self.fetch_raw(&client).await?.as_str())?) } }