matrix-sdk: Update request_builder to use new constructors

The create_room::Request and get_message_events::Request now have
constructors that we use in our builder structs.
master
Devin R 2020-07-04 15:28:36 -04:00
parent e7c70854ab
commit c0e6279837
4 changed files with 47 additions and 86 deletions

View File

@ -772,7 +772,7 @@ impl Client {
/// # use url::Url; /// # use url::Url;
/// ///
/// # let homeserver = Url::parse("http://example.com").unwrap(); /// # let homeserver = Url::parse("http://example.com").unwrap();
/// let mut builder = RoomBuilder::default(); /// let mut builder = RoomBuilder::new();
/// builder.federate(false) /// builder.federate(false)
/// .initial_state(vec![]) /// .initial_state(vec![])
/// .visibility(Visibility::Public) /// .visibility(Visibility::Public)

View File

@ -31,7 +31,7 @@ use matrix_sdk_common::{
/// # let homeserver = Url::parse("http://example.com").unwrap(); /// # let homeserver = Url::parse("http://example.com").unwrap();
/// # let mut rt = tokio::runtime::Runtime::new().unwrap(); /// # let mut rt = tokio::runtime::Runtime::new().unwrap();
/// # rt.block_on(async { /// # rt.block_on(async {
/// let mut builder = RoomBuilder::default(); /// let mut builder = RoomBuilder::new();
/// builder.federate(false) /// builder.federate(false)
/// .initial_state(vec![]) /// .initial_state(vec![])
/// .visibility(Visibility::Public) /// .visibility(Visibility::Public)
@ -41,30 +41,21 @@ use matrix_sdk_common::{
/// client.create_room(builder).await; /// client.create_room(builder).await;
/// # }) /// # })
/// ``` /// ```
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug)]
pub struct RoomBuilder { pub struct RoomBuilder {
previous_room: Option<PreviousRoom>, req: create_room::Request,
federate: bool, creation_content: CreationContent,
initial_state: Vec<InitialStateEvent>,
invite: Vec<UserId>,
invite_3pid: Vec<Invite3pid>,
is_direct: Option<bool>,
name: Option<String>,
power_level_content_override: Option<PowerLevelsEventContent>,
preset: Option<RoomPreset>,
room_alias_name: Option<String>,
room_version: Option<String>,
topic: Option<String>,
visibility: Option<Visibility>,
} }
impl RoomBuilder { impl RoomBuilder {
/// Returns an empty `RoomBuilder` for creating rooms. /// Returns an empty `RoomBuilder` for creating rooms.
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
// we can't send false as a default as it will overwrite the true default. req: create_room::Request::new(),
federate: true, creation_content: CreationContent {
..Default::default() federate: true,
predecessor: None,
},
} }
} }
@ -72,7 +63,7 @@ impl RoomBuilder {
/// ///
/// Note: this is used to create the `CreationContent`. /// Note: this is used to create the `CreationContent`.
pub fn previous_room(&mut self, previous_room: PreviousRoom) -> &mut Self { pub fn previous_room(&mut self, previous_room: PreviousRoom) -> &mut Self {
self.previous_room = Some(previous_room); self.creation_content.predecessor = Some(previous_room);
self self
} }
@ -81,7 +72,7 @@ impl RoomBuilder {
/// Defaults to `true` if key does not exist. /// Defaults to `true` if key does not exist.
/// Note: this is used to create the `CreationContent`. /// Note: this is used to create the `CreationContent`.
pub fn federate(&mut self, federate: bool) -> &mut Self { pub fn federate(&mut self, federate: bool) -> &mut Self {
self.federate = federate; self.creation_content.federate = federate;
self self
} }
@ -90,7 +81,7 @@ impl RoomBuilder {
/// Takes precedence over events set by preset, but gets overriden by /// Takes precedence over events set by preset, but gets overriden by
/// name and topic keys. /// name and topic keys.
pub fn initial_state(&mut self, state: Vec<InitialStateEvent>) -> &mut Self { pub fn initial_state(&mut self, state: Vec<InitialStateEvent>) -> &mut Self {
self.initial_state = state; self.req.initial_state = state;
self self
} }
@ -98,57 +89,57 @@ impl RoomBuilder {
/// ///
/// This will tell the server to invite everyone in the list to the newly created room. /// This will tell the server to invite everyone in the list to the newly created room.
pub fn invite(&mut self, invite: Vec<UserId>) -> &mut Self { pub fn invite(&mut self, invite: Vec<UserId>) -> &mut Self {
self.invite = invite; self.req.invite = invite;
self self
} }
/// Sets a list of third party IDs of users to invite. /// Sets a list of third party IDs of users to invite.
pub fn invite_3pid(&mut self, invite: Vec<Invite3pid>) -> &mut Self { pub fn invite_3pid(&mut self, invite: Vec<Invite3pid>) -> &mut Self {
self.invite_3pid = invite; self.req.invite_3pid = invite;
self self
} }
/// If set, this sets the `is_direct` flag on room invites. /// If set, this sets the `is_direct` flag on room invites.
pub fn is_direct(&mut self, direct: bool) -> &mut Self { pub fn is_direct(&mut self, direct: bool) -> &mut Self {
self.is_direct = Some(direct); self.req.is_direct = Some(direct);
self self
} }
/// If this is included, an `m.room.name` event will be sent into the room to indicate /// If this is included, an `m.room.name` event will be sent into the room to indicate
/// the name of the room. /// the name of the room.
pub fn name<S: Into<String>>(&mut self, name: S) -> &mut Self { pub fn name<S: Into<String>>(&mut self, name: S) -> &mut Self {
self.name = Some(name.into()); self.req.name = Some(name.into());
self self
} }
/// Power level content to override in the default power level event. /// Power level content to override in the default power level event.
pub fn power_level_override(&mut self, power: PowerLevelsEventContent) -> &mut Self { pub fn power_level_override(&mut self, power: PowerLevelsEventContent) -> &mut Self {
self.power_level_content_override = Some(power); self.req.power_level_content_override = Some(EventJson::from(power));
self self
} }
/// Convenience parameter for setting various default state events based on a preset. /// Convenience parameter for setting various default state events based on a preset.
pub fn preset(&mut self, preset: RoomPreset) -> &mut Self { pub fn preset(&mut self, preset: RoomPreset) -> &mut Self {
self.preset = Some(preset); self.req.preset = Some(preset);
self self
} }
/// The desired room alias local part. /// The desired room alias local part.
pub fn room_alias_name<S: Into<String>>(&mut self, alias: S) -> &mut Self { pub fn room_alias_name<S: Into<String>>(&mut self, alias: S) -> &mut Self {
self.room_alias_name = Some(alias.into()); self.req.room_alias_name = Some(alias.into());
self self
} }
/// Room version to set for the room. Defaults to homeserver's default if not specified. /// Room version to set for the room. Defaults to homeserver's default if not specified.
pub fn room_version<S: Into<String>>(&mut self, version: S) -> &mut Self { pub fn room_version<S: Into<String>>(&mut self, version: S) -> &mut Self {
self.room_version = Some(version.into()); self.req.room_version = Some(version.into());
self self
} }
/// If this is included, an `m.room.topic` event will be sent into the room to indicate /// If this is included, an `m.room.topic` event will be sent into the room to indicate
/// the topic for the room. /// the topic for the room.
pub fn topic<S: Into<String>>(&mut self, topic: S) -> &mut Self { pub fn topic<S: Into<String>>(&mut self, topic: S) -> &mut Self {
self.topic = Some(topic.into()); self.req.topic = Some(topic.into());
self self
} }
@ -156,31 +147,15 @@ impl RoomBuilder {
/// room list. A private visibility will hide the room from the published room list. /// room list. A private visibility will hide the room from the published room list.
/// Rooms default to private visibility if this key is not included. /// Rooms default to private visibility if this key is not included.
pub fn visibility(&mut self, vis: Visibility) -> &mut Self { pub fn visibility(&mut self, vis: Visibility) -> &mut Self {
self.visibility = Some(vis); self.req.visibility = Some(vis);
self self
} }
} }
impl Into<create_room::Request> for RoomBuilder { impl Into<create_room::Request> for RoomBuilder {
fn into(self) -> create_room::Request { fn into(mut self) -> create_room::Request {
let creation_content = Some(CreationContent { self.req.creation_content = Some(self.creation_content);
federate: self.federate, self.req
predecessor: self.previous_room,
});
create_room::Request {
creation_content,
initial_state: self.initial_state,
invite: self.invite,
invite_3pid: self.invite_3pid,
is_direct: self.is_direct,
name: self.name,
power_level_content_override: self.power_level_content_override.map(EventJson::from),
preset: self.preset,
room_alias_name: self.room_alias_name,
room_version: self.room_version,
topic: self.topic,
visibility: self.visibility,
}
} }
} }
@ -213,14 +188,7 @@ impl Into<create_room::Request> for RoomBuilder {
/// # }) /// # })
/// ``` /// ```
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct MessagesRequestBuilder { pub struct MessagesRequestBuilder(get_message_events::Request);
room_id: RoomId,
from: String,
to: Option<String>,
direction: Option<Direction>,
limit: Option<u32>,
filter: Option<RoomEventFilter>,
}
impl MessagesRequestBuilder { impl MessagesRequestBuilder {
/// Create a `MessagesRequestBuilder` builder to make a `get_message_events::Request`. /// Create a `MessagesRequestBuilder` builder to make a `get_message_events::Request`.
@ -233,21 +201,18 @@ impl MessagesRequestBuilder {
/// a `prev_batch` token from a sync response, or a start or end token from a previous request /// a `prev_batch` token from a sync response, or a start or end token from a previous request
/// to this endpoint. /// to this endpoint.
pub fn new(room_id: RoomId, from: String) -> Self { pub fn new(room_id: RoomId, from: String) -> Self {
Self { Self(get_message_events::Request::new(
room_id, room_id,
from, from,
to: None, Direction::Backward,
direction: None, ))
limit: None,
filter: None,
}
} }
/// A `next_batch` token or `start` or `end` from a previous `get_message_events` request. /// A `next_batch` token or `start` or `end` from a previous `get_message_events` request.
/// ///
/// This token signals when to stop receiving events. /// This token signals when to stop receiving events.
pub fn to<S: Into<String>>(&mut self, to: S) -> &mut Self { pub fn to<S: Into<String>>(&mut self, to: S) -> &mut Self {
self.to = Some(to.into()); self.0.to = Some(to.into());
self self
} }
@ -255,7 +220,7 @@ impl MessagesRequestBuilder {
/// ///
/// If not specified `Direction::Backward` is used. /// If not specified `Direction::Backward` is used.
pub fn direction(&mut self, direction: Direction) -> &mut Self { pub fn direction(&mut self, direction: Direction) -> &mut Self {
self.direction = Some(direction); self.0.dir = direction;
self self
} }
@ -263,28 +228,20 @@ impl MessagesRequestBuilder {
/// ///
/// The default is 10. /// The default is 10.
pub fn limit(&mut self, limit: u32) -> &mut Self { pub fn limit(&mut self, limit: u32) -> &mut Self {
self.limit = Some(limit); self.0.limit = UInt::from(limit);
self self
} }
/// Filter events by the given `RoomEventFilter`. /// Filter events by the given `RoomEventFilter`.
pub fn filter(&mut self, filter: RoomEventFilter) -> &mut Self { pub fn filter(&mut self, filter: RoomEventFilter) -> &mut Self {
self.filter = Some(filter); self.0.filter = Some(filter);
self self
} }
} }
impl Into<get_message_events::Request> for MessagesRequestBuilder { impl Into<get_message_events::Request> for MessagesRequestBuilder {
fn into(self) -> get_message_events::Request { fn into(self) -> get_message_events::Request {
get_message_events::Request { self.0
room_id: self.room_id,
from: self.from,
to: self.to,
dir: self.direction.unwrap_or(Direction::Backward),
// Will our default overwrite the normal default
limit: self.limit.map(UInt::from).unwrap_or_default(),
filter: self.filter,
}
} }
} }

View File

@ -50,7 +50,10 @@ use crate::js_int::{Int, UInt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "messages")] #[cfg(feature = "messages")]
fn full_event_from_stub(event: RedactionEventStub, room_id: RoomId) -> RedactionEvent { fn redaction_event_from_redaction_stub(
event: RedactionEventStub,
room_id: RoomId,
) -> RedactionEvent {
RedactionEvent { RedactionEvent {
content: event.content, content: event.content,
redacts: event.redacts, redacts: event.redacts,
@ -638,9 +641,11 @@ impl Room {
self.messages.push(message) self.messages.push(message)
} }
/// Handle a room.redaction event and update the `MessageQueue` if necessary. /// Handle a room.redaction event and update the `MessageQueue`.
/// ///
/// Returns true if `MessageQueue` was updated. /// Returns true if `MessageQueue` was updated. The effected message event
/// has it's contents replaced with the `RedactionEventContents` and the whole
/// redaction event is added to the `Unsigned` `redacted_because` field.
#[cfg(feature = "messages")] #[cfg(feature = "messages")]
#[cfg_attr(docsrs, doc(cfg(feature = "messages")))] #[cfg_attr(docsrs, doc(cfg(feature = "messages")))]
pub fn handle_redaction(&mut self, event: &RedactionEventStub) -> bool { pub fn handle_redaction(&mut self, event: &RedactionEventStub) -> bool {
@ -651,7 +656,8 @@ impl Room {
{ {
msg.content = AnyMessageEventContent::RoomRedaction(event.content.clone()); msg.content = AnyMessageEventContent::RoomRedaction(event.content.clone());
let redaction = full_event_from_stub(event.clone(), self.room_id.clone()); let redaction =
redaction_event_from_redaction_stub(event.clone(), self.room_id.clone());
msg.unsigned.redacted_because = Some(EventJson::from(redaction)); msg.unsigned.redacted_because = Some(EventJson::from(redaction));
true true
} else { } else {

View File

@ -11,11 +11,9 @@ repository = "https://github.com/matrix-org/matrix-rust-sdk"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
js_int = "0.1.5"
ruma = { git = "https://github.com/ruma/ruma", features = ["client-api"] }
instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] } instant = { version = "0.1.4", features = ["wasm-bindgen", "now"] }
js_int = "0.1.5"
ruma = { git = "https://github.com/ruma/ruma", features = ["client-api"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
uuid = { version = "0.8.1", features = ["v4"] } uuid = { version = "0.8.1", features = ["v4"] }