From 5fe13335a171c264839f79c6eb0dbf52eea7d5a3 Mon Sep 17 00:00:00 2001 From: Adrien Date: Sun, 31 Dec 2023 15:21:49 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Factorize=20Room=20creation=20fr?= =?UTF-8?q?om=20MatrixRoom=20instances?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base.rs | 13 +++++++++++-- src/matrix_interface/client.rs | 20 ++------------------ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/base.rs b/src/base.rs index 47c32c5..6a13266 100644 --- a/src/base.rs +++ b/src/base.rs @@ -7,9 +7,8 @@ use std::{collections::HashMap, sync::Arc}; use dioxus::prelude::*; use fermi::*; -use matrix_sdk::room::Room as MatrixRoom; use matrix_sdk::{ - room::RoomMember, + room::{Room as MatrixRoom, RoomMember}, ruma::{OwnedRoomId, OwnedUserId}, }; use tokio::select; @@ -63,6 +62,16 @@ impl Room { } } + pub async fn from_matrix_room(matrix_room: &MatrixRoom) -> Self { + let room_topic = matrix_room.topic().map(RefCell::new); + + Self::new( + Arc::new(matrix_room.to_owned()), + room_topic, + matrix_room.is_direct().await.ok(), + ) + } + pub fn name(&self) -> Option { self.matrix_room.name() } diff --git a/src/matrix_interface/client.rs b/src/matrix_interface/client.rs index b93562a..ac3a7d3 100644 --- a/src/matrix_interface/client.rs +++ b/src/matrix_interface/client.rs @@ -110,12 +110,7 @@ impl Client { if ev.state_key == matrix_client.user_id().unwrap() { if matrix_room.state() == MatrixRoomState::Invited { let room_id = matrix_room.room_id(); - let room_topic = matrix_room.topic().map(RefCell::new); - let room = Room::new( - Arc::new(matrix_room.to_owned()), - room_topic, - matrix_room.is_direct().await.ok(), - ); + let room = Room::from_matrix_room(&matrix_room).await; if let Err(err) = senders .room_sender @@ -152,22 +147,11 @@ impl Client { matrix_room: MatrixRoom, senders: Ctx, ) { - error!("== on_room_member_event =="); - // dbg!(&matrix_room); - dbg!(matrix_room.room_id()); - - dbg!(ev.membership()); - if let SyncStateEvent::Original(_ev) = ev { let room_sender = &senders.room_sender; let room_id = matrix_room.room_id(); - let room_topic = matrix_room.topic().map(RefCell::new); - let room = Room::new( - Arc::new(matrix_room.to_owned()), - room_topic, - matrix_room.is_direct().await.ok(), - ); + let room = Room::from_matrix_room(&matrix_room).await; if let Err(err) = room_sender.send(RoomEvent::MemberEvent(room_id.to_owned(), room)) { error!(