[fractal/fractal-next] session: Create User for the logged in user
- From: Julian Sparber <jsparber src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/fractal-next] session: Create User for the logged in user
- Date: Wed, 5 May 2021 07:43:37 +0000 (UTC)
commit 5f1ec136317cca9205d0578aa03464c647591a9d
Author: Julian Sparber <julian sparber net>
Date: Tue May 4 18:30:09 2021 +0200
session: Create User for the logged in user
src/session/mod.rs | 33 +++++++++++++++++++++++++--------
src/session/room/room.rs | 22 ++++++++++++++++++++--
2 files changed, 45 insertions(+), 10 deletions(-)
---
diff --git a/src/session/mod.rs b/src/session/mod.rs
index f20959f0..2164a5eb 100644
--- a/src/session/mod.rs
+++ b/src/session/mod.rs
@@ -27,7 +27,7 @@ use matrix_sdk::{
self, assign,
deserialized_responses::SyncResponse,
events::{AnyRoomEvent, AnySyncRoomEvent},
- identifiers::RoomId,
+ identifiers::{RoomId, UserId},
Client, ClientConfig, RequestConfig, SyncSettings,
};
use std::time::Duration;
@@ -54,6 +54,7 @@ mod imp {
pub client: OnceCell<Client>,
pub rooms: RefCell<HashMap<RoomId, room::Room>>,
pub categories: Categories,
+ pub user: OnceCell<User>,
}
#[glib::object_subclass]
@@ -205,7 +206,8 @@ impl Session {
CreationMethod::SessionRestore(session) => {
let res = client.restore_login(session).await;
let success = res.is_ok();
- send!(sender, res.map(|_| None));
+ let user_id = client.user_id().await.unwrap();
+ send!(sender, res.map(|_| (user_id, None)));
success
}
CreationMethod::Password(username, password) => {
@@ -213,7 +215,8 @@ impl Session {
.login(&username, &password, None, Some("Fractal Next"))
.await;
let success = response.is_ok();
- send!(sender, response.map(|r| Some(r)));
+ let user_id = client.user_id().await.unwrap();
+ send!(sender, response.map(|r| (user_id, Some(r))));
success
}
};
@@ -248,9 +251,9 @@ impl Session {
});
}
- fn setup(&self) -> glib::SyncSender<matrix_sdk::Result<Option<login::Response>>> {
+ fn setup(&self) -> glib::SyncSender<matrix_sdk::Result<(UserId, Option<login::Response>)>> {
let (sender, receiver) = glib::MainContext::sync_channel::<
- matrix_sdk::Result<Option<login::Response>>,
+ matrix_sdk::Result<(UserId, Option<login::Response>)>,
>(Default::default(), 100);
receiver.attach(
None,
@@ -260,16 +263,20 @@ impl Session {
let priv_ = &imp::Session::from_instance(&obj);
priv_.error.replace(Some(error));
}
- Ok(Some(response)) => {
+ Ok((user_id, Some(response))) => {
let session = matrix_sdk::Session {
access_token: response.access_token,
user_id: response.user_id,
device_id: response.device_id,
};
+ obj.set_user(User::new(&user_id));
+
//TODO: set error to this error
obj.store_session(session).unwrap();
}
- Ok(None) => {}
+ Ok((user_id, None)) => {
+ obj.set_user(User::new(&user_id));
+ }
}
obj.load();
@@ -282,6 +289,16 @@ impl Session {
sender
}
+ fn set_user(&self, user: User) {
+ let priv_ = &imp::Session::from_instance(self);
+ priv_.user.set(user).unwrap();
+ }
+
+ fn user(&self) -> &User {
+ let priv_ = &imp::Session::from_instance(self);
+ priv_.user.get().unwrap()
+ }
+
/// Sets up the required channel to receive new room events
fn create_new_sync_response_sender(&self) -> SyncSender<SyncResponse> {
let (sender, receiver) =
@@ -364,7 +381,7 @@ impl Session {
for room_id in new_rooms_id {
if let Some(matrix_room) = priv_.client.get().unwrap().get_room(&room_id) {
- let room = room::Room::new(matrix_room);
+ let room = room::Room::new(matrix_room, self.user());
rooms_map.insert(room_id.clone(), room.clone());
new_rooms.push(room.clone());
}
diff --git a/src/session/room/room.rs b/src/session/room/room.rs
index dd9dfb95..d461ac21 100644
--- a/src/session/room/room.rs
+++ b/src/session/room/room.rs
@@ -24,6 +24,7 @@ mod imp {
#[derive(Debug, Default)]
pub struct Room {
pub matrix_room: OnceCell<MatrixRoom>,
+ pub user: OnceCell<User>,
pub name: RefCell<Option<String>>,
pub avatar: RefCell<Option<gio::LoadableIcon>>,
pub category: Cell<CategoryType>,
@@ -56,6 +57,13 @@ mod imp {
None,
glib::ParamFlags::READABLE,
),
+ glib::ParamSpec::new_object(
+ "user",
+ "User",
+ "The user of the session that owns this room",
+ User::static_type(),
+ glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY,
+ ),
glib::ParamSpec::new_object(
"avatar",
"Avatar",
@@ -113,6 +121,10 @@ mod imp {
let matrix_room = value.get::<BoxedMatrixRoom>().unwrap();
obj.set_matrix_room(matrix_room.0);
}
+ "user" => {
+ let user = value.get().unwrap();
+ self.user.set(user).unwrap();
+ }
_ => unimplemented!(),
}
}
@@ -120,6 +132,7 @@ mod imp {
fn property(&self, obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
let matrix_room = self.matrix_room.get().unwrap();
match pspec.name() {
+ "user" => obj.user().to_value(),
"display-name" => obj.display_name().to_value(),
"avatar" => self.avatar.borrow().to_value(),
"timeline" => self.timeline.get().unwrap().to_value(),
@@ -151,8 +164,8 @@ glib::wrapper! {
struct BoxedMatrixRoom(MatrixRoom);
impl Room {
- pub fn new(room: MatrixRoom) -> Self {
- glib::Object::new(&[("matrix-room", &BoxedMatrixRoom(room))])
+ pub fn new(room: MatrixRoom, user: &User) -> Self {
+ glib::Object::new(&[("matrix-room", &BoxedMatrixRoom(room)), ("user", user)])
.expect("Failed to create Room")
}
@@ -180,6 +193,11 @@ impl Room {
self.set_category(category);
}
+ pub fn user(&self) -> &User {
+ let priv_ = imp::Room::from_instance(self);
+ priv_.user.get().unwrap()
+ }
+
pub fn category(&self) -> CategoryType {
let priv_ = imp::Room::from_instance(self);
priv_.category.get()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]