add optional txn_id to room_send, add docs to room_messages

master
Devin R 2020-04-13 14:08:51 -04:00
parent 0d8702b292
commit fb10e9bf87
2 changed files with 40 additions and 33 deletions

View File

@ -479,15 +479,15 @@ impl AsyncClient {
/// ```ignore /// ```ignore
/// use matrix_sdk::{AsyncClient, RoomBuilder}; /// use matrix_sdk::{AsyncClient, RoomBuilder};
/// ///
/// let mut bldr = RoomBuilder::default(); /// let mut builder = RoomBuilder::default();
/// bldr.creation_content(false) /// builder.creation_content(false)
/// .initial_state(vec![]) /// .initial_state(vec![])
/// .visibility(Visibility::Public) /// .visibility(Visibility::Public)
/// .name("name") /// .name("name")
/// .room_version("v1.0"); /// .room_version("v1.0");
/// ///
/// let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap(); /// let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap();
/// assert!(cli.create_room(bldr).await.is_ok()); /// assert!(cli.create_room(builder).await.is_ok());
/// ``` /// ```
/// ///
pub async fn create_room<R: Into<create_room::Request>>( pub async fn create_room<R: Into<create_room::Request>>(
@ -505,25 +505,27 @@ impl AsyncClient {
/// # Arguments /// # Arguments
/// ///
/// * request - The easiest way to create a `Request` is using the `GetMessageBuilder` /// * request - The easiest way to create a `Request` is using the `GetMessageBuilder`
///
/// # Examples
/// ```ignore
/// use matrix_sdk::{AsyncClient, RoomBuilder};
///
/// let mut builder = RoomMessageBuilder::new();
/// builder.room_id(RoomId::try_from("!roomid:example.com").unwrap())
/// .from("t47429-4392820_219380_26003_2265".to_string())
/// .to("t4357353_219380_26003_2265".to_string())
/// .direction(Direction::Backward)
/// .limit(UInt::new(10).unwrap());
///
/// let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap();
/// assert!(cli.create_room(builder).await.is_ok());
/// ```
pub async fn room_messages<R: Into<get_message_events::Request>>( pub async fn room_messages<R: Into<get_message_events::Request>>(
&mut self, &mut self,
request: R, request: R,
) -> Result<get_message_events::IncomingResponse> { ) -> Result<get_message_events::IncomingResponse> {
let req = request.into(); let req = request.into();
let room_id = req.room_id.clone(); self.send(req).await
let mut res = self.send(req).await?;
let mut client = self.base_client.write().await;
// TODO should we support this event? to keep emitting these msg events this is needed
for mut event in &mut res.chunk {
client
.receive_joined_timeline_event(&room_id, &mut event)
.await;
if let EventResult::Ok(e) = event {
client.emit_timeline_event(&room_id, e).await;
}
}
Ok(res)
} }
/// Synchronize the client's state with the latest state on the server. /// Synchronize the client's state with the latest state on the server.
@ -827,6 +829,9 @@ impl AsyncClient {
/// * `room_id` - The id of the room that should receive the message. /// * `room_id` - The id of the room that should receive the message.
/// ///
/// * `content` - The content of the message event. /// * `content` - The content of the message event.
///
/// * `txn_id` - A unique `Uuid` that can be attached to a `MessageEvent` held
/// in it's unsigned field as `transaction_id`.
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
@ -842,21 +847,23 @@ impl AsyncClient {
/// # let homeserver = Url::parse("http://localhost:8080").unwrap(); /// # let homeserver = Url::parse("http://localhost:8080").unwrap();
/// # let mut client = AsyncClient::new(homeserver, None).unwrap(); /// # let mut client = AsyncClient::new(homeserver, None).unwrap();
/// # let room_id = RoomId::try_from("!test:localhost").unwrap(); /// # let room_id = RoomId::try_from("!test:localhost").unwrap();
/// /// use uuid::Uuid;
///
/// let content = MessageEventContent::Text(TextMessageEventContent { /// let content = MessageEventContent::Text(TextMessageEventContent {
/// body: "Hello world".to_owned(), /// body: "Hello world".to_owned(),
/// format: None, /// format: None,
/// formatted_body: None, /// formatted_body: None,
/// relates_to: None, /// relates_to: None,
/// }); /// });
/// /// let txn_id = Uuid::new_v4();
/// client.room_send(&room_id, content).await.unwrap(); /// client.room_send(&room_id, content, Some(uuid)).await.unwrap();
/// }) /// })
/// ``` /// ```
pub async fn room_send( pub async fn room_send(
&mut self, &mut self,
room_id: &RoomId, room_id: &RoomId,
#[allow(unused_mut)] mut content: MessageEventContent, #[allow(unused_mut)] mut content: MessageEventContent,
txn_id: Option<Uuid>,
) -> Result<create_message_event::Response> { ) -> Result<create_message_event::Response> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut event_type = EventType::RoomMessage; let mut event_type = EventType::RoomMessage;
@ -915,7 +922,7 @@ impl AsyncClient {
let request = create_message_event::Request { let request = create_message_event::Request {
room_id: room_id.clone(), room_id: room_id.clone(),
event_type, event_type,
txn_id: Uuid::new_v4().to_string(), txn_id: txn_id.unwrap_or(Uuid::new_v4()).to_string(),
data: content, data: content,
}; };

View File

@ -23,14 +23,14 @@ use js_int::UInt;
/// # 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 bldr = RoomBuilder::default(); /// let mut builder = RoomBuilder::default();
/// bldr.creation_content(false) /// builder.creation_content(false)
/// .initial_state(vec![]) /// .initial_state(vec![])
/// .visibility(Visibility::Public) /// .visibility(Visibility::Public)
/// .name("name") /// .name("name")
/// .room_version("v1.0"); /// .room_version("v1.0");
/// let mut cli = AsyncClient::new(homeserver, None).unwrap(); /// let mut cli = AsyncClient::new(homeserver, None).unwrap();
/// cli.create_room(bldr).await; /// cli.create_room(builder).await;
/// # }) /// # })
/// ``` /// ```
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -218,12 +218,12 @@ impl RoomMessageBuilder {
/// # let last_sync_token = "".to_string();; /// # let last_sync_token = "".to_string();;
/// let mut cli = AsyncClient::new(homeserver, None).unwrap(); /// let mut cli = AsyncClient::new(homeserver, None).unwrap();
/// ///
/// let mut bldr = RoomMessageBuilder::new(); /// let mut builder = RoomMessageBuilder::new();
/// bldr.room_id(room_id) /// builder.room_id(room_id)
/// .from(last_sync_token) /// .from(last_sync_token)
/// .direction(Direction::Forward); /// .direction(Direction::Forward);
/// ///
/// cli.room_messages(bldr).await.is_err(); /// cli.room_messages(builder).await.is_err();
/// # }) /// # })
/// ``` /// ```
pub fn new() -> Self { pub fn new() -> Self {
@ -314,8 +314,8 @@ mod test {
device_id: "DEVICEID".to_owned(), device_id: "DEVICEID".to_owned(),
}; };
let mut bldr = RoomBuilder::new(); let mut builder = RoomBuilder::new();
bldr.creation_content(false) builder.creation_content(false)
.initial_state(vec![]) .initial_state(vec![])
.visibility(Visibility::Public) .visibility(Visibility::Public)
.name("room_name") .name("room_name")
@ -341,7 +341,7 @@ mod test {
.topic("room topic") .topic("room topic")
.visibility(Visibility::Private); .visibility(Visibility::Private);
let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap(); let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap();
assert!(cli.create_room(bldr).await.is_ok()); assert!(cli.create_room(builder).await.is_ok());
} }
#[tokio::test] #[tokio::test]
@ -362,8 +362,8 @@ mod test {
device_id: "DEVICEID".to_owned(), device_id: "DEVICEID".to_owned(),
}; };
let mut bldr = RoomMessageBuilder::new(); let mut builder = RoomMessageBuilder::new();
bldr.room_id(RoomId::try_from("!roomid:example.com").unwrap()) builder.room_id(RoomId::try_from("!roomid:example.com").unwrap())
.from("t47429-4392820_219380_26003_2265".to_string()) .from("t47429-4392820_219380_26003_2265".to_string())
.to("t4357353_219380_26003_2265".to_string()) .to("t4357353_219380_26003_2265".to_string())
.direction(Direction::Backward) .direction(Direction::Backward)
@ -372,6 +372,6 @@ mod test {
// .filter(RoomEventFilter::default()); // .filter(RoomEventFilter::default());
let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap(); let mut cli = AsyncClient::new(homeserver, Some(session)).unwrap();
assert!(cli.room_messages(bldr).await.is_ok()); assert!(cli.room_messages(builder).await.is_ok());
} }
} }