diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-09 10:44:43 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-09 10:44:43 -0700 |
| commit | c74cc15f8969c8db68119d07a4f273a0d3fc44f4 (patch) | |
| tree | a37a4989a4af4b6d78fe1d40a62a329ae336578c /src/ext | |
| parent | Remove selfbot support (diff) | |
| download | serenity-c74cc15f8969c8db68119d07a4f273a0d3fc44f4.tar.xz serenity-c74cc15f8969c8db68119d07a4f273a0d3fc44f4.zip | |
Remove support for group calls and guild sync
Calls and guild sync are essentially leftovers from selfbot support
removal, the former moreso.
Removes the following `model::event` structs:
- CallCreateEvent
- CallDeleteEvent
- CallUpdateEvent
- GuildSyncEvent
which also removes the following `model::event::Event` variants:
`client::gateway::Shard::sync_calls` has been removed.
The following `client::Client` methods have been removed:
- `on_call_create`
- `on_call_delete`
- `on_call_update`
- `on_guild_sync`
Removes the following items on `ext::cache::Cache`:
```
ext::cache::Cache::{
// fields
calls,
// methods
get_call,
update_with_call_create,
update_with_call_delete,
update_with_call_update,
update_with_guild_sync,
}
```
Voice structs and methods now take solely a `guild_id` instead of a
`target`, due to the handling of 1-on-1 and group calls being removed.
This continues off commit d9118c0.<Paste>
Diffstat (limited to 'src/ext')
| -rw-r--r-- | src/ext/cache/mod.rs | 103 | ||||
| -rw-r--r-- | src/ext/voice/connection_info.rs | 4 | ||||
| -rw-r--r-- | src/ext/voice/handler.rs | 51 | ||||
| -rw-r--r-- | src/ext/voice/manager.rs | 72 | ||||
| -rw-r--r-- | src/ext/voice/mod.rs | 31 | ||||
| -rw-r--r-- | src/ext/voice/payload.rs | 2 | ||||
| -rw-r--r-- | src/ext/voice/threading.rs | 12 |
7 files changed, 55 insertions, 220 deletions
diff --git a/src/ext/cache/mod.rs b/src/ext/cache/mod.rs index 7bb9dd8..ee26816 100644 --- a/src/ext/cache/mod.rs +++ b/src/ext/cache/mod.rs @@ -49,7 +49,6 @@ //! while needing to hit the REST API as little as possible, then the answer //! is "yes". //! -//! [`Call`]: ../../model/struct.Call.html //! [`Context`]: ../../client/struct.Context.html //! [`Context::get_channel`]: ../../client/struct.Context.html#method.get_channel //! [`Emoji`]: ../../model/struct.Emoji.html @@ -103,16 +102,6 @@ use ::model::event::*; /// [`rest`]: ../../client/rest/index.html #[derive(Clone, Debug)] pub struct Cache { - /// A map of the currently active calls that the current user knows about, - /// where the key is the Id of the [`PrivateChannel`] or [`Group`] hosting - /// the call. - /// - /// For bot users this will always be empty, except for in [special cases]. - /// - /// [`Group`]: ../../model/struct.Group.html - /// [`PrivateChannel`]: ../../model/struct.PrivateChannel.html - /// [special cases]: index.html#special-cases-in-the-cache - pub calls: HashMap<ChannelId, Arc<RwLock<Call>>>, /// A map of channels in [`Guild`]s that the current user has received data /// for. /// @@ -264,16 +253,6 @@ impl Cache { .collect() } - /// Retrieves a reference to a [`Call`] from the cache based on the - /// associated [`Group`]'s channel Id. - /// - /// [`Call`]: ../../model/struct.Call.html - /// [`Group`]: ../../model/struct.Group.html - #[inline] - pub fn get_call<C: Into<ChannelId>>(&self, group_id: C) -> Option<Arc<RwLock<Call>>> { - self.calls.get(&group_id.into()).cloned() - } - /// Retrieves a [`Channel`] from the cache based on the given Id. /// /// This will search the [`channels`] map, the [`private_channels`] map, and @@ -465,45 +444,6 @@ impl Cache { } #[doc(hidden)] - pub fn update_with_call_create(&mut self, event: &CallCreateEvent) { - match self.calls.entry(event.call.channel_id) { - Entry::Vacant(e) => { - e.insert(Arc::new(RwLock::new(event.call.clone()))); - }, - Entry::Occupied(mut e) => { - *e.get_mut() = Arc::new(RwLock::new(event.call.clone())); - }, - } - } - - #[doc(hidden)] - pub fn update_with_call_delete(&mut self, event: &CallDeleteEvent) - -> Option<Arc<RwLock<Call>>> { - self.calls.remove(&event.channel_id) - } - - #[doc(hidden)] - pub fn update_with_call_update(&mut self, event: &CallUpdateEvent, old: bool) - -> Option<Arc<RwLock<Call>>> { - let item = if old { - self.calls.get(&event.channel_id).cloned() - } else { - None - }; - - self.calls - .get_mut(&event.channel_id) - .map(|call| { - let mut call = call.write().unwrap(); - - call.region.clone_from(&event.region); - call.ringing.clone_from(&event.ringing); - }); - - item - } - - #[doc(hidden)] pub fn update_with_channel_create(&mut self, event: &ChannelCreateEvent) -> Option<Channel> { match event.channel { Channel::Group(ref group) => { @@ -833,23 +773,6 @@ impl Cache { } #[doc(hidden)] - pub fn update_with_guild_sync(&mut self, event: &GuildSyncEvent) { - for member in event.members.values() { - self.update_user_entry(&member.user.read().unwrap()); - } - - self.guilds - .get_mut(&event.guild_id) - .map(|guild| { - let mut guild = guild.write().unwrap(); - - guild.large = event.large; - guild.members.clone_from(&event.members); - guild.presences.clone_from(&event.presences); - }); - } - - #[doc(hidden)] pub fn update_with_guild_unavailable(&mut self, event: &GuildUnavailableEvent) { self.unavailable_guilds.insert(event.guild_id); self.guilds.remove(&event.guild_id); @@ -988,31 +911,6 @@ impl Cache { return; } - - if let Some(channel) = event.voice_state.channel_id { - // channel id available, insert voice state - if let Some(call) = self.calls.get_mut(&channel) { - let mut call = call.write().unwrap(); - - { - let finding = call.voice_states - .get_mut(&event.voice_state.user_id); - - if let Some(group_state) = finding { - group_state.clone_from(&event.voice_state); - - return; - } - } - - call.voice_states.insert(event.voice_state.user_id, event.voice_state.clone()); - } - } else { - // delete this user from any group call containing them - for call in self.calls.values_mut() { - call.write().unwrap().voice_states.remove(&event.voice_state.user_id); - } - } } // Adds or updates a user entry in the [`users`] map with a received user. @@ -1033,7 +931,6 @@ impl Cache { impl Default for Cache { fn default() -> Cache { Cache { - calls: HashMap::default(), channels: HashMap::default(), groups: HashMap::default(), guilds: HashMap::default(), diff --git a/src/ext/voice/connection_info.rs b/src/ext/voice/connection_info.rs index 282b5f1..d0364ce 100644 --- a/src/ext/voice/connection_info.rs +++ b/src/ext/voice/connection_info.rs @@ -1,10 +1,10 @@ -use ::model::UserId; +use ::model::{GuildId, UserId}; #[derive(Clone, Debug)] pub struct ConnectionInfo { pub endpoint: String, + pub guild_id: GuildId, pub session_id: String, - pub target_id: u64, pub token: String, pub user_id: UserId, } diff --git a/src/ext/voice/handler.rs b/src/ext/voice/handler.rs index 0ef518a..cb5838c 100644 --- a/src/ext/voice/handler.rs +++ b/src/ext/voice/handler.rs @@ -2,7 +2,7 @@ use serde_json::builder::ObjectBuilder; use std::sync::mpsc::{self, Sender as MpscSender}; use super::{AudioReceiver, AudioSource}; use super::connection_info::ConnectionInfo; -use super::{Status as VoiceStatus, Target}; +use super::Status as VoiceStatus; use ::client::gateway::GatewayStatus; use ::constants::VoiceOpCode; use ::model::{ChannelId, GuildId, UserId, VoiceState}; @@ -59,7 +59,7 @@ pub struct Handler { /// /// [`Call`]: ../../model/struct.Call.html /// [`Group`]: ../../model/struct.Group.html - pub guild_id: Option<GuildId>, + pub guild_id: GuildId, /// Whether the current handler is set to deafen voice connections. /// /// **Note**: This _must not_ be manually mutated. Call [`deafen`] to @@ -114,8 +114,8 @@ impl Handler { /// [`Manager::join`]: struct.Manager.html#method.join #[doc(hidden)] #[inline] - pub fn new(target: Target, ws: MpscSender<GatewayStatus>, user_id: UserId) -> Self { - Self::new_raw(target, Some(ws), user_id) + pub fn new(guild_id: GuildId, ws: MpscSender<GatewayStatus>, user_id: UserId) -> Self { + Self::new_raw(guild_id, Some(ws), user_id) } /// Creates a new, standalone Handler which is not connected to the primary @@ -128,8 +128,8 @@ impl Handler { /// For most use cases you do not want this. Only use it if you are using /// the voice component standalone from the rest of the library. #[inline] - pub fn standalone(target: Target, user_id: UserId) -> Self { - Self::new_raw(target, None, user_id) + pub fn standalone(guild_id: GuildId, user_id: UserId) -> Self { + Self::new_raw(guild_id, None, user_id) } /// Connects to the voice channel if the following are present: @@ -154,29 +154,19 @@ impl Handler { return false; } - let target_id = if let Some(guild_id) = self.guild_id { - guild_id.0 - } else if let Some(channel_id) = self.channel_id { - channel_id.0 - } else { - // Theoretically never happens? This needs to be researched more. - error!("(╯°□°)╯︵ ┻━┻ No guild/channel ID when connecting"); - - return false; - }; - - // Safe as all of these being present was already checked. let endpoint = self.endpoint.clone().unwrap(); + let guild_id = self.guild_id; let session_id = self.session_id.clone().unwrap(); let token = self.token.clone().unwrap(); let user_id = self.user_id; + // Safe as all of these being present was already checked. self.send(VoiceStatus::Connect(ConnectionInfo { - endpoint: endpoint, - session_id: session_id, - target_id: target_id, - token: token, - user_id: user_id, + endpoint, + guild_id, + session_id, + token, + user_id, })); true @@ -368,18 +358,13 @@ impl Handler { } } - fn new_raw(target: Target, ws: Option<MpscSender<GatewayStatus>>, user_id: UserId) -> Self { + fn new_raw(guild_id: GuildId, ws: Option<MpscSender<GatewayStatus>>, user_id: UserId) -> Self { let (tx, rx) = mpsc::channel(); - let (channel_id, guild_id) = match target { - Target::Channel(channel_id) => (Some(channel_id), None), - Target::Guild(guild_id) => (None, Some(guild_id)), - }; - - threading::start(target, rx); + threading::start(guild_id, rx); Handler { - channel_id: channel_id, + channel_id: None, endpoint: None, guild_id: guild_id, self_deaf: false, @@ -401,7 +386,7 @@ impl Handler { self.sender = tx; self.sender.send(status).unwrap(); - threading::start(Target::Guild(self.guild_id.unwrap()), rx); + threading::start(self.guild_id, rx); self.update(); } @@ -428,7 +413,7 @@ impl Handler { .insert("op", VoiceOpCode::SessionDescription.num()) .insert_object("d", |o| o .insert("channel_id", self.channel_id.map(|c| c.0)) - .insert("guild_id", self.guild_id.map(|g| g.0)) + .insert("guild_id", self.guild_id.0) .insert("self_deaf", self.self_deaf) .insert("self_mute", self.self_mute)) .build(); diff --git a/src/ext/voice/manager.rs b/src/ext/voice/manager.rs index 44530c1..4ba7bfb 100644 --- a/src/ext/voice/manager.rs +++ b/src/ext/voice/manager.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use std::sync::mpsc::Sender as MpscSender; -use super::{Handler, Target}; +use super::Handler; use ::client::gateway::GatewayStatus; use ::model::{ChannelId, GuildId, UserId}; @@ -21,7 +21,7 @@ use ::model::{ChannelId, GuildId, UserId}; /// [guild's channel]: ../../model/enum.ChannelType.html#variant.Voice /// [WebSocket connection]: ../../client/struct.Connection.html pub struct Manager { - handlers: HashMap<Target, Handler>, + handlers: HashMap<GuildId, Handler>, user_id: UserId, ws: MpscSender<GatewayStatus>, } @@ -37,8 +37,8 @@ impl Manager { } /// Retrieves a mutable handler for the given target, if one exists. - pub fn get<T: Into<Target>>(&mut self, target_id: T) -> Option<&mut Handler> { - self.handlers.get_mut(&target_id.into()) + pub fn get<G: Into<GuildId>>(&mut self, guild_id: G) -> Option<&mut Handler> { + self.handlers.get_mut(&guild_id.into()) } /// Connects to a target by retrieving its relevant [`Handler`] and @@ -63,45 +63,33 @@ impl Manager { /// [`Handler`]: struct.Handler.html /// [`get`]: #method.get #[allow(map_entry)] - pub fn join(&mut self, guild_id: Option<GuildId>, channel_id: ChannelId) -> &mut Handler { - if let Some(guild_id) = guild_id { - let target = Target::Guild(guild_id); + pub fn join<C, G>(&mut self, guild_id: G, channel_id: C) -> &mut Handler + where C: Into<ChannelId>, G: Into<GuildId> { + let channel_id = channel_id.into(); + let guild_id = guild_id.into(); - { - let mut found = false; + { + let mut found = false; - if let Some(handler) = self.handlers.get_mut(&target) { - handler.switch_to(channel_id); + if let Some(handler) = self.handlers.get_mut(&guild_id) { + handler.switch_to(channel_id); - found = true; - } - - if found { - // Actually safe, as the key has already been found above. - return self.handlers.get_mut(&target).unwrap(); - } + found = true; } - let mut handler = Handler::new(target, self.ws.clone(), self.user_id); - handler.join(channel_id); - - self.handlers.insert(target, handler); - - // Actually safe, as the key would have been inserted above. - self.handlers.get_mut(&target).unwrap() - } else { - let target = Target::Channel(channel_id); + if found { + // Actually safe, as the key has already been found above. + return self.handlers.get_mut(&guild_id).unwrap(); + } + } - if !self.handlers.contains_key(&target) { - let mut handler = Handler::new(target, self.ws.clone(), self.user_id); - handler.join(channel_id); + let mut handler = Handler::new(guild_id, self.ws.clone(), self.user_id); + handler.join(channel_id); - self.handlers.insert(target, handler); - } + self.handlers.insert(guild_id, handler); - // Actually safe, as the key would have been inserted above. - self.handlers.get_mut(&target).unwrap() - } + // Actually safe, as the key would have been inserted above. + self.handlers.get_mut(&guild_id).unwrap() } /// Retrieves the [handler][`Handler`] for the given target and leaves the @@ -115,10 +103,8 @@ impl Manager { /// [`Handler`]: struct.Handler.html /// [`get`]: #method.get /// [`leave`]: struct.Handler.html#method.leave - pub fn leave<T: Into<Target>>(&mut self, target_id: T) { - let target = target_id.into(); - - if let Some(handler) = self.handlers.get_mut(&target) { + pub fn leave<G: Into<GuildId>>(&mut self, guild_id: G) { + if let Some(handler) = self.handlers.get_mut(&guild_id.into()) { handler.leave(); } } @@ -129,11 +115,11 @@ impl Manager { /// The handler is then dropped, removing settings for the target. /// /// [`Handler`]: struct.Handler.html - pub fn remove<T: Into<Target>>(&mut self, target_id: T) { - let target = target_id.into(); + pub fn remove<G: Into<GuildId>>(&mut self, guild_id: G) { + let guild_id = guild_id.into(); - self.leave(target); + self.leave(guild_id); - self.handlers.remove(&target); + self.handlers.remove(&guild_id); } } diff --git a/src/ext/voice/mod.rs b/src/ext/voice/mod.rs index 9ccb1d9..94e3b40 100644 --- a/src/ext/voice/mod.rs +++ b/src/ext/voice/mod.rs @@ -17,7 +17,6 @@ pub use self::manager::Manager; pub use self::streamer::{ffmpeg, pcm, ytdl}; use self::connection_info::ConnectionInfo; -use ::model::{ChannelId, GuildId}; const CRYPTO_MODE: &'static str = "xsalsa20_poly1305"; @@ -28,33 +27,3 @@ pub enum Status { SetReceiver(Option<Box<AudioReceiver>>), SetSender(Option<Box<AudioSource>>), } - -/// Denotes the target to manage a connection for. -/// -/// For most cases, targets should entirely be guilds, except for the one case -/// where a user account can be in a 1-to-1 or group call. -/// -/// It _may_ be possible in the future for bots to be in multiple groups. If -/// this turns out to be the case, supporting that now rather than messily in -/// the future is the best option. Thus, these types of calls are specified by -/// the group's channel Id. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] -pub enum Target { - /// Used for managing a voice handler for a 1-on-1 (user-to-user) or group - /// call. - Channel(ChannelId), - /// Used for managing a voice handler for a guild. - Guild(GuildId), -} - -impl From<ChannelId> for Target { - fn from(channel_id: ChannelId) -> Target { - Target::Channel(channel_id) - } -} - -impl From<GuildId> for Target { - fn from(guild_id: GuildId) -> Target { - Target::Guild(guild_id) - } -} diff --git a/src/ext/voice/payload.rs b/src/ext/voice/payload.rs index 5086b08..964d6d2 100644 --- a/src/ext/voice/payload.rs +++ b/src/ext/voice/payload.rs @@ -8,7 +8,7 @@ pub fn build_identify(info: &ConnectionInfo) -> Value { ObjectBuilder::new() .insert("op", VoiceOpCode::Identify.num()) .insert_object("d", |o| o - .insert("server_id", info.target_id) + .insert("server_id", info.guild_id.0) .insert("session_id", &info.session_id) .insert("token", &info.token) .insert("user_id", info.user_id.0)) diff --git a/src/ext/voice/threading.rs b/src/ext/voice/threading.rs index 0aaafba..3986f48 100644 --- a/src/ext/voice/threading.rs +++ b/src/ext/voice/threading.rs @@ -1,19 +1,17 @@ use std::sync::mpsc::{Receiver as MpscReceiver, TryRecvError}; use std::thread::Builder as ThreadBuilder; use super::connection::Connection; -use super::{Status, Target}; +use super::Status; use ::internal::Timer; +use ::model::GuildId; -pub fn start(target_id: Target, rx: MpscReceiver<Status>) { - let name = match target_id { - Target::Channel(channel_id) => format!("Serenity Voice (C{})", channel_id), - Target::Guild(guild_id) => format!("Serenity Voice (G{})", guild_id), - }; +pub fn start(guild_id: GuildId, rx: MpscReceiver<Status>) { + let name = format!("Serenity Voice (G{})", guild_id); ThreadBuilder::new() .name(name) .spawn(move || runner(rx)) - .expect(&format!("[Voice] Error starting target: {:?}", target_id)); + .expect(&format!("[Voice] Error starting guild: {:?}", guild_id)); } fn runner(rx: MpscReceiver<Status>) { |