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!(
3939 // Default value.
4040 "🔒",
4141 },
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 }
4250 },
4351 Section network {
4452 debug_buffer: bool {
@@ -53,7 +61,7 @@ config!(
5361/// A wrapper for our config struct that can be cloned around.
5462#[derive(Clone)]
5563pub struct ConfigHandle {
56 inner: Rc<RefCell<Config>>,
64 pub inner: Rc<RefCell<Config>>,
5765 servers: Servers,
5866}
5967
src/room_buffer.rs +33 −7
@@ -51,6 +51,7 @@ use futures::executor::block_on;
5151use tracing::{debug, error, trace};
5252
5353use crate::{
54 config::Config,
5455 connection::{Connection, TYPING_NOTICE_TIMEOUT},
5556 render::{render_membership, Render, RenderedEvent},
5657};
@@ -117,6 +118,7 @@ struct MatrixRoom {
117118 own_user_id: Rc<UserId>,
118119 room: Arc<RwLock<Room>>,
119120
121 config: Rc<RefCell<Config>>,
120122 connection: Rc<RefCell<Option<Connection>>>,
121123
122124 typing_notice_time: Rc<RefCell<Option<Instant>>>,
@@ -157,7 +159,9 @@ impl MatrixRoom {
157159 match c.send_message(&self.room_id, content, Some(uuid)).await {
158160 // TODO the event id is in the response, this needs to end up in
159161 // a tag.
160 Ok(_r) => self.replace_local_echo(buffer, uuid),
162 Ok(_r) => {
163 self.replace_local_echo(buffer, uuid);
164 }
161165 Err(_e) => (),
162166 }
163167 } else {
@@ -175,7 +179,7 @@ impl MatrixRoom {
175179 uuid: Uuid,
176180 content: &AnyMessageEventContent,
177181 ) {
178 if true {
182 if self.config.borrow().look().local_echo() {
179183 if let AnyMessageEventContent::RoomMessage(
180184 MessageEventContent::Text(c),
181185 ) = content
@@ -211,7 +215,17 @@ impl MatrixRoom {
211215 }
212216 }
213217
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 {
215229 let uuid_tag = Cow::from(format!("matrix_echo_{}", uuid.to_string()));
216230
217231 let line_contains_uuid = |l: &BufferLine| l.tags().contains(&uuid_tag);
@@ -220,7 +234,8 @@ impl MatrixRoom {
220234 {
221235 let content = match content {
222236 MessageEventContent::Text(c) => c.render(&()),
223 _ => return,
237 // TODO support more message types.
238 _ => return false,
224239 };
225240
226241 if let Ok(buffer) = buffer_handle.upgrade() {
@@ -237,6 +252,10 @@ impl MatrixRoom {
237252 first_line = lines.next_back().filter(line_contains_uuid);
238253 }
239254 }
255
256 true
257 } else {
258 false
240259 }
241260 }
242261}
@@ -244,6 +263,7 @@ impl MatrixRoom {
244263impl RoomBuffer {
245264 pub fn new(
246265 connection: &Rc<RefCell<Option<Connection>>>,
266 config: Rc<RefCell<Config>>,
247267 room: Arc<RwLock<Room>>,
248268 homeserver: &Url,
249269 room_id: RoomId,
@@ -255,6 +275,7 @@ impl RoomBuffer {
255275 connection: connection.clone(),
256276 typing_notice_time: Rc::new(RefCell::new(None)),
257277 typing_in_flight: Rc::new(Mutex::new(())),
278 config,
258279 room,
259280 own_user_id: Rc::new(own_user_id.to_owned()),
260281 members: Rc::new(RefCell::new(HashMap::new())),
@@ -286,6 +307,7 @@ impl RoomBuffer {
286307 pub fn restore(
287308 room: Arc<RwLock<Room>>,
288309 connection: &Rc<RefCell<Option<Connection>>>,
310 config: Rc<RefCell<Config>>,
289311 homeserver: &Url,
290312 ) -> Self {
291313 let room_clone = room.clone();
@@ -295,6 +317,7 @@ impl RoomBuffer {
295317
296318 let room_buffer = RoomBuffer::new(
297319 connection,
320 config,
298321 room_clone,
299322 homeserver,
300323 room_id,
@@ -862,9 +885,12 @@ impl RoomBuffer {
862885 pub fn handle_room_message(&self, event: &AnySyncMessageEvent) {
863886 if let Some(id) = &event.unsigned().transaction_id {
864887 if let Ok(id) = Uuid::parse_str(id) {
865 self.inner
866 .replace_local_echo(self.buffer_handle.clone(), id);
867 return;
888 if self
889 .inner
890 .replace_local_echo(self.buffer_handle.clone(), id)
891 {
892 return;
893 }
868894 }
869895 }
870896
src/server.rs +7 −1
@@ -564,6 +564,7 @@ impl InnerServer {
564564 room.expect("Receiving events for a room while no room found");
565565 let buffer = RoomBuffer::new(
566566 &self.connection,
567 self.config.inner.clone(),
567568 room,
568569 homeserver,
569570 room_id.clone(),
@@ -594,7 +595,12 @@ impl InnerServer {
594595 .as_ref()
595596 .expect("Creating room buffer while no homeserver");
596597
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 );
598604 let room_id = buffer.room_id().to_owned();
599605
600606 self.room_buffers.insert(room_id, buffer);