aboutsummaryrefslogblamecommitdiffstats
path: root/src/abconfig.rs
blob: 723747c83e1894af7b140af74378a90ea8554cc4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                         
                                          
                       
 


                                                              

                             
                                  
                

               
                  

                                                 
                      

                
                    

           
                  

             
                  
 
 
                                   


                                                               
 
 






                                                                          




                                                              








                                                     
// 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;

/// A struct for storing a key value pair with a category in
/// relation to a contact. Typically used to store permissions
/// a given contact has on a given channel.
#[derive(Debug, Deserialize)]
pub struct ABConfig {
    /// Database ID for this entry
    pub id: u32,

    /// Channel
    pub chan: u32,

    /// Unique identifier for the related contact
    pub xchan: String,

    /// Category
    pub cat: String,

    /// Key
    pub k: String,

    /// Value
    pub v: String,
}

/// Fetch ABConfig for all contacts
pub fn fetch(client: &Client) -> Result<Vec<ABConfig>, Error> {
    let payload = client.fetch_stream("abconfig", &())?;
    Ok(serde_json::from_str(&payload)?)
}

#[derive(Default)]
pub struct ABConfigRequest {
    abook_id: u32,
}

impl ABConfigRequest {
    pub fn fetch(&self, client: &Client) -> Result<Vec<ABConfig>, Error> {
        let mut res = client.get("abconfig")
            .query(&[("abook_id", self.abook_id.to_string())])
            .send()?;

        Ok(serde_json::from_str(&res.text()?)?)
    }
}

/// Fetch ABConfig for a given contact
///
/// `abook` is the abook id of the given contact
pub fn with_abook_id(abook: u32) -> ABConfigRequest {
    ABConfigRequest { abook_id: abook }
}