From 2047dbf42097d21b595b8cd7a31232e1961f0ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= <76261501+zecakeh@users.noreply.github.com> Date: Wed, 23 Feb 2022 14:18:31 +0100 Subject: [PATCH] common: Add join_rule to directory::PublicRoomsChunk --- crates/ruma-common/src/directory.rs | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/ruma-common/src/directory.rs b/crates/ruma-common/src/directory.rs index 91a332a6..326096d1 100644 --- a/crates/ruma-common/src/directory.rs +++ b/crates/ruma-common/src/directory.rs @@ -4,7 +4,7 @@ use std::fmt; use js_int::UInt; use ruma_identifiers::{MxcUri, RoomAliasId, RoomId, RoomName}; -use ruma_serde::Outgoing; +use ruma_serde::{Outgoing, StringEnum}; use serde::{ de::{Error, MapAccess, Visitor}, ser::SerializeStruct, @@ -12,6 +12,8 @@ use serde::{ }; use serde_json::Value as JsonValue; +use crate::PrivOwnedStr; + /// A chunk of a room list response, describing one room. /// /// To create an instance of this type, first create a `PublicRoomsChunkInit` and convert it via @@ -59,6 +61,10 @@ pub struct PublicRoomsChunk { serde(default, deserialize_with = "ruma_serde::empty_string_as_none") )] pub avatar_url: Option>, + + /// The join rule of the room. + #[serde(default, skip_serializing_if = "ruma_serde::is_default")] + pub join_rule: PublicRoomJoinRule, } /// Initial set of mandatory fields of `PublicRoomsChunk`. @@ -97,6 +103,7 @@ impl From for PublicRoomsChunk { world_readable, guest_can_join, avatar_url: None, + join_rule: PublicRoomJoinRule::default(), } } } @@ -223,6 +230,37 @@ impl<'de> Visitor<'de> for RoomNetworkVisitor { } } +/// The rule used for users wishing to join a public room. +/// +/// This type can hold an arbitrary string. To check for join rules that are not available as a +/// documented variant here, use its string representation, obtained through `.as_str()`. +#[derive(Clone, Debug, PartialEq, Eq, StringEnum)] +#[ruma_enum(rename_all = "snake_case")] +#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] +pub enum PublicRoomJoinRule { + /// Users can request an invite to the room. + Knock, + + /// Anyone can join the room without any prior action. + Public, + + #[doc(hidden)] + _Custom(PrivOwnedStr), +} + +impl PublicRoomJoinRule { + /// Returns the string name of this `PublicRoomJoinRule`. + pub fn as_str(&self) -> &str { + self.as_ref() + } +} + +impl Default for PublicRoomJoinRule { + fn default() -> Self { + Self::Public + } +} + #[cfg(test)] mod tests { use serde_json::{from_value as from_json_value, json, to_value as to_json_value};