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
//! Structs for storing request information.

use rvk::objects::Integer;
use serde_derive::Deserialize;
use serde_json::Value;
use std::collections::HashMap;

/// A request received from Callback API.
#[derive(Debug, Deserialize)]
pub struct CallbackAPIRequest {
    secret: String,
    group_id: i32,
    #[serde(rename = "type")]
    r#type: String,
    #[serde(default)]
    object: Object,
}

impl CallbackAPIRequest {
    /// Creates a new [`CallbackAPIRequest`].
    pub fn new(secret: &str, group_id: i32, r#type: &str, object: Object) -> Self {
        Self {
            secret: secret.into(),
            group_id,
            r#type: r#type.into(),
            object,
        }
    }

    /// Returns the secret sent in this request.
    pub fn secret(&self) -> &str {
        &self.secret
    }

    /// Returns the group ID sent in this request.
    pub fn group_id(&self) -> i32 {
        self.group_id
    }

    /// Returns the type of this request.
    pub fn r#type(&self) -> &str {
        &self.r#type
    }

    /// Returns the [`Object`] sent in this request.
    pub fn object(&self) -> &Object {
        &self.object
    }
}

/// An object of a [`CallbackAPIRequest`].
#[derive(Debug, Deserialize, Clone)]
pub struct Object {
    from_id: Option<Integer>,
    peer_id: Option<Integer>,
    user_id: Option<Integer>,
    text: Option<String>,
    payload: Option<String>,
    action: Option<Value>,

    #[serde(flatten)]
    extra: HashMap<String, Value>,
}

impl Default for Object {
    fn default() -> Self {
        Self {
            from_id: None,
            peer_id: None,
            user_id: None,
            text: None,
            payload: None,
            action: None,
            extra: Default::default(),
        }
    }
}

impl Object {
    /// Creates a new [`Object`].
    pub fn new(
        from_id: Option<Integer>,
        peer_id: Option<Integer>,
        user_id: Option<Integer>,
        text: Option<String>,
        payload: Option<String>,
        action: Option<Value>,
        extra: HashMap<String, Value>,
    ) -> Self {
        Self {
            from_id,
            peer_id,
            user_id,
            text,
            payload,
            action,
            extra,
        }
    }

    /// Returns the "from" ID of this [`Object`].
    pub fn get_from_id(&self) -> &Option<Integer> {
        &self.from_id
    }

    /// Returns the peer ID of this [`Object`].
    pub fn peer_id(&self) -> &Option<Integer> {
        &self.peer_id
    }

    /// Returns the user ID of this [`Object`].
    pub fn user_id(&self) -> &Option<Integer> {
        &self.user_id
    }

    /// Returns the text of this [`Object`].
    pub fn text(&self) -> &Option<String> {
        &self.text
    }

    /// Returns the payload of this [`Object`].
    pub fn payload(&self) -> &Option<String> {
        &self.payload
    }

    /// Returns the action of this [`Object`].
    pub fn action(&self) -> &Option<Value> {
        &self.action
    }

    /// Returns extra fields of this [`Object`].
    pub fn extra(&self) -> &HashMap<String, Value> {
        &self.extra
    }
}