mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 23:31:24 +01:00
refactor calculate_invite_state
That was terribly named and terribly implemented. Co-authored-by: Charles Hall <charles@computer.surgery>
This commit is contained in:
parent
e14b7f28f2
commit
287f6b9163
3 changed files with 34 additions and 49 deletions
|
|
@ -1307,7 +1307,7 @@ pub(crate) async fn invite_helper(
|
|||
)?;
|
||||
|
||||
let invite_room_state =
|
||||
services().rooms.state.calculate_invite_state(&pdu)?;
|
||||
services().rooms.state.get_helpful_invite_events(&pdu)?;
|
||||
|
||||
drop(room_token);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
mod data;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
iter,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
|
|
@ -266,58 +267,42 @@ impl Service {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gather events to help the invited user identify the room
|
||||
///
|
||||
/// Also includes the invite event itself.
|
||||
#[tracing::instrument(skip(self, invite_event))]
|
||||
pub(crate) fn calculate_invite_state(
|
||||
pub(crate) fn get_helpful_invite_events(
|
||||
&self,
|
||||
invite_event: &PduEvent,
|
||||
) -> Result<Vec<Raw<AnyStrippedStateEvent>>> {
|
||||
let mut state = Vec::new();
|
||||
// Add recommended events
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomCreate,
|
||||
"",
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomJoinRules,
|
||||
"",
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomCanonicalAlias,
|
||||
"",
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomAvatar,
|
||||
"",
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomName,
|
||||
"",
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
if let Some(e) = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&StateEventType::RoomMember,
|
||||
invite_event.sender.as_str(),
|
||||
)? {
|
||||
state.push(e.to_stripped_state_event());
|
||||
}
|
||||
let helpful_events = [
|
||||
(StateEventType::RoomCreate, ""),
|
||||
(StateEventType::RoomJoinRules, ""),
|
||||
(StateEventType::RoomCanonicalAlias, ""),
|
||||
(StateEventType::RoomAvatar, ""),
|
||||
(StateEventType::RoomName, ""),
|
||||
(StateEventType::RoomMember, invite_event.sender.as_str()),
|
||||
];
|
||||
|
||||
state.push(invite_event.to_stripped_state_event());
|
||||
Ok(state)
|
||||
let helpful_events =
|
||||
helpful_events.into_iter().filter_map(|(state_type, state_key)| {
|
||||
let state = services().rooms.state_accessor.room_state_get(
|
||||
&invite_event.room_id,
|
||||
&state_type,
|
||||
state_key,
|
||||
);
|
||||
|
||||
match state {
|
||||
Ok(Some(x)) => Some(Ok(x.to_stripped_state_event())),
|
||||
Err(x) => Some(Err(x)),
|
||||
Ok(None) => None,
|
||||
}
|
||||
});
|
||||
|
||||
let actual_event =
|
||||
iter::once(Ok(invite_event.to_stripped_state_event()));
|
||||
|
||||
helpful_events.chain(actual_event).collect()
|
||||
}
|
||||
|
||||
/// Set the state hash to a new version, but does not update state_cache.
|
||||
|
|
|
|||
|
|
@ -473,7 +473,7 @@ impl Service {
|
|||
let state = services()
|
||||
.rooms
|
||||
.state
|
||||
.calculate_invite_state(pdu)?;
|
||||
.get_helpful_invite_events(pdu)?;
|
||||
Some(state)
|
||||
}
|
||||
_ => None,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue