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
use std::fmt;

use revolt_result::Result;
use ulid::Ulid;

use crate::Database;

auto_derived!(
    /// Ratelimit Event
    pub struct RatelimitEvent {
        /// Id
        #[serde(rename = "_id")]
        pub id: String,
        /// Relevant Object Id
        pub target_id: String,
        /// Type of event
        pub event_type: RatelimitEventType,
    }

    /// Event type
    pub enum RatelimitEventType {
        DiscriminatorChange,
    }
);

impl fmt::Display for RatelimitEventType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

#[allow(clippy::disallowed_methods)]
impl RatelimitEvent {
    /// Create ratelimit event
    pub async fn create(
        db: &Database,
        target_id: String,
        event_type: RatelimitEventType,
    ) -> Result<()> {
        db.insert_ratelimit_event(&RatelimitEvent {
            id: Ulid::new().to_string(),
            target_id,
            event_type,
        })
        .await
    }
}