[fractal/fractal-next] member: Refactor update from events
- From: Julian Sparber <jsparber src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/fractal-next] member: Refactor update from events
- Date: Fri, 13 Aug 2021 18:46:41 +0000 (UTC)
commit a5883494d56cee044e9dd1403b1f9bb89a0f2636
Author: Kai A. Hiller <V02460 gmail com>
Date: Mon Aug 9 10:03:17 2021 +0200
member: Refactor update from events
src/session/room/member.rs | 78 ++++++++++++++++++++++------------------------
src/session/room/room.rs | 2 +-
src/session/user.rs | 5 ++-
3 files changed, 43 insertions(+), 42 deletions(-)
---
diff --git a/src/session/room/member.rs b/src/session/room/member.rs
index 6db7d018..c848ad81 100644
--- a/src/session/room/member.rs
+++ b/src/session/room/member.rs
@@ -2,7 +2,7 @@ use gtk::glib;
use gtk::subclass::prelude::*;
use matrix_sdk::ruma::events::room::member::MemberEventContent;
use matrix_sdk::ruma::events::{StrippedStateEvent, SyncStateEvent};
-use matrix_sdk::ruma::identifiers::UserId;
+use matrix_sdk::ruma::identifiers::{MxcUri, UserId};
use matrix_sdk::RoomMember;
use crate::prelude::*;
@@ -39,61 +39,59 @@ impl 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() {
+ log::error!("Tried Member update from RoomMember with wrong user ID.");
return;
};
- let display_name = member.display_name().map(|name| name.to_owned());
+ self.set_display_name(member.display_name().map(String::from));
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() {
+ pub fn update_from_member_event(&self, event: &impl MemberEvent) {
+ if event.sender() != self.user_id() {
+ log::error!("Tried Member update from MemberEvent with wrong 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.set_display_name(event.display_name());
+ self.avatar().set_url(event.avatar_url());
+ }
+}
- self.avatar().set_url(event.content.avatar_url.to_owned());
+pub trait MemberEvent {
+ fn sender(&self) -> &UserId;
+ fn content(&self) -> &MemberEventContent;
- if Some(self.display_name()) != display_name {
- self.set_display_name(display_name);
- }
+ fn avatar_url(&self) -> Option<MxcUri> {
+ self.content().avatar_url.clone()
}
- /// 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
+ fn display_name(&self) -> Option<String> {
+ match &self.content().displayname {
+ Some(display_name) => Some(display_name.clone()),
+ None => self
+ .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)
+ .map(|i| i.display_name.clone()),
}
}
}
+
+impl MemberEvent for SyncStateEvent<MemberEventContent> {
+ fn sender(&self) -> &UserId {
+ &self.sender
+ }
+ fn content(&self) -> &MemberEventContent {
+ &self.content
+ }
+}
+impl MemberEvent for StrippedStateEvent<MemberEventContent> {
+ fn sender(&self) -> &UserId {
+ &self.sender
+ }
+ fn content(&self) -> &MemberEventContent {
+ &self.content
+ }
+}
diff --git a/src/session/room/room.rs b/src/session/room/room.rs
index e7d392e4..3538987e 100644
--- a/src/session/room/room.rs
+++ b/src/session/room/room.rs
@@ -533,7 +533,7 @@ impl Room {
let inviter = Member::new(self, inviter_id);
if let Some(AnyStrippedStateEvent::RoomMember(event)) = inviter_event {
- inviter.update_from_stripped_member_event(event);
+ inviter.update_from_member_event(event);
}
priv_.inviter.replace(Some(inviter));
diff --git a/src/session/user.rs b/src/session/user.rs
index 4a6724fa..21c56b09 100644
--- a/src/session/user.rs
+++ b/src/session/user.rs
@@ -39,7 +39,7 @@ mod imp {
"Display Name",
"The display name of the user",
None,
- glib::ParamFlags::READWRITE,
+ glib::ParamFlags::READWRITE | glib::ParamFlags::EXPLICIT_NOTIFY,
),
glib::ParamSpec::new_object(
"avatar",
@@ -136,6 +136,9 @@ pub trait UserExt: IsA<User> {
}
fn set_display_name(&self, display_name: Option<String>) {
+ if Some(self.display_name()) == display_name {
+ return;
+ }
let priv_ = imp::User::from_instance(self.upcast_ref());
priv_.display_name.replace(display_name);
self.notify("display-name");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]