chorus 0.20.0

A library for interacting with multiple Spacebar-compatible Instances at once.
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use futures_util::SinkExt;
use log::*;

use std::fmt::Debug;

use super::{events::Events, *};
use crate::types::{self, Composite, GuildMembersChunk, Opcode, Shared, VoiceStateUpdate};

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

/// Represents a handle to a Gateway connection.
///
/// A Gateway connection will create observable [`Events`], which you can subscribe to.
///
/// Using this handle you can also send Gateway Events directly.
#[derive(Debug, Clone)]
pub struct GatewayHandle {
    pub url: String,
    pub events: Arc<Mutex<Events>>,
    pub websocket_send: Arc<Mutex<Sink>>,
    /// Tells gateway tasks to close
    pub(super) kill_send: tokio::sync::broadcast::Sender<()>,
    pub(crate) store: Arc<Mutex<HashMap<Snowflake, Arc<RwLock<ObservableObject>>>>>,
}

impl GatewayHandle {
    /// Sends json to the gateway with an opcode
    async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value) {
        let gateway_payload = types::GatewaySendPayload {
            op_code,
            event_data: Some(to_send),
            sequence_number: None,
        };

        let payload_json = serde_json::to_string(&gateway_payload).unwrap();
        let message = GatewayMessage(payload_json);

        self.websocket_send
            .lock()
            .await
            .send(message.into())
            .await
            .unwrap();
    }

    /// Recursively observes a [`Shared`] object, by making sure all [`Composite `] fields within
    /// that object and its children are being watched.
    ///
    /// Observing means, that if new information arrives about the observed object or its children,
    /// the object automatically gets updated, without you needing to request new information about
    /// the object in question from the API, which is expensive and can lead to rate limiting.
    ///
    /// The [`Shared`] object returned by this method points to a different object than the one
    /// being supplied as a &self function argument.
    pub async fn observe<T: Updateable + Clone + Debug + Composite<T>>(
        &self,
        object: Shared<T>,
    ) -> Shared<T> {
        let mut store = self.store.lock().await;
        let id = object.read().unwrap().id();
        if let Some(channel) = store.get(&id) {
            let object = channel.clone();
            drop(store);
            object
                .read()
                .unwrap()
                .downcast_ref::<T>()
                .unwrap_or_else(|| {
                    panic!(
                        "Snowflake {} already exists in the store, but it is not of type T.",
                        id
                    )
                });
            let ptr = Arc::into_raw(object.clone());
            // SAFETY:
            // - We have just checked that the typeid of the `dyn Any ...` matches that of `T`.
            // - This operation doesn't read or write any shared data, and thus cannot cause a data race
            // - The reference count is not being modified
            let downcasted = unsafe { Arc::from_raw(ptr as *const RwLock<T>).clone() };
            let object = downcasted.read().unwrap().clone();

            let watched_object = object.watch_whole(self).await;
            *downcasted.write().unwrap() = watched_object;
            downcasted
        } else {
            let id = object.read().unwrap().id();
            let object = object.read().unwrap().clone();
            let object = object.clone().watch_whole(self).await;
            let wrapped = Arc::new(RwLock::new(object));
            store.insert(id, wrapped.clone());
            wrapped
        }
    }

    /// Recursively observes and updates all updateable fields on the struct T. Returns an object `T`
    /// with all of its observable fields being observed.
    pub async fn observe_and_into_inner<T: Updateable + Clone + Debug + Composite<T>>(
        &self,
        object: Shared<T>,
    ) -> T {
        let channel = self.observe(object.clone()).await;
        let object = channel.read().unwrap().clone();
        object
    }

    /// Sends an identify event ([types::GatewayIdentifyPayload]) to the gateway
    ///
    /// Fires off a [types::GatewayReady] event
    pub async fn send_identify(&self, to_send: types::GatewayIdentifyPayload) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Identify..");

        self.send_json_event(Opcode::Identify as u8, to_send_value)
            .await;
    }

    /// Sends an identify event ([types::GatewayIdentifyPayload]) to the gateway and
    /// waits to receive a [types::GatewayReady] event.
    ///
    /// Returns [GatewayError::NoResponse] if the server sends no response after 5 seconds of
    /// waiting
    pub async fn identify(
        &self,
        to_send: types::GatewayIdentifyPayload,
    ) -> Result<types::GatewayReady, GatewayError> {
        self.send_identify(to_send).await;

        let (observer, receiver) = OneshotEventObserver::<types::GatewayReady>::new();

        self.events
            .lock()
            .await
            .session
            .ready
            .subscribe(observer.clone());

        tokio::select! {
              () = sleep(std::time::Duration::from_secs(5)) => {
                  // Timeout
                  self.events.lock().await.session.ready.unsubscribe(observer);
                  Err(GatewayError::NoResponse)
              }
              result = receiver => {
                  match result {
                      Ok(event) => {
                          self.events.lock().await.session.ready.unsubscribe(observer);
                          Ok(event)
                      }
                      Err(e) => {
                          warn!("Gateway in-place-events receive error: {:?}", e);
                          self.events.lock().await.session.ready.unsubscribe(observer);
                          Err(GatewayError::Unknown)
                      }
                  }
              }
        }
    }

    /// Sends a resume event ([types::GatewayResume]) to the gateway
    ///
    /// Fires off a [types::GatewayResumed] event after replaying missed events
    pub async fn send_resume(&self, to_send: types::GatewayResume) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Resume..");

        self.send_json_event(Opcode::Resume as u8, to_send_value)
            .await;
    }

    /// Sends a resume event ([types::GatewayResume]) to the gateway and
    /// waits to receive a [types::GatewayResumed] event.
    ///
    /// Returns [GatewayError::NoResponse] if the server sends no response after 5 seconds of
    /// waiting
    pub async fn resume(
        &self,
        to_send: types::GatewayResume,
    ) -> Result<types::GatewayResumed, GatewayError> {
        self.send_resume(to_send).await;

        let (observer, receiver) = OneshotEventObserver::<types::GatewayResumed>::new();

        self.events
            .lock()
            .await
            .session
            .resumed
            .subscribe(observer.clone());

        tokio::select! {
              () = sleep(std::time::Duration::from_secs(5)) => {
                  // Timeout
                  self.events.lock().await.session.resumed.unsubscribe(observer);
                  Err(GatewayError::NoResponse)
              }
              result = receiver => {
                  match result {
                      Ok(event) => {
                          self.events.lock().await.session.resumed.unsubscribe(observer);
                          Ok(event)
                      }
                      Err(e) => {
                          warn!("Gateway in-place-events receive error: {:?}", e);
                          self.events.lock().await.session.resumed.unsubscribe(observer);
                          Err(GatewayError::Unknown)
                      }
                  }
              }
        }
    }

    /// Sends an update presence event ([types::UpdatePresence]) to the gateway
    pub async fn send_update_presence(&self, to_send: types::UpdatePresence) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Update Presence..");

        self.send_json_event(Opcode::PresenceUpdate as u8, to_send_value)
            .await;
    }

    /// Sends a request guild members ([types::GatewayRequestGuildMembers]) event to the server
    ///
    /// Fires off one or more [types::GuildMembersChunk]
    pub async fn send_request_guild_members(&self, to_send: types::GatewayRequestGuildMembers) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Request Guild Members..");

        self.send_json_event(Opcode::RequestGuildMembers as u8, to_send_value)
            .await;
    }

    /// Sends a request guild members ([types::GatewayRequestGuildMembers]) event to the server and
    /// waits to receive all [types::GuildMembersChunk]s
    ///
    /// Returns [GatewayError::NoResponse] if the server sends no response after 5 seconds of
    /// waiting
    pub async fn request_guild_members(
        &self,
        to_send: types::GatewayRequestGuildMembers,
    ) -> Result<Vec<GuildMembersChunk>, GatewayError> {
        self.send_request_guild_members(to_send).await;

        let (observer, mut receiver) = BroadcastEventObserver::<GuildMembersChunk>::new(32);

        self.events
            .lock()
            .await
            .guild
            .members_chunk
            .subscribe(observer.clone());

        let mut chunks = Vec::new();

        loop {
            tokio::select! {
                  () = sleep(std::time::Duration::from_secs(5)) => {
                      // Timeout
                      self.events.lock().await.guild.members_chunk.unsubscribe(observer);
                      return Err(GatewayError::NoResponse);
                  }
                  result = receiver.recv() => {
                      match result {
                          Ok(event) => {
                              let remaining = event.chunk_count - (event.chunk_index + 1);

                              chunks.push(event);

                              if remaining < 1 {
                                  self.events.lock().await.guild.members_chunk.unsubscribe(observer);
                                  return Ok(chunks);
                              }
                          }
                          Err(e) => {
                              warn!("Gateway in-place-events receive error: {:?}", e);
                              self.events.lock().await.guild.members_chunk.unsubscribe(observer);
                              return Err(GatewayError::Unknown);
                          }
                      }
                  }
            }
        }
    }

    /// Sends an update voice state ([types::UpdateVoiceState]) event to the server
    ///
    /// Fires a [types::VoiceStateUpdate] event if the user left or joined a different channel
    pub async fn send_update_voice_state(&self, to_send: types::UpdateVoiceState) {
        let to_send_value = serde_json::to_value(to_send).unwrap();

        trace!("GW: Sending Update Voice State..");

        self.send_json_event(Opcode::VoiceStateUpdate as u8, to_send_value)
            .await;
    }

    /// Sends an update voice state ([types::UpdateVoiceState]) event to the server and
    /// waits to receive a [types::VoiceStateUpdate] event
    ///
    /// Returns [None] if the server sends no response after a second of
    /// waiting
    ///
    /// Note that not receiving a response is normal behaviour if the user didn't leave or join a
    /// new voice channel
    pub async fn update_voice_state(
        &self,
        to_send: types::UpdateVoiceState,
    ) -> Option<VoiceStateUpdate> {
        self.send_update_voice_state(to_send).await;

        let (observer, receiver) = OneshotEventObserver::<VoiceStateUpdate>::new();

        self.events
            .lock()
            .await
            .voice
            .state_update
            .subscribe(observer.clone());

        tokio::select! {
              () = sleep(std::time::Duration::from_secs(1)) => {
                  // Timeout
                  self.events.lock().await.voice.state_update.unsubscribe(observer);
                  None
              }
              result = receiver => {
                  match result {
                      Ok(event) => {
                          self.events.lock().await.voice.state_update.unsubscribe(observer);
                          Some(event)
                      }
                      Err(e) => {
                          warn!("Gateway in-place-events receive error: {:?}", e);
                          self.events.lock().await.voice.state_update.unsubscribe(observer);
                          None
                      }
                  }
              }
        }
    }

    /// Sends a call sync ([types::CallSync]) to the server
    pub async fn send_call_sync(&self, to_send: types::CallSync) {
        let to_send_value = serde_json::to_value(to_send).unwrap();

        trace!("GW: Sending Call Sync..");

        self.send_json_event(Opcode::CallConnect as u8, to_send_value)
            .await;
    }

    /// Sends a request call connect event (aka [types::CallSync]) to the server
    ///
    /// # Notes
    /// Alias of [Self::send_call_sync]
    pub async fn send_request_call_connect(&self, to_send: types::CallSync) {
        self.send_call_sync(to_send).await
    }

    /// Sends a Lazy Request ([types::LazyRequest]) to the server
    pub async fn send_lazy_request(&self, to_send: types::LazyRequest) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Lazy Request..");

        self.send_json_event(Opcode::GuildSubscriptions as u8, to_send_value)
            .await;
    }

    /// Sends a Request Last Messages ([types::RequestLastMessages]) to the server
    ///
    /// Fires off a [types::LastMessages] event
    pub async fn send_request_last_messages(&self, to_send: types::RequestLastMessages) {
        let to_send_value = serde_json::to_value(&to_send).unwrap();

        trace!("GW: Sending Request Last Messages..");

        self.send_json_event(Opcode::RequestLastMessages as u8, to_send_value)
            .await;
    }

    /// Sends a Request Last Messages ([types::RequestLastMessages]) event to the server and
    /// waits to receive a [types::LastMessages] event
    ///
    /// Returns [None] if the server sends no response after 5 seconds of
    /// waiting
    pub async fn request_last_messages(
        &self,
        to_send: types::RequestLastMessages,
    ) -> Result<types::LastMessages, GatewayError> {
        self.send_request_last_messages(to_send).await;

        let (observer, receiver) = OneshotEventObserver::<types::LastMessages>::new();

        self.events
            .lock()
            .await
            .message
            .last_messages
            .subscribe(observer.clone());

        tokio::select! {
              () = sleep(std::time::Duration::from_secs(5)) => {
                  // Timeout
                  self.events.lock().await.message.last_messages.unsubscribe(observer);
                  Err(GatewayError::NoResponse)
              }
              result = receiver => {
                  match result {
                      Ok(event) => {
                          self.events.lock().await.message.last_messages.unsubscribe(observer);
                          Ok(event)
                      }
                      Err(e) => {
                          warn!("Gateway in-place-events receive error: {:?}", e);
                          self.events.lock().await.message.last_messages.unsubscribe(observer);
                          Err(GatewayError::Unknown)
                      }
                  }
              }
        }
    }

    /// Closes the websocket connection and stops all gateway tasks.
    ///
    /// Essentially pulls the plug on the gateway, leaving it possible to resume
    pub async fn close(&self) {
        self.kill_send.send(()).unwrap();
        self.websocket_send.lock().await.close().await.unwrap();
    }
}