refactor: Do not extract members for room each time

This commit is contained in:
Andriy Kushnir (Orhideous) 2022-11-28 01:58:25 +02:00
parent 297c716807
commit 65d7df290f
No known key found for this signature in database
GPG key ID: 62E078AB621B0D15
2 changed files with 24 additions and 17 deletions

View file

@ -1084,8 +1084,9 @@ fn get_missing_events(
if event_room_id != room_id {
warn!(
"Evil event detected: Event {} found while searching in room {}",
queued_events[i], room_id
?room_id,
evil_event = ?queued_events[i],
"Evil event detected while searching in room"
);
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
@ -1093,9 +1094,26 @@ fn get_missing_events(
));
}
let (room_members, room_errors): (Vec<_>, Vec<_>) = services()
.rooms
.state_cache
.room_members(room_id)
.partition(Result::is_ok);
// Just log errors and continue with correct users
if !room_errors.is_empty() {
warn!(?room_id, "Some errors occurred when fetching room members");
}
let current_server_members: Vec<OwnedUserId> = room_members
.into_iter()
.map(Result::unwrap)
.filter(|member| member.server_name() == sender_servername)
.collect();
let event_is_visible = services().rooms.state_accessor.server_can_see_event(
sender_servername,
room_id,
current_server_members.as_slice(),
&queued_events[i],
)?;

View file

@ -12,8 +12,9 @@ use ruma::{
},
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
};
use tracing::warn;
use crate::{services, PduEvent, Result};
use crate::{PduEvent, Result};
pub struct Service {
pub db: &'static dyn Data,
@ -77,7 +78,7 @@ impl Service {
pub fn server_can_see_event(
&self,
server_name: &ServerName,
room_id: &RoomId,
current_server_members: &[OwnedUserId],
event_id: &EventId,
) -> Result<bool> {
let shortstatehash = match self.pdu_shortstatehash(event_id) {
@ -94,18 +95,6 @@ impl Service {
return Ok(*visibility);
}
let current_server_members: Vec<OwnedUserId> = services()
.rooms
.state_cache
.room_members(room_id)
.filter(|member| {
member
.as_ref()
.map(|member| member.server_name() == server_name)
.unwrap_or(true)
})
.collect::<Result<_>>()?;
let history_visibility = self
.state_get_content(shortstatehash, &StateEventType::RoomHistoryVisibility, "")?
.map(|content| match content.get("history_visibility") {