aboutsummaryrefslogtreecommitdiffstats
path: root/src/abook.rs
blob: 59981bad3f719d211a5b2c09d6b713b2873cdaca (plain) (blame)
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
// zotapi - Rust wrapper for the Zot 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;

#[derive(Debug, Default, Deserialize)]
pub struct Abook {
    #[serde(rename = "abook_id")]
    id: u32,

    #[serde(rename = "abook_account")]
    account: u32,

    #[serde(rename = "abook_channel")]
    channel: u32,

    #[serde(rename = "abook_xchan")]
    xchan: String,

    #[serde(rename = "abook_my_perms")]
    my_perms: u16,

    #[serde(rename = "abook_their_perms")]
    their_perms: u16,

    // Closeness is a number between 0 and 99, but is somehow
    // returned as a string.
    #[serde(rename = "abook_closeness")]
    closeness: String,

    #[serde(rename = "abook_created")]
    created: String,

    #[serde(rename = "abook_updated")]
    updated: String,

    #[serde(rename = "abook_connected")]
    connected: String,

    #[serde(rename = "abook_dob")]
    dob: String,

    #[serde(rename = "abook_flags")]
    flags: u16,         // No longer used

    #[serde(rename = "abook_profile")]
    profile: String,

    #[serde(rename = "abook_blocked")]
    blocked: u8,

    #[serde(rename = "abook_ignored")]
    ignored: u8,

    #[serde(rename = "abook_hidden")]
    hidden: u8,

    #[serde(rename = "abook_archived")]
    archived: u8,

    #[serde(rename = "abook_pending")]
    pending: u8,

    #[serde(rename = "abook_unconnected")]
    unconnected: u8,  // Currently unused

    #[serde(rename = "abook_self")]
    myself: u8,       // original name `self`

    #[serde(rename = "abook_feed")]
    feed: u8,

    #[serde(rename = "abook_incl")]
    incl: String,

    #[serde(rename = "abook_excl")]
    excl: String,

    #[serde(rename = "abook_instance")]
    instance: String,
}

impl Abook {
    pub fn z() -> AbookRequest {
        AbookRequest::default()
    }
}

#[derive(Debug, Default)]
pub struct AbookRequest {
    abook_id: Option<u32>,
}

impl AbookRequest {
    /// Limit to ABConfigs for a given contact
    ///
    /// `abook` is the abook id of the given contact
    pub fn with_abook_id(mut self, abook: u32) -> AbookRequest {
        self.abook_id = Some(abook);
        self
    }

    pub fn fetch(&self, client: &Client) -> Result<Vec<Abook>, 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()?.text()?)?)
    }
}