Commit 87ef9d3d59

87ef9d3d5972fef78a4e5bc1875a58d5b9a53f54

parent: e53abd2732

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-06 09:44 UTC

room: Move the sending logic out of the input callback.

Layout: unified · split

src/room_buffer.rs +15 −12
@@ -106,19 +106,22 @@ pub struct MatrixRoom {
106#[async_trait(?Send)] 106#[async_trait(?Send)]
107impl BufferInputCallbackAsync for MatrixRoom { 107impl BufferInputCallbackAsync for MatrixRoom {
108 async fn callback(&mut self, buffer: BufferHandle, input: String) { 108 async fn callback(&mut self, buffer: BufferHandle, input: String) {
109 let connection = &self.connection; 109 // TODO parse the input here and produce a formatted body.
110 let content = MessageEventContent::Text(TextMessageEventContent {
111 body: input,
112 formatted: None,
113 relates_to: None,
114 });
115 let content = AnyMessageEventContent::RoomMessage(content);
116
117 self.send_message(buffer, content).await;
118 }
119}
110 120
111 // FIXME move this logic into the `MatrixRoom` 121impl MatrixRoom {
112 if let Some(c) = &*connection.borrow() { 122 pub async fn send_message(&self, buffer: BufferHandle, content: AnyMessageEventContent) {
123 if let Some(c) = &*self.connection.borrow() {
113 // TODO check for errors and print them out. 124 // TODO check for errors and print them out.
114 let content = MessageEventContent::Text(TextMessageEventContent {
115 body: input,
116 formatted: None,
117 relates_to: None,
118 });
119
120 let content = AnyMessageEventContent::RoomMessage(content);
121
122 match c.send_message(&self.room_id, content, None).await { 125 match c.send_message(&self.room_id, content, None).await {
123 Ok(_r) => (), 126 Ok(_r) => (),
124 Err(_e) => (), 127 Err(_e) => (),
@@ -126,7 +129,7 @@ impl BufferInputCallbackAsync for MatrixRoom {
126 } else { 129 } else {
127 let buffer = buffer 130 let buffer = buffer
128 .upgrade() 131 .upgrade()
129 .expect("Running input cb but buffer is closed"); 132 .expect("Trying to send a message while the buffer is closed");
130 133
131 buffer.print("Error not connected"); 134 buffer.print("Error not connected");
132 } 135 }