Commit dc2516a27e

dc2516a27e0770aa8551ac2d370740f75b9da1fb

parent: f93e558121

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-10-12 15:02 UTC

room_buffer: Make the local echo configurable.

Layout: unified · split

src/config.rs +9 −1
@@ -39,6 +39,14 @@ config!(
39 // Default value. 39 // Default value.
40 "🔒", 40 "🔒",
41 }, 41 },
42
43 local_echo: bool {
44 // Description
45 "Should the sending event be printed out before the server \
46 the receipt of the message",
47 // Default value
48 true,
49 }
42 }, 50 },
43 Section network { 51 Section network {
44 debug_buffer: bool { 52 debug_buffer: bool {
@@ -53,7 +61,7 @@ config!(
53/// A wrapper for our config struct that can be cloned around. 61/// A wrapper for our config struct that can be cloned around.
54#[derive(Clone)] 62#[derive(Clone)]
55pub struct ConfigHandle { 63pub struct ConfigHandle {
56 inner: Rc<RefCell<Config>>, 64 pub inner: Rc<RefCell<Config>>,
57 servers: Servers, 65 servers: Servers,
58} 66}
59 67
src/room_buffer.rs +33 −7
@@ -51,6 +51,7 @@ use futures::executor::block_on;
51use tracing::{debug, error, trace}; 51use tracing::{debug, error, trace};
52 52
53use crate::{ 53use crate::{
54 config::Config,
54 connection::{Connection, TYPING_NOTICE_TIMEOUT}, 55 connection::{Connection, TYPING_NOTICE_TIMEOUT},
55 render::{render_membership, Render, RenderedEvent}, 56 render::{render_membership, Render, RenderedEvent},
56}; 57};
@@ -117,6 +118,7 @@ struct MatrixRoom {
117 own_user_id: Rc<UserId>, 118 own_user_id: Rc<UserId>,
118 room: Arc<RwLock<Room>>, 119 room: Arc<RwLock<Room>>,
119 120
121 config: Rc<RefCell<Config>>,
120 connection: Rc<RefCell<Option<Connection>>>, 122 connection: Rc<RefCell<Option<Connection>>>,
121 123
122 typing_notice_time: Rc<RefCell<Option<Instant>>>, 124 typing_notice_time: Rc<RefCell<Option<Instant>>>,
@@ -157,7 +159,9 @@ impl MatrixRoom {
157 match c.send_message(&self.room_id, content, Some(uuid)).await { 159 match c.send_message(&self.room_id, content, Some(uuid)).await {
158 // TODO the event id is in the response, this needs to end up in 160 // TODO the event id is in the response, this needs to end up in
159 // a tag. 161 // a tag.
160 Ok(_r) => self.replace_local_echo(buffer, uuid), 162 Ok(_r) => {
163 self.replace_local_echo(buffer, uuid);
164 }
161 Err(_e) => (), 165 Err(_e) => (),
162 } 166 }
163 } else { 167 } else {
@@ -175,7 +179,7 @@ impl MatrixRoom {
175 uuid: Uuid, 179 uuid: Uuid,
176 content: &AnyMessageEventContent, 180 content: &AnyMessageEventContent,
177 ) { 181 ) {
178 if true { 182 if self.config.borrow().look().local_echo() {
179 if let AnyMessageEventContent::RoomMessage( 183 if let AnyMessageEventContent::RoomMessage(
180 MessageEventContent::Text(c), 184 MessageEventContent::Text(c),
181 ) = content 185 ) = content
@@ -211,7 +215,17 @@ impl MatrixRoom {
211 } 215 }
212 } 216 }
213 217
214 pub fn replace_local_echo(&self, buffer_handle: BufferHandle, uuid: Uuid) { 218 /// Replace the local echo line with a normal line for the event content
219 /// with the given UUID.
220 ///
221 /// If no content with the given UUID was found in the local echo queue
222 /// false is returned, if the event was successfully found and replaced true
223 /// is returned.
224 pub fn replace_local_echo(
225 &self,
226 buffer_handle: BufferHandle,
227 uuid: Uuid,
228 ) -> bool {
215 let uuid_tag = Cow::from(format!("matrix_echo_{}", uuid.to_string())); 229 let uuid_tag = Cow::from(format!("matrix_echo_{}", uuid.to_string()));
216 230
217 let line_contains_uuid = |l: &BufferLine| l.tags().contains(&uuid_tag); 231 let line_contains_uuid = |l: &BufferLine| l.tags().contains(&uuid_tag);
@@ -220,7 +234,8 @@ impl MatrixRoom {
220 { 234 {
221 let content = match content { 235 let content = match content {
222 MessageEventContent::Text(c) => c.render(&()), 236 MessageEventContent::Text(c) => c.render(&()),
223 _ => return, 237 // TODO support more message types.
238 _ => return false,
224 }; 239 };
225 240
226 if let Ok(buffer) = buffer_handle.upgrade() { 241 if let Ok(buffer) = buffer_handle.upgrade() {
@@ -237,6 +252,10 @@ impl MatrixRoom {
237 first_line = lines.next_back().filter(line_contains_uuid); 252 first_line = lines.next_back().filter(line_contains_uuid);
238 } 253 }
239 } 254 }
255
256 true
257 } else {
258 false
240 } 259 }
241 } 260 }
242} 261}
@@ -244,6 +263,7 @@ impl MatrixRoom {
244impl RoomBuffer { 263impl RoomBuffer {
245 pub fn new( 264 pub fn new(
246 connection: &Rc<RefCell<Option<Connection>>>, 265 connection: &Rc<RefCell<Option<Connection>>>,
266 config: Rc<RefCell<Config>>,
247 room: Arc<RwLock<Room>>, 267 room: Arc<RwLock<Room>>,
248 homeserver: &Url, 268 homeserver: &Url,
249 room_id: RoomId, 269 room_id: RoomId,
@@ -255,6 +275,7 @@ impl RoomBuffer {
255 connection: connection.clone(), 275 connection: connection.clone(),
256 typing_notice_time: Rc::new(RefCell::new(None)), 276 typing_notice_time: Rc::new(RefCell::new(None)),
257 typing_in_flight: Rc::new(Mutex::new(())), 277 typing_in_flight: Rc::new(Mutex::new(())),
278 config,
258 room, 279 room,
259 own_user_id: Rc::new(own_user_id.to_owned()), 280 own_user_id: Rc::new(own_user_id.to_owned()),
260 members: Rc::new(RefCell::new(HashMap::new())), 281 members: Rc::new(RefCell::new(HashMap::new())),
@@ -286,6 +307,7 @@ impl RoomBuffer {
286 pub fn restore( 307 pub fn restore(
287 room: Arc<RwLock<Room>>, 308 room: Arc<RwLock<Room>>,
288 connection: &Rc<RefCell<Option<Connection>>>, 309 connection: &Rc<RefCell<Option<Connection>>>,
310 config: Rc<RefCell<Config>>,
289 homeserver: &Url, 311 homeserver: &Url,
290 ) -> Self { 312 ) -> Self {
291 let room_clone = room.clone(); 313 let room_clone = room.clone();
@@ -295,6 +317,7 @@ impl RoomBuffer {
295 317
296 let room_buffer = RoomBuffer::new( 318 let room_buffer = RoomBuffer::new(
297 connection, 319 connection,
320 config,
298 room_clone, 321 room_clone,
299 homeserver, 322 homeserver,
300 room_id, 323 room_id,
@@ -862,9 +885,12 @@ impl RoomBuffer {
862 pub fn handle_room_message(&self, event: &AnySyncMessageEvent) { 885 pub fn handle_room_message(&self, event: &AnySyncMessageEvent) {
863 if let Some(id) = &event.unsigned().transaction_id { 886 if let Some(id) = &event.unsigned().transaction_id {
864 if let Ok(id) = Uuid::parse_str(id) { 887 if let Ok(id) = Uuid::parse_str(id) {
865 self.inner 888 if self
866 .replace_local_echo(self.buffer_handle.clone(), id); 889 .inner
867 return; 890 .replace_local_echo(self.buffer_handle.clone(), id)
891 {
892 return;
893 }
868 } 894 }
869 } 895 }
870 896
src/server.rs +7 −1
@@ -564,6 +564,7 @@ impl InnerServer {
564 room.expect("Receiving events for a room while no room found"); 564 room.expect("Receiving events for a room while no room found");
565 let buffer = RoomBuffer::new( 565 let buffer = RoomBuffer::new(
566 &self.connection, 566 &self.connection,
567 self.config.inner.clone(),
567 room, 568 room,
568 homeserver, 569 homeserver,
569 room_id.clone(), 570 room_id.clone(),
@@ -594,7 +595,12 @@ impl InnerServer {
594 .as_ref() 595 .as_ref()
595 .expect("Creating room buffer while no homeserver"); 596 .expect("Creating room buffer while no homeserver");
596 597
597 let buffer = RoomBuffer::restore(room, &self.connection, homeserver); 598 let buffer = RoomBuffer::restore(
599 room,
600 &self.connection,
601 self.config.inner.clone(),
602 homeserver,
603 );
598 let room_id = buffer.room_id().to_owned(); 604 let room_id = buffer.room_id().to_owned();
599 605
600 self.room_buffers.insert(room_id, buffer); 606 self.room_buffers.insert(room_id, buffer);