1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// zotapi - Rust wrapper for Sot API as implemented by Hubzilla
// 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 <https://www.gnu.org/licenses/>.
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<Self, Self::Error> {
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<XChanRequestSelector<'a>>,
}
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<String, Error> {
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<XChan, Error> {
Ok(XChan::try_from(self.fetch_raw(&client).await?.as_str())?)
}
}
|