Commit 071dd1ec35

071dd1ec358cabbfcdfda2e8b870dba28bc6d4fd

parent: 76911dfcc7

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-09-05 11:50 UTC

weechat-matrix: Refactor out the server/room finding logic.

Layout: unified · split

src/lib.rs +39 −11
@@ -19,7 +19,7 @@ use weechat::{
1919 plugin, Args, Plugin, ReturnCode, Weechat,
2020};
2121
22use crate::{commands::Commands, config::ConfigHandle, server::MatrixServer};
22use crate::{commands::Commands, config::ConfigHandle, server::MatrixServer, room_buffer::RoomBuffer};
2323
2424const PLUGIN_NAME: &str = "matrix";
2525
@@ -38,6 +38,42 @@ impl Servers {
3838 fn borrow_mut(&self) -> RefMut<'_, HashMap<String, MatrixServer>> {
3939 self.0.borrow_mut()
4040 }
41
42 /// Find a `MatrixServer` that the given buffer belongs to.
43 ///
44 /// Returns None if the buffer doesn't belong to any of our servers of
45 /// rooms.
46 pub fn find_server(&self, buffer: &Buffer) -> Option<MatrixServer> {
47 let servers = self.borrow();
48
49 for server in servers.values() {
50 for room_buffer in server.inner().room_buffers.values() {
51 if buffer == &room_buffer.weechat_buffer() {
52 return Some(server.clone())
53 }
54 }
55 }
56
57 None
58 }
59
60 /// Find a `RoomBuffer` that the given buffer belongs to.
61 ///
62 /// Returns None if the buffer doesn't belong to any of our servers of
63 /// rooms.
64 pub fn find_room(&self, buffer: &Buffer) -> Option<RoomBuffer> {
65 let servers = self.borrow();
66
67 for server in servers.values() {
68 for room_buffer in server.inner().room_buffers.values() {
69 if buffer == &room_buffer.weechat_buffer() {
70 return Some(room_buffer.clone());
71 }
72 }
73 }
74
75 None
76 }
4177}
4278
4379impl SignalCallback for Servers {
@@ -49,16 +85,8 @@ impl SignalCallback for Servers {
4985 ) -> ReturnCode {
5086 if let Some(data) = data {
5187 if let SignalData::Buffer(buffer) = data {
52 let servers = self.borrow();
53
54 for server in servers.values() {
55 for room_buffer in server.inner().room_buffers.values() {
56 if buffer == room_buffer.weechat_buffer() {
57 room_buffer.update_typing_notice();
58
59 return ReturnCode::Ok;
60 }
61 }
88 if let Some(room_buffer) = self.find_room(&buffer) {
89 room_buffer.update_typing_notice();
6290 }
6391 }
6492 }
src/room_buffer.rs +3 −2
@@ -66,10 +66,11 @@ use weechat::{
6666 Weechat,
6767};
6868
69#[derive(Clone)]
6970pub struct RoomBuffer {
7071 own_user_id: Rc<UserId>,
7172 room_id: Rc<RoomId>,
72 typing_in_flight: Mutex<()>,
73 typing_in_flight: Rc<Mutex<()>>,
7374 inner: MatrixRoom,
7475 buffer_handle: BufferHandle,
7576}
@@ -160,7 +161,7 @@ impl RoomBuffer {
160161 RoomBuffer {
161162 room_id: Rc::new(room_id),
162163 own_user_id: Rc::new(own_user_id.to_owned()),
163 typing_in_flight: Mutex::new(()),
164 typing_in_flight: Rc::new(Mutex::new(())),
164165 inner: room,
165166 buffer_handle,
166167 }
src/server.rs +2 −2
@@ -198,7 +198,7 @@ impl Connection {
198198}
199199
200200#[derive(Clone)]
201pub(crate) struct MatrixServer {
201pub struct MatrixServer {
202202 server_name: Rc<String>,
203203 inner: Rc<RefCell<InnerServer>>,
204204}
@@ -210,7 +210,7 @@ impl std::fmt::Debug for MatrixServer {
210210 }
211211}
212212
213pub(crate) struct InnerServer {
213pub struct InnerServer {
214214 server_name: Rc<String>,
215215 pub room_buffers: HashMap<RoomId, RoomBuffer>,
216216 settings: ServerSettings,