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
#[cfg(feature = "validator")]
use validator::Validate;

use super::File;

auto_derived_partial!(
    /// Webhook
    pub struct Webhook {
        /// Webhook Id
        pub id: String,

        /// The name of the webhook
        pub name: String,

        /// The avatar of the webhook
        #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
        pub avatar: Option<File>,

        /// The channel this webhook belongs to
        pub channel_id: String,

        /// The permissions for the webhook
        pub permissions: u64,

        /// The private token for the webhook
        pub token: Option<String>,
    },
    "PartialWebhook"
);

auto_derived!(
    /// Information about the webhook bundled with Message
    pub struct MessageWebhook {
        // The name of the webhook - 1 to 32 chars
        pub name: String,

        // The id of the avatar of the webhook, if it has one
        pub avatar: Option<String>,
    }

    /// New webhook information
    #[cfg_attr(feature = "validator", derive(validator::Validate))]
    pub struct DataEditWebhook {
        /// Webhook name
        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 32)))]
        pub name: Option<String>,

        /// Avatar ID
        #[cfg_attr(feature = "validator", validate(length(min = 1, max = 128)))]
        pub avatar: Option<String>,

        /// Webhook permissions
        pub permissions: Option<u64>,

        /// Fields to remove from webhook
        #[cfg_attr(feature = "serde", serde(default))]
        pub remove: Vec<FieldsWebhook>,
    }

    /// Webhook information
    pub struct ResponseWebhook {
        /// Webhook Id
        pub id: String,

        /// Webhook name
        pub name: String,

        /// Avatar ID
        pub avatar: Option<String>,

        /// The channel this webhook belongs to
        pub channel_id: String,

        /// The permissions for the webhook
        pub permissions: u64,
    }

    /// Optional fields on webhook object
    pub enum FieldsWebhook {
        Avatar,
    }

    /// Information for the webhook
    #[cfg_attr(feature = "validator", derive(Validate))]
    pub struct CreateWebhookBody {
        #[validate(length(min = 1, max = 32))]
        pub name: String,

        #[validate(length(min = 1, max = 128))]
        pub avatar: Option<String>,
    }
);

impl From<Webhook> for MessageWebhook {
    fn from(value: Webhook) -> Self {
        MessageWebhook {
            name: value.name,
            avatar: value.avatar.map(|file| file.id),
        }
    }
}

impl From<Webhook> for ResponseWebhook {
    fn from(value: Webhook) -> Self {
        ResponseWebhook {
            id: value.id,
            name: value.name,
            avatar: value.avatar.map(|file| file.id),
            channel_id: value.channel_id,
            permissions: value.permissions,
        }
    }
}