refactor(state_accessor): add method to check if a user can invite another user
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
a8452f3ae1
commit
f0b91461a0
1 changed files with 41 additions and 15 deletions
|
@ -13,15 +13,15 @@ use ruma::{
|
||||||
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
|
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
|
||||||
member::{MembershipState, RoomMemberEventContent},
|
member::{MembershipState, RoomMemberEventContent},
|
||||||
name::RoomNameEventContent,
|
name::RoomNameEventContent,
|
||||||
power_levels::RoomPowerLevelsEventContent,
|
|
||||||
},
|
},
|
||||||
StateEventType,
|
StateEventType,
|
||||||
},
|
},
|
||||||
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
|
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
|
||||||
};
|
};
|
||||||
use tracing::error;
|
use serde_json::value::to_raw_value;
|
||||||
|
use tracing::{error, warn};
|
||||||
|
|
||||||
use crate::{services, Error, PduEvent, Result};
|
use crate::{service::pdu::PduBuilder, services, Error, PduEvent, Result};
|
||||||
|
|
||||||
pub struct Service {
|
pub struct Service {
|
||||||
pub db: &'static dyn Data,
|
pub db: &'static dyn Data,
|
||||||
|
@ -140,19 +140,45 @@ impl Service {
|
||||||
Ok(visibility)
|
Ok(visibility)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether a user's power level is sufficient to invite other users
|
pub async fn user_can_invite(&self, room_id: &RoomId, sender: &UserId, state_key: &UserId) -> Result<bool> {
|
||||||
pub fn user_can_invite(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
let content = self
|
||||||
if services().rooms.state_cache.is_joined(user_id, room_id)? {
|
.room_state_get(room_id, &StateEventType::RoomMember, state_key.as_str())?
|
||||||
self.room_state_get(room_id, &StateEventType::RoomPowerLevels, "")?
|
.map(|prev| {
|
||||||
.map_or(Ok(false), |pdu_event| {
|
serde_json::from_str(prev.content.get()).map(|mut content: RoomMemberEventContent| {
|
||||||
serde_json::from_str(pdu_event.content.get()).map(|content: RoomPowerLevelsEventContent| {
|
content.membership = MembershipState::Invite;
|
||||||
content.users.get(user_id).unwrap_or(&content.users_default) >= &content.invite
|
content.join_authorized_via_users_server = None;
|
||||||
|
content
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.map_err(|_| Error::bad_database("Invalid history visibility event in database."))
|
.transpose()
|
||||||
} else {
|
.map_err(|_| Error::BadDatabase("Incorrect state event type stored"))?
|
||||||
Ok(false)
|
.unwrap_or(RoomMemberEventContent::new(MembershipState::Invite));
|
||||||
}
|
let content = to_raw_value(&content).expect("Event content always serializes");
|
||||||
|
|
||||||
|
let new_event = PduBuilder {
|
||||||
|
event_type: ruma::events::TimelineEventType::RoomMember,
|
||||||
|
content,
|
||||||
|
unsigned: None,
|
||||||
|
state_key: Some(state_key.into()),
|
||||||
|
redacts: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mutex_state = Arc::clone(
|
||||||
|
services()
|
||||||
|
.globals
|
||||||
|
.roomid_mutex_state
|
||||||
|
.write()
|
||||||
|
.await
|
||||||
|
.entry(room_id.to_owned())
|
||||||
|
.or_default(),
|
||||||
|
);
|
||||||
|
let state_lock = mutex_state.lock().await;
|
||||||
|
|
||||||
|
services()
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.create_hash_and_sign_event(new_event, sender, room_id, &state_lock)
|
||||||
|
.map(|_| true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Whether a user is allowed to see an event, based on
|
/// Whether a user is allowed to see an event, based on
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue