client-api: Update membership endpoints to the new API standards

This commit is contained in:
Jonas Platte 2020-09-03 17:34:39 +02:00
parent 53162321c9
commit ce402604e9
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 65 additions and 7 deletions

View File

@ -16,16 +16,17 @@ ruma_api! {
requires_authentication: true,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {
/// The room to get the member events for.
#[ruma_api(path)]
pub room_id: RoomId,
pub room_id: &'a RoomId,
/// The point in time (pagination token) to return members for in the room. This token can
/// be obtained from a prev_batch token returned for each room by the sync API.
#[serde(skip_serializing_if = "Option::is_none")]
#[ruma_api(query)]
pub at: Option<String>,
pub at: Option<&'a str>,
/// The kind of memberships to filter for. Defaults to no filtering if unspecified. When
/// specified alongside not_membership, the two parameters create an 'or' condition: either
@ -41,6 +42,7 @@ ruma_api! {
pub not_membership: Option<MembershipEventFilter>,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// A list of member events.
pub chunk: Vec<Raw<MemberEvent>>
@ -49,8 +51,23 @@ ruma_api! {
error: crate::Error
}
impl<'a> Request<'a> {
/// Creates a new `Request` with the given room ID.
pub fn new(room_id: &'a RoomId) -> Self {
Self { room_id, at: None, membership: None, not_membership: None }
}
}
impl Response {
/// Creates a new `Response` with the given member event chunk.
pub fn new(chunk: Vec<Raw<MemberEvent>>) -> Self {
Self { chunk }
}
}
/// The kind of membership events to filter for.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[serde(rename_all = "lowercase")]
pub enum MembershipEventFilter {
/// The user has joined.
@ -72,7 +89,7 @@ mod tests {
use matches::assert_matches;
use super::{MembershipEventFilter, Request};
use super::{IncomingRequest, MembershipEventFilter};
#[test]
fn deserialization() {
@ -87,12 +104,12 @@ mod tests {
.build()
.unwrap();
let req: Result<Request, _> =
let req: Result<IncomingRequest, _> =
http::Request::builder().uri(uri).body(Vec::<u8>::new()).unwrap().try_into();
assert_matches!(
req,
Ok(Request {
Ok(IncomingRequest {
room_id,
at: Some(at),
membership: None,

View File

@ -16,12 +16,14 @@ ruma_api! {
requires_authentication: true,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {
/// The room to get the members of.
#[ruma_api(path)]
pub room_id: RoomId,
pub room_id: &'a RoomId,
}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// A list of the rooms the user is in, i.e.
/// the ID of each room in which the user has joined membership.
@ -31,8 +33,23 @@ ruma_api! {
error: crate::Error
}
impl<'a> Request<'a> {
/// Creates a new `Request` with the given room ID.
pub fn new(room_id: &'a RoomId) -> Self {
Self { room_id }
}
}
impl Response {
/// Creates a new `Response` with the given joined rooms.
pub fn new(joined: BTreeMap<UserId, RoomMember>) -> Self {
Self { joined }
}
}
/// Information about a room member.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct RoomMember {
/// The display name of the user.
#[serde(skip_serializing_if = "Option::is_none")]
@ -42,3 +59,10 @@ pub struct RoomMember {
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar_url: Option<String>,
}
impl RoomMember {
/// Creates an empty `RoomMember`.
pub fn new() -> Self {
Default::default()
}
}

View File

@ -13,8 +13,11 @@ ruma_api! {
requires_authentication: true,
}
#[derive(Default)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
request: {}
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
response: {
/// A list of the rooms the user is in, i.e. the ID of each room in
/// which the user has joined membership.
@ -23,3 +26,17 @@ ruma_api! {
error: crate::Error
}
impl Request {
/// Creates an empty `Request`.
pub fn new() -> Self {
Self
}
}
impl Response {
/// Creates a new `Response` with the given joined rooms.
pub fn new(joined_rooms: Vec<RoomId>) -> Self {
Self { joined_rooms }
}
}