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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::prelude::*;
use rive_models::{
    message::{BulkMessageResponse, Message},
    payload::{
        BulkDeleteMessagesPayload, EditMessagePayload, FetchMessagesPayload,
        SearchForMessagesPayload, SendMessagePayload,
    },
};

impl Client {
    /// Lets the server and all other clients know that we've seen this message id in this channel.
    pub async fn acknowledge_message(
        &self,
        channel_id: impl Into<String>,
        message_id: impl Into<String>,
    ) -> Result<()> {
        self.client
            .put(ep!(
                self,
                "/channels/{}/ack/{}",
                channel_id.into(),
                message_id.into()
            ))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    /// Fetch multiple messages.
    pub async fn fetch_messages(
        &self,
        channel_id: impl Into<String>,
        payload: FetchMessagesPayload,
    ) -> Result<BulkMessageResponse> {
        Ok(self
            .client
            .get(ep!(self, "/channels/{}/messages", channel_id.into()))
            .auth(&self.authentication)
            .query(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Send a message to a given channel.
    pub async fn send_message(
        &self,
        channel_id: impl Into<String>,
        payload: SendMessagePayload,
    ) -> Result<Message> {
        Ok(self
            .client
            .post(ep!(self, "/channels/{}/messages", channel_id.into()))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Search for messages within the given parameters.
    pub async fn search_for_messages(
        &self,
        channel_id: impl Into<String>,
        payload: SearchForMessagesPayload,
    ) -> Result<Message> {
        Ok(self
            .client
            .post(ep!(self, "/channels/{}/messages/search", channel_id.into()))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Retrieves a message by its ID.
    pub async fn fetch_message(
        &self,
        channel_id: impl Into<String>,
        message_id: impl Into<String>,
    ) -> Result<Message> {
        Ok(self
            .client
            .get(ep!(
                self,
                "/channels/{}/messages/{}",
                channel_id.into(),
                message_id.into()
            ))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Delete a message you've sent or one you have permission to delete.
    pub async fn delete_message(
        &self,
        channel_id: impl Into<String>,
        message_id: impl Into<String>,
    ) -> Result<()> {
        self.client
            .delete(ep!(
                self,
                "/channels/{}/messages/{}",
                channel_id.into(),
                message_id.into()
            ))
            .auth(&self.authentication)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }

    pub async fn edit_message(
        &self,
        channel_id: impl Into<String>,
        message_id: impl Into<String>,
        payload: EditMessagePayload,
    ) -> Result<Message> {
        Ok(self
            .client
            .patch(ep!(
                self,
                "/channels/{}/messages/{}",
                channel_id.into(),
                message_id.into()
            ))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?
            .json()
            .await?)
    }

    /// Delete multiple messages you've sent or one you have permission to delete.
    ///
    /// This will always require ManageMessages permission regardless of whether you own the message or not.
    ///
    /// Messages must have been sent within the past 1 week.
    pub async fn bulk_delete_messages(
        &self,
        channel_id: impl Into<String>,
        payload: BulkDeleteMessagesPayload,
    ) -> Result<()> {
        self.client
            .delete(ep!(self, "/channels/{}/messages/bulk", channel_id.into(),))
            .auth(&self.authentication)
            .json(&payload)
            .send()
            .await?
            .process_error()
            .await?;
        Ok(())
    }
}