[fractal/fractal-next] Specialize User to Member where possible
- From: Julian Sparber <jsparber src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/fractal-next] Specialize User to Member where possible
- Date: Fri, 13 Aug 2021 18:46:41 +0000 (UTC)
commit e52f067c03b416b3ba77016541d40a33acd23623
Author: Kai A. Hiller <V02460 gmail com>
Date: Mon Aug 9 00:38:53 2021 +0200
Specialize User to Member where possible
src/session/room/event.rs | 7 ++--
src/session/room/member.rs | 61 +++++++++++++++++++++++++++++++
src/session/room/room.rs | 24 ++++++-------
src/session/user.rs | 90 ++--------------------------------------------
4 files changed, 78 insertions(+), 104 deletions(-)
---
diff --git a/src/session/room/event.rs b/src/session/room/event.rs
index 2be1fd26..c2e6e123 100644
--- a/src/session/room/event.rs
+++ b/src/session/room/event.rs
@@ -12,7 +12,8 @@ use matrix_sdk::{
},
};
-use crate::session::{Room, User};
+use crate::session::room::Member;
+use crate::session::Room;
use log::warn;
#[derive(Clone, Debug, glib::GBoxed)]
@@ -86,7 +87,7 @@ mod imp {
"sender",
"Sender",
"The sender of this matrix event",
- User::static_type(),
+ Member::static_type(),
glib::ParamFlags::READABLE,
),
glib::ParamSpec::new_object(
@@ -160,7 +161,7 @@ impl Event {
glib::Object::new(&[("event", &event), ("room", room)]).expect("Failed to create Event")
}
- pub fn sender(&self) -> User {
+ pub fn sender(&self) -> Member {
let priv_ = imp::Event::from_instance(self);
priv_
.room
diff --git a/src/session/room/member.rs b/src/session/room/member.rs
index 99d6bb66..6db7d018 100644
--- a/src/session/room/member.rs
+++ b/src/session/room/member.rs
@@ -35,4 +35,65 @@ impl Member {
glib::Object::new(&[("session", &session), ("user-id", &user_id.as_str())])
.expect("Failed to create Member")
}
+
+ /// Update the user based on the the room member state event
+ pub fn update_from_room_member(&self, member: &RoomMember) {
+ if member.user_id() != self.user_id() {
+ return;
+ };
+
+ let display_name = member.display_name().map(|name| name.to_owned());
+ self.avatar().set_url(member.avatar_url().cloned());
+
+ if Some(self.display_name()) != display_name {
+ self.set_display_name(display_name);
+ }
+ }
+
+ /// Update the user based on the the room member state event
+ pub fn update_from_member_event(&self, event: &SyncStateEvent<MemberEventContent>) {
+ if &event.sender != self.user_id() {
+ return;
+ };
+
+ let display_name = if let Some(display_name) = &event.content.displayname {
+ Some(display_name.to_owned())
+ } else {
+ event
+ .content
+ .third_party_invite
+ .as_ref()
+ .map(|i| i.display_name.to_owned())
+ };
+
+ self.avatar().set_url(event.content.avatar_url.to_owned());
+
+ if Some(self.display_name()) != display_name {
+ self.set_display_name(display_name);
+ }
+ }
+
+ /// Update the user based on the the stripped room member state event
+ pub fn update_from_stripped_member_event(
+ &self,
+ event: &StrippedStateEvent<MemberEventContent>,
+ ) {
+ if &event.sender != self.user_id() {
+ return;
+ };
+
+ let display_name = match &event.content.displayname {
+ Some(display_name) => Some(display_name.to_owned()),
+ None => event
+ .content
+ .third_party_invite
+ .as_ref()
+ .map(|i| i.display_name.to_owned()),
+ };
+ self.avatar().set_url(event.content.avatar_url.to_owned());
+
+ if Some(self.display_name()) != display_name {
+ self.set_display_name(display_name)
+ }
+ }
}
diff --git a/src/session/room/room.rs b/src/session/room/room.rs
index e8e3ad05..e7d392e4 100644
--- a/src/session/room/room.rs
+++ b/src/session/room/room.rs
@@ -31,10 +31,8 @@ use std::convert::TryFrom;
use crate::components::{LabelWithWidgets, Pill};
use crate::prelude::*;
-use crate::session::{
- room::{Event, HighlightFlags, RoomType, Timeline},
- Avatar, Session, User,
-};
+use crate::session::room::{Event, HighlightFlags, Member, RoomType, Timeline};
+use crate::session::{Avatar, Session};
use crate::utils::do_async;
use crate::Error;
use crate::RUNTIME;
@@ -54,9 +52,9 @@ mod imp {
pub avatar: OnceCell<Avatar>,
pub category: Cell<RoomType>,
pub timeline: OnceCell<Timeline>,
- pub room_members: RefCell<HashMap<UserId, User>>,
+ pub room_members: RefCell<HashMap<UserId, Member>>,
/// The user who sent the invite to this room. This is only set when this room is an invitiation.
- pub inviter: RefCell<Option<User>>,
+ pub inviter: RefCell<Option<Member>>,
pub members_loaded: Cell<bool>,
}
@@ -96,7 +94,7 @@ mod imp {
"inviter",
"Inviter",
"The user who sent the invite to this room, this is only set when this room
represents an invite",
- User::static_type(),
+ Member::static_type(),
glib::ParamFlags::READABLE,
),
glib::ParamSpec::new_object(
@@ -488,7 +486,7 @@ impl Room {
.filter(|topic| !topic.is_empty() && topic.find(|c: char| !c.is_whitespace()).is_some())
}
- pub fn inviter(&self) -> Option<User> {
+ pub fn inviter(&self) -> Option<Member> {
let priv_ = imp::Room::from_instance(self);
priv_.inviter.borrow().clone()
}
@@ -496,13 +494,13 @@ impl Room {
/// Returns the room member `User` object
///
/// The returned `User` is specific to this room
- pub fn member_by_id(&self, user_id: &UserId) -> User {
+ pub fn member_by_id(&self, user_id: &UserId) -> Member {
let priv_ = imp::Room::from_instance(self);
let mut room_members = priv_.room_members.borrow_mut();
room_members
.entry(user_id.clone())
- .or_insert_with(|| User::new(self.session(), user_id))
+ .or_insert_with(|| Member::new(self, user_id))
.clone()
}
@@ -533,7 +531,7 @@ impl Room {
}
});
- let inviter = User::new(self.session(), inviter_id);
+ let inviter = Member::new(self, inviter_id);
if let Some(AnyStrippedStateEvent::RoomMember(event)) = inviter_event {
inviter.update_from_stripped_member_event(event);
}
@@ -583,7 +581,7 @@ impl Room {
let user_id = member.user_id();
let user = room_members
.entry(user_id.clone())
- .or_insert_with(|| User::new(self.session(), user_id));
+ .or_insert_with(|| Member::new(self, user_id));
user.update_from_room_member(&member);
}
}
@@ -595,7 +593,7 @@ impl Room {
let user_id = &event.sender;
let user = room_members
.entry(user_id.clone())
- .or_insert_with(|| User::new(self.session(), user_id));
+ .or_insert_with(|| Member::new(self, user_id));
user.update_from_member_event(event);
}
diff --git a/src/session/user.rs b/src/session/user.rs
index ab0c87d8..4a6724fa 100644
--- a/src/session/user.rs
+++ b/src/session/user.rs
@@ -1,15 +1,7 @@
use gtk::{glib, prelude::*, subclass::prelude::*};
+use matrix_sdk::ruma::identifiers::{MxcUri, UserId};
-use crate::session::Session;
-use matrix_sdk::{
- ruma::{
- events::{room::member::MemberEventContent, StrippedStateEvent, SyncStateEvent},
- identifiers::{MxcUri, UserId},
- },
- RoomMember,
-};
-
-use crate::session::Avatar;
+use crate::session::{Avatar, Session};
mod imp {
use super::*;
@@ -157,84 +149,6 @@ pub trait UserExt: IsA<User> {
fn set_avatar_url(&self, url: Option<MxcUri>) {
self.avatar().set_url(url);
}
-
- /// Update the user based on the the room member state event
- fn update_from_room_member(&self, member: &RoomMember) {
- let priv_ = imp::User::from_instance(self.upcast_ref());
-
- let user_id = priv_.user_id.get().unwrap();
- if member.user_id().as_str() != user_id {
- return;
- };
-
- //let content = event.content;
- let display_name = member.display_name().map(|name| name.to_owned());
- self.avatar().set_url(member.avatar_url().cloned());
-
- if *priv_.display_name.borrow() != display_name {
- self.set_display_name(display_name);
- }
- }
-
- /// Update the user based on the the room member state event
- fn update_from_member_event(&self, event: &SyncStateEvent<MemberEventContent>) {
- let priv_ = imp::User::from_instance(self.upcast_ref());
- let user_id = priv_.user_id.get().unwrap();
- if event.sender.as_str() != user_id {
- return;
- };
-
- let display_name = if let Some(display_name) = &event.content.displayname {
- Some(display_name.to_owned())
- } else {
- event
- .content
- .third_party_invite
- .as_ref()
- .map(|i| i.display_name.to_owned())
- };
-
- self.avatar().set_url(event.content.avatar_url.to_owned());
-
- if *priv_.display_name.borrow() != display_name {
- self.set_display_name(display_name);
- }
- }
-
- /// Update the user based on the the stripped room member state event
- fn update_from_stripped_member_event(&self, event: &StrippedStateEvent<MemberEventContent>) {
- let changed = {
- let priv_ = imp::User::from_instance(self.upcast_ref());
- let user_id = priv_.user_id.get().unwrap();
- if event.sender.as_str() != user_id {
- return;
- };
-
- let display_name = if let Some(display_name) = &event.content.displayname {
- Some(display_name.to_owned())
- } else {
- event
- .content
- .third_party_invite
- .as_ref()
- .map(|i| i.display_name.to_owned())
- };
-
- self.avatar().set_url(event.content.avatar_url.to_owned());
-
- let mut current_display_name = priv_.display_name.borrow_mut();
- if *current_display_name != display_name {
- *current_display_name = display_name;
- true
- } else {
- false
- }
- };
-
- if changed {
- self.notify("display-name");
- }
- }
}
impl<T: IsA<User>> UserExt for T {}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]