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:
Matthias Ahouansou 2024-04-06 10:13:06 -04:00 committed by June
parent 70ce9c299e
commit d8949d55c4

View file

@ -18,9 +18,11 @@ use ruma::{
},
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
};
use serde_json::value::to_raw_value;
use tokio::sync::MutexGuard;
use tracing::{error, warn};
use crate::{services, Error, PduEvent, Result};
use crate::{service::pdu::PduBuilder, services, Error, PduEvent, Result};
pub struct Service {
pub db: &'static dyn Data,
@ -269,4 +271,25 @@ impl Service {
.map_err(|_| Error::bad_database("Invalid room member event in database."))
})
}
pub async fn user_can_invite(
&self, room_id: &RoomId, sender: &UserId, target_user: &UserId, state_lock: &MutexGuard<'_, ()>,
) -> Result<bool> {
let content = to_raw_value(&RoomMemberEventContent::new(MembershipState::Invite))
.expect("Event content always serializes");
let new_event = PduBuilder {
event_type: ruma::events::TimelineEventType::RoomMember,
content,
unsigned: None,
state_key: Some(target_user.into()),
redacts: None,
};
Ok(services()
.rooms
.timeline
.create_hash_and_sign_event(new_event, sender, room_id, state_lock)
.is_ok())
}
}