Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models: Implement stage channels #793

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cache/in-memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@ impl InMemoryCache {
GuildChannel::Voice(ref mut c) => {
c.guild_id.replace(guild_id);
}
GuildChannel::Stage(ref mut c) => {
c.guild_id.replace(guild_id);
}
}

let id = channel.id();
Expand Down Expand Up @@ -1074,6 +1077,7 @@ mod tests {
suppress: false,
token: None,
user_id,
request_to_speak_timestamp: Some("2021-04-21T22:16:50+0000".to_owned()),
}
}

Expand Down
2 changes: 2 additions & 0 deletions cache/in-memory/src/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ mod tests {
suppress: false,
token: None,
user_id: UserId(1),
request_to_speak_timestamp: Some("2021-04-21T22:16:50+0000".to_owned()),
}));
}

Expand Down Expand Up @@ -1084,6 +1085,7 @@ mod tests {
suppress: false,
token: None,
user_id: UserId(3),
request_to_speak_timestamp: Some("2021-04-21T22:16:50+0000".to_owned()),
});

cache.update(&mutation);
Expand Down
47 changes: 43 additions & 4 deletions model/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub enum GuildChannel {
Category(CategoryChannel),
Text(TextChannel),
Voice(VoiceChannel),
Stage(VoiceChannel),
}

impl GuildChannel {
Expand All @@ -94,6 +95,7 @@ impl GuildChannel {
Self::Category(category) => category.guild_id,
Self::Text(text) => text.guild_id,
Self::Voice(voice) => voice.guild_id,
Self::Stage(stage) => stage.guild_id,
}
}

Expand All @@ -103,6 +105,7 @@ impl GuildChannel {
Self::Category(category) => category.id,
Self::Text(text) => text.id,
Self::Voice(voice) => voice.id,
Self::Stage(stage) => stage.id,
}
}

Expand All @@ -112,6 +115,7 @@ impl GuildChannel {
Self::Category(category) => category.name.as_ref(),
Self::Text(text) => text.name.as_ref(),
Self::Voice(voice) => voice.name.as_ref(),
Self::Stage(stage) => stage.name.as_ref(),
}
}
}
Expand Down Expand Up @@ -345,11 +349,10 @@ impl<'de> Visitor<'de> for GuildChannelVisitor {
let user_limit = user_limit.ok_or_else(|| DeError::missing_field("user_limit"))?;

tracing::trace!(%bitrate, ?user_limit, "handling voice channel");

GuildChannel::Voice(VoiceChannel {
let voice_channel = VoiceChannel {
id,
bitrate,
guild_id,
id,
kind,
name,
parent_id,
Expand All @@ -358,7 +361,13 @@ impl<'de> Visitor<'de> for GuildChannelVisitor {
rtc_region: None,
user_limit,
video_quality_mode,
})
};

if kind == ChannelType::GuildVoice {
GuildChannel::Voice(voice_channel)
} else {
GuildChannel::Stage(voice_channel)
}
}
ChannelType::GuildNews | ChannelType::GuildStore | ChannelType::GuildText => {
let last_message_id = last_message_id.unwrap_or_default();
Expand Down Expand Up @@ -467,6 +476,22 @@ mod tests {
}
}

fn guild_stage() -> VoiceChannel {
VoiceChannel {
bitrate: 1000,
guild_id: Some(GuildId(321)),
id: ChannelId(789),
kind: ChannelType::GuildStageVoice,
name: "stage".to_owned(),
permission_overwrites: Vec::new(),
parent_id: None,
position: 2,
rtc_region: None,
user_limit: None,
video_quality_mode: None,
}
}

fn private() -> PrivateChannel {
PrivateChannel {
id: ChannelId(234),
Expand All @@ -492,6 +517,10 @@ mod tests {
Channel::Guild(GuildChannel::Voice(guild_voice())).id(),
ChannelId(789)
);
assert_eq!(
Channel::Guild(GuildChannel::Stage(guild_stage())).id(),
ChannelId(789)
);
assert_eq!(Channel::Private(private()).id(), ChannelId(234));
}

Expand All @@ -513,6 +542,10 @@ mod tests {
Channel::Guild(GuildChannel::Voice(guild_voice())).name(),
Some("voice")
);
assert_eq!(
Channel::Guild(GuildChannel::Stage(guild_stage())).name(),
Some("stage")
);
assert!(Channel::Private(private()).name().is_none());
}

Expand All @@ -530,6 +563,10 @@ mod tests {
GuildChannel::Voice(guild_voice()).guild_id(),
Some(GuildId(321))
);
assert_eq!(
GuildChannel::Stage(guild_stage()).guild_id(),
Some(GuildId(321))
);
}

#[test]
Expand All @@ -540,13 +577,15 @@ mod tests {
);
assert_eq!(GuildChannel::Text(guild_text()).id(), ChannelId(456));
assert_eq!(GuildChannel::Voice(guild_voice()).id(), ChannelId(789));
assert_eq!(GuildChannel::Stage(guild_stage()).id(), ChannelId(789));
}

#[test]
fn test_guild_channel_name() {
assert_eq!(GuildChannel::Category(guild_category()).name(), "category");
assert_eq!(GuildChannel::Text(guild_text()).name(), "text");
assert_eq!(GuildChannel::Voice(guild_voice()).name(), "voice");
assert_eq!(GuildChannel::Stage(guild_stage()).name(), "stage");
}

// The deserializer for GuildChannel should skip over fields names that
Expand Down
9 changes: 8 additions & 1 deletion model/src/gateway/payload/voice_state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod tests {
suppress: false,
token: None,
user_id: UserId(1),
request_to_speak_timestamp: None,
});

serde_test::assert_tokens(
Expand All @@ -65,7 +66,7 @@ mod tests {
},
Token::Struct {
name: "VoiceState",
len: 11,
len: 12,
},
Token::Str("channel_id"),
Token::None,
Expand Down Expand Up @@ -136,6 +137,8 @@ mod tests {
Token::Str("user_id"),
Token::NewtypeStruct { name: "UserId" },
Token::Str("1"),
Token::Str("request_to_speak_timestamp"),
Token::None,
Token::StructEnd,
],
);
Expand Down Expand Up @@ -182,6 +185,7 @@ mod tests {
suppress: false,
token: None,
user_id: UserId(123_213),
request_to_speak_timestamp: Some("2021-04-21T22:16:50+0000".to_owned()),
});

// Token stream here's `Member` has no `guild_id`, which deserialiser
Expand Down Expand Up @@ -269,6 +273,9 @@ mod tests {
Token::Str("user_id"),
Token::NewtypeStruct { name: "UserId" },
Token::Str("123213"),
Token::Str("request_to_speak_timestamp"),
Token::Some,
Token::Str("2021-04-21T22:16:50+0000"),
Token::StructEnd,
],
);
Expand Down
2 changes: 1 addition & 1 deletion model/src/guild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ impl<'de> Deserialize<'de> for Guild {
GuildChannel::Text(c) => {
c.guild_id.replace(id);
}
GuildChannel::Voice(c) => {
GuildChannel::Voice(c) | GuildChannel::Stage(c) => {
c.guild_id.replace(id);
}
}
Expand Down
1 change: 1 addition & 0 deletions model/src/guild/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ bitflags! {
const MANAGE_ROLES = 0x1000_0000;
const MANAGE_WEBHOOKS = 0x2000_0000;
const MANAGE_EMOJIS = 0x4000_0000;
const REQUEST_TO_SPEAK = 0x10000_0000;
}
}

Expand Down
23 changes: 21 additions & 2 deletions model/src/voice/voice_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct VoiceState {
#[serde(skip_serializing_if = "Option::is_none")]
pub token: Option<String>,
pub user_id: UserId,
pub request_to_speak_timestamp: Option<String>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -45,6 +46,7 @@ enum Field {
Suppress,
Token,
UserId,
RequestToSpeakTimestamp,
}

struct VoiceStateVisitor;
Expand All @@ -70,6 +72,7 @@ impl<'de> Visitor<'de> for VoiceStateVisitor {
let mut suppress = None;
let mut token = None;
let mut user_id = None;
let mut request_to_speak_timestamp = None;

let span = tracing::trace_span!("deserializing voice state");
let _span_enter = span.enter();
Expand Down Expand Up @@ -182,6 +185,13 @@ impl<'de> Visitor<'de> for VoiceStateVisitor {

user_id = Some(map.next_value()?);
}
Field::RequestToSpeakTimestamp => {
if request_to_speak_timestamp.is_some() {
return Err(DeError::duplicate_field("request_to_speak_timestamp"));
}

request_to_speak_timestamp = map.next_value()?;
}
}
}

Expand Down Expand Up @@ -225,6 +235,7 @@ impl<'de> Visitor<'de> for VoiceStateVisitor {
suppress,
token,
user_id,
request_to_speak_timestamp,
})
}
}
Expand All @@ -244,6 +255,7 @@ impl<'de> Deserialize<'de> for VoiceState {
"suppress",
"token",
"user_id",
"request_to_speak_timestamp",
];

deserializer.deserialize_struct("VoiceState", FIELDS, VoiceStateVisitor)
Expand Down Expand Up @@ -271,14 +283,15 @@ mod tests {
suppress: true,
token: None,
user_id: UserId(3),
request_to_speak_timestamp: None,
};

serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "VoiceState",
len: 10,
len: 11,
},
Token::Str("channel_id"),
Token::Some,
Expand All @@ -305,6 +318,8 @@ mod tests {
Token::Str("user_id"),
Token::NewtypeStruct { name: "UserId" },
Token::Str("3"),
Token::Str("request_to_speak_timestamp"),
Token::None,
Token::StructEnd,
],
);
Expand Down Expand Up @@ -351,14 +366,15 @@ mod tests {
suppress: true,
token: Some("abc".to_owned()),
user_id: UserId(3),
request_to_speak_timestamp: Some("2021-04-21T22:16:50+0000".to_owned()),
};

serde_test::assert_tokens(
&value,
&[
Token::Struct {
name: "VoiceState",
len: 12,
len: 13,
},
Token::Str("channel_id"),
Token::Some,
Expand Down Expand Up @@ -437,6 +453,9 @@ mod tests {
Token::Str("user_id"),
Token::NewtypeStruct { name: "UserId" },
Token::Str("3"),
Token::Str("request_to_speak_timestamp"),
Token::Some,
Token::Str("2021-04-21T22:16:50+0000"),
Token::StructEnd,
],
);
Expand Down