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;
///
/// # let homeserver = Url::parse("http://example.com").unwrap();
/// let mut builder = RoomBuilder::default();
/// let mut builder = RoomBuilder::new();
/// builder.federate(false)
/// .initial_state(vec![])
/// .visibility(Visibility::Public)

View File

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

View File

@ -50,7 +50,10 @@ use crate::js_int::{Int, UInt};
use serde::{Deserialize, Serialize};
#[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 {
content: event.content,
redacts: event.redacts,
@ -638,9 +641,11 @@ impl Room {
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_attr(docsrs, doc(cfg(feature = "messages")))]
pub fn handle_redaction(&mut self, event: &RedactionEventStub) -> bool {
@ -651,7 +656,8 @@ impl Room {
{
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));
true
} else {

View File

@ -11,11 +11,9 @@ repository = "https://github.com/matrix-org/matrix-rust-sdk"
version = "0.1.0"
[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"] }
js_int = "0.1.5"
ruma = { git = "https://github.com/ruma/ruma", features = ["client-api"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
uuid = { version = "0.8.1", features = ["v4"] }