aboutsummaryrefslogblamecommitdiffstats
path: root/src/verify.rs
blob: 8edb2f18e06a1593a0934650c3be657d3cbb36de (plain) (tree)
































































































































































                                                                             
// zotapi - Rust wrapper for the Zot API as implemented by Hubzilla
// Copyright (C) 2023  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, XChan};
use serde::Deserialize;
use std::convert::TryFrom;

#[derive(Deserialize, Debug)]
pub struct Channel {
    #[serde(alias = "channel_id")]
    pub id: u32,

    #[serde(alias = "channel_account_id")]
    pub account_id: u32,

    #[serde(alias = "channel_primary")]
    pub primary: char,

    #[serde(alias = "channel_name")]
    pub name: String,

    #[serde(alias = "channel_address")]
    pub address: String,

    #[serde(alias = "channel_guid")]
    pub guid: String,

    #[serde(alias = "channel_guid_sig")]
    pub guid_sig: String,

    #[serde(alias = "channel_hash")]
    pub hash: String,

    #[serde(alias = "channel_timezone")]
    pub timezone: String,

    #[serde(alias = "channel_location")]
    pub location: String,

    #[serde(alias = "channel_theme")]
    pub theme: String,

    #[serde(alias = "channel_startpage")]
    pub startpage: String,

    #[serde(alias = "channel_pubkey")]
    pub pubkey: String,

    #[serde(alias = "channel_prvkey")]
    pub prvkey: String,

    #[serde(alias = "channel_notifyflags")]
    pub notifyflags: u32,

    #[serde(alias = "channel_pageflags")]
    pub pageflags: u32,

    #[serde(alias = "channel_dirdate")]
    pub dirdate: String,

    #[serde(alias = "channel_lastpost")]
    pub lastpost: String,

    #[serde(alias = "channel_deleted")]
    pub deleted: String,

    #[serde(alias = "channel_active")]
    pub active: String,

    #[serde(alias = "channel_max_anon_mail")]
    pub max_anon_mail: u32,

    #[serde(alias = "channel_max_friend_req")]
    pub max_friend_req: u32,

    #[serde(alias = "channel_expire_days")]
    pub expire_days: u32,

    #[serde(alias = "channel_passwd_reset")]
    pub passwd_reset: String,

    #[serde(alias = "channel_default_group")]
    pub default_group: String,

    #[serde(alias = "channel_allow_cid")]
    pub allow_cid: String,

    #[serde(alias = "channel_allow_gid")]
    pub allow_gid: String,

    #[serde(alias = "channel_deny_cid")]
    pub deny_cid: String,

    #[serde(alias = "channel_deny_gid")]
    pub deny_gid: String,

    #[serde(alias = "channel_removed")]
    pub removed: u32,

    #[serde(alias = "channel_system")]
    pub system: u32,

    #[serde(alias = "channel_moved")]
    pub moved: String,

    #[serde(alias = "channel_password")]
    pub password: String,

    #[serde(alias = "channel_salt")]
    pub salt: String,

    #[serde(alias = "channel_portable_id")]
    pub portable_id: String,

    #[serde(alias = "channel_xchan", skip)]
    pub xchan: XChan,
}

impl Channel {
    pub fn z() -> ChannelRequest {
        ChannelRequest::default()
    }
}

impl<'a> TryFrom<&'a str> for Channel {
    type Error = Error;

    fn try_from(s: &'a str) -> Result<Self, Self::Error> {
        Ok(serde_json::from_str(s)?)
    }
}

#[derive(Default)]
pub struct ChannelRequest;

impl ChannelRequest {
    pub async fn fetch_raw(&self, client: &Client) -> Result<String, Error> {
        Ok(client.get("verify").send().await?.text().await?)
    }

    pub async fn fetch(&self, client: &Client) -> Result<Channel, Error> {
        let raw = self.fetch_raw(&client).await?;
        let mut channel: Channel = serde_json::from_str(&raw)?;
        channel.xchan = serde_json::from_str(&raw)?;

        Ok(channel)
    }
}