1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::sync::Arc;
use ruma::{
events::{
presence::PresenceEvent,
room::{member::MembershipState, power_levels::SyncRoomPowerLevelsEvent},
},
MxcUri, UserId,
};
use crate::{deserialized_responses::MemberEvent, MinimalRoomMemberEvent};
#[derive(Clone, Debug)]
pub struct RoomMember {
pub(crate) event: Arc<MemberEvent>,
pub(crate) profile: Arc<Option<MinimalRoomMemberEvent>>,
#[allow(dead_code)]
pub(crate) presence: Arc<Option<PresenceEvent>>,
pub(crate) power_levels: Arc<Option<SyncRoomPowerLevelsEvent>>,
pub(crate) max_power_level: i64,
pub(crate) is_room_creator: bool,
pub(crate) display_name_ambiguous: bool,
}
impl RoomMember {
pub fn user_id(&self) -> &UserId {
self.event.user_id()
}
pub fn display_name(&self) -> Option<&str> {
if let Some(p) = self.profile.as_ref() {
p.as_original().and_then(|e| e.content.displayname.as_deref())
} else {
self.event.original_content()?.displayname.as_deref()
}
}
pub fn name(&self) -> &str {
if let Some(d) = self.display_name() {
d
} else {
self.user_id().localpart()
}
}
pub fn avatar_url(&self) -> Option<&MxcUri> {
if let Some(p) = self.profile.as_ref() {
p.as_original().and_then(|e| e.content.avatar_url.as_deref())
} else {
self.event.original_content()?.avatar_url.as_deref()
}
}
pub fn normalized_power_level(&self) -> i64 {
if self.max_power_level > 0 {
(self.power_level() * 100) / self.max_power_level
} else {
self.power_level()
}
}
pub fn power_level(&self) -> i64 {
(*self.power_levels)
.as_ref()
.map(|e| e.power_levels().for_user(self.user_id()).into())
.unwrap_or_else(|| if self.is_room_creator { 100 } else { 0 })
}
pub fn name_ambiguous(&self) -> bool {
self.display_name_ambiguous
}
pub fn membership(&self) -> &MembershipState {
if let Some(p) = self.profile.as_ref() {
p.membership()
} else {
self.event.membership()
}
}
}