1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-10-15 19:42:07 +00:00

refactor: move duplicate code and some other small optimizations

This commit is contained in:
Matthias Ahouansou 2025-03-01 19:17:32 +00:00
parent 8acacdebc8
commit f4d90e9989
No known key found for this signature in database
9 changed files with 283 additions and 354 deletions

View file

@ -10,6 +10,7 @@ use ruma::{
events::{
room::{create::RoomCreateEventContent, member::MembershipState},
AnyStrippedStateEvent, StateEventType, TimelineEventType,
RECOMMENDED_STRIPPED_STATE_EVENT_TYPES,
},
serde::Raw,
state_res::{self, StateMap},
@ -258,58 +259,22 @@ impl Service {
}
}
#[tracing::instrument(skip(self, invite_event))]
pub fn calculate_invite_state(
&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());
}
state.push(invite_event.to_stripped_state_event());
Ok(state)
#[tracing::instrument(skip(self, room_id))]
/// Gets all the [recommended stripped state events] from the given room
///
/// [recommended stripped state events]: https://spec.matrix.org/v1.13/client-server-api/#stripped-state
pub fn stripped_state(&self, room_id: &RoomId) -> Result<Vec<Raw<AnyStrippedStateEvent>>> {
RECOMMENDED_STRIPPED_STATE_EVENT_TYPES
.iter()
.filter_map(|state_event_type| {
services()
.rooms
.state_accessor
.room_state_get(room_id, state_event_type, "")
.transpose()
})
.map(|e| e.map(|e| e.to_stripped_state_event()))
.collect::<Result<Vec<_>>>()
}
/// Set the state hash to a new version, but does not update state_cache.

View file

@ -40,6 +40,13 @@ impl Service {
// TODO: displayname, avatar url
}
// We don't need to store stripped state on behalf of remote users, since these events are only used on `/sync`
let last_state = if user_id.server_name() == services().globals.server_name() {
last_state
} else {
None
};
match &membership {
MembershipState::Join => {
// Check if the user never joined this room

View file

@ -450,9 +450,11 @@ impl Service {
let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
.map_err(|_| Error::bad_database("Invalid content in pdu."))?;
let invite_state = match content.membership {
let stripped_state = match content.membership {
MembershipState::Invite => {
let state = services().rooms.state.calculate_invite_state(pdu)?;
let mut state = services().rooms.state.stripped_state(&pdu.room_id)?;
// So that clients can get info about who invitied them, the reason, when, etc.
state.push(pdu.to_stripped_state_event());
Some(state)
}
_ => None,
@ -465,7 +467,7 @@ impl Service {
&target_user_id,
content.membership,
&pdu.sender,
invite_state,
stripped_state,
true,
)?;
}