update_member, update_power methods for room_member
parent
d4c9bb3cec
commit
0147e8c0ac
|
@ -24,6 +24,7 @@ use crate::events::room::{
|
||||||
canonical_alias::CanonicalAliasEvent,
|
canonical_alias::CanonicalAliasEvent,
|
||||||
member::{MemberEvent, MembershipState},
|
member::{MemberEvent, MembershipState},
|
||||||
name::NameEvent,
|
name::NameEvent,
|
||||||
|
power_levels::PowerLevelsEvent,
|
||||||
};
|
};
|
||||||
use crate::events::{
|
use crate::events::{
|
||||||
presence::{PresenceEvent, PresenceEventContent},
|
presence::{PresenceEvent, PresenceEventContent},
|
||||||
|
@ -188,10 +189,8 @@ impl Room {
|
||||||
match &event.content.membership {
|
match &event.content.membership {
|
||||||
MembershipState::Invite | MembershipState::Join => self.add_member(event),
|
MembershipState::Invite | MembershipState::Join => self.add_member(event),
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(member) = self.members.get_mut(&event.sender.to_string()) {
|
if let Some(member) = self.members.get_mut(&event.state_key) {
|
||||||
let changed = member.membership == event.content.membership;
|
member.update_member(event)
|
||||||
member.membership = event.content.membership;
|
|
||||||
changed
|
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@ -247,6 +246,17 @@ impl Room {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle a room.power_levels event, updating the room state if necessary.
|
||||||
|
///
|
||||||
|
/// Returns true if the room name changed, false otherwise.
|
||||||
|
pub fn handle_power_level(&mut self, event: &PowerLevelsEvent) -> bool {
|
||||||
|
if let Some(member) = self.members.get_mut(&event.state_key) {
|
||||||
|
member.update_power(event)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Receive a timeline event for this room and update the room state.
|
/// Receive a timeline event for this room and update the room state.
|
||||||
///
|
///
|
||||||
/// Returns true if the joined member list changed, false otherwise.
|
/// Returns true if the joined member list changed, false otherwise.
|
||||||
|
@ -263,7 +273,7 @@ impl Room {
|
||||||
RoomEvent::RoomCanonicalAlias(ca) => self.handle_canonical(ca),
|
RoomEvent::RoomCanonicalAlias(ca) => self.handle_canonical(ca),
|
||||||
RoomEvent::RoomAliases(a) => self.handle_room_aliases(a),
|
RoomEvent::RoomAliases(a) => self.handle_room_aliases(a),
|
||||||
// power levels of the room members
|
// power levels of the room members
|
||||||
// RoomEvent::RoomPowerLevels(p) => self.handle_power_level(p),
|
RoomEvent::RoomPowerLevels(p) => self.handle_power_level(p),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
use super::{User, UserId};
|
use super::User;
|
||||||
use crate::api::r0 as api;
|
use crate::api::r0 as api;
|
||||||
use crate::events::collections::all::{Event, RoomEvent, StateEvent};
|
use crate::events::collections::all::{Event, RoomEvent, StateEvent};
|
||||||
use crate::events::room::{
|
use crate::events::room::{
|
||||||
|
@ -24,9 +24,10 @@ use crate::events::room::{
|
||||||
canonical_alias::CanonicalAliasEvent,
|
canonical_alias::CanonicalAliasEvent,
|
||||||
member::{MemberEvent, MemberEventContent, MembershipState},
|
member::{MemberEvent, MemberEventContent, MembershipState},
|
||||||
name::NameEvent,
|
name::NameEvent,
|
||||||
|
power_levels::PowerLevelsEvent,
|
||||||
};
|
};
|
||||||
use crate::events::EventResult;
|
use crate::events::EventResult;
|
||||||
use crate::identifiers::RoomAliasId;
|
use crate::identifiers::{RoomAliasId, UserId};
|
||||||
use crate::session::Session;
|
use crate::session::Session;
|
||||||
|
|
||||||
use js_int::{Int, UInt};
|
use js_int::{Int, UInt};
|
||||||
|
@ -66,7 +67,7 @@ impl RoomMember {
|
||||||
let user = User::new(event);
|
let user = User::new(event);
|
||||||
Self {
|
Self {
|
||||||
room_id: event.room_id.as_ref().map(|id| id.to_string()),
|
room_id: event.room_id.as_ref().map(|id| id.to_string()),
|
||||||
user_id: event.state_key.clone(),
|
user_id: event.sender.clone(),
|
||||||
typing: None,
|
typing: None,
|
||||||
user,
|
user,
|
||||||
power_level: None,
|
power_level: None,
|
||||||
|
@ -77,34 +78,31 @@ impl RoomMember {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, event: &MemberEvent) {
|
pub fn update_member(&mut self, event: &MemberEvent) -> bool {
|
||||||
let MemberEvent {
|
let changed = self.membership == event.content.membership;
|
||||||
content: MemberEventContent { membership, .. },
|
self.membership = event.content.membership;
|
||||||
room_id,
|
changed
|
||||||
state_key,
|
}
|
||||||
..
|
|
||||||
} = event;
|
|
||||||
|
|
||||||
let mut events = Vec::new();
|
pub fn update_power(&mut self, event: &PowerLevelsEvent) -> bool {
|
||||||
events.extend(
|
let mut max_power = event.content.users_default;
|
||||||
self.events
|
for power in event.content.users.values() {
|
||||||
.drain(..)
|
max_power = *power.max(&max_power);
|
||||||
.chain(Some(Event::RoomMember(event.clone()))),
|
|
||||||
);
|
|
||||||
|
|
||||||
*self = Self {
|
|
||||||
room_id: room_id
|
|
||||||
.as_ref()
|
|
||||||
.map(|id| id.to_string())
|
|
||||||
.or(self.room_id.take()),
|
|
||||||
user_id: state_key.clone(),
|
|
||||||
typing: None,
|
|
||||||
user: User::new(event),
|
|
||||||
power_level: None,
|
|
||||||
power_level_norm: None,
|
|
||||||
membership: membership.clone(),
|
|
||||||
name: state_key.clone(),
|
|
||||||
events,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut changed = false;
|
||||||
|
if let Some(user_power) = event.content.users.get(&self.user_id) {
|
||||||
|
changed = self.power_level == Some(*user_power);
|
||||||
|
self.power_level = Some(*user_power);
|
||||||
|
} else {
|
||||||
|
changed = self.power_level == Some(event.content.users_default);
|
||||||
|
self.power_level = Some(event.content.users_default);
|
||||||
|
}
|
||||||
|
|
||||||
|
if max_power > Int::from(0) {
|
||||||
|
self.power_level_norm = Some((self.power_level.unwrap() * Int::from(100)) / max_power);
|
||||||
|
}
|
||||||
|
|
||||||
|
changed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue