Skip to content
Snippets Groups Projects
Commit d8949d55 authored by Matthias Ahouansou's avatar Matthias Ahouansou Committed by 🥺
Browse files

refactor(state_accessor): add method to check if a user can invite another user

parent 70ce9c29
No related branches found
No related tags found
No related merge requests found
......@@ -18,9 +18,11 @@
},
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 @@ pub fn get_member(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<Ro
.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())
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment