add `is_world_readable` state_accessor func, use `self` instead of `services()`
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
1cc7cf54a7
commit
bfbb29dded
|
@ -14,7 +14,6 @@ use ruma::{
|
||||||
canonical_alias::RoomCanonicalAliasEventContent,
|
canonical_alias::RoomCanonicalAliasEventContent,
|
||||||
create::RoomCreateEventContent,
|
create::RoomCreateEventContent,
|
||||||
guest_access::{GuestAccess, RoomGuestAccessEventContent},
|
guest_access::{GuestAccess, RoomGuestAccessEventContent},
|
||||||
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
|
|
||||||
join_rules::{JoinRule, RoomJoinRulesEventContent},
|
join_rules::{JoinRule, RoomJoinRulesEventContent},
|
||||||
topic::RoomTopicEventContent,
|
topic::RoomTopicEventContent,
|
||||||
},
|
},
|
||||||
|
@ -261,21 +260,7 @@ pub(crate) async fn get_public_rooms_filtered_helper(
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap_or(None),
|
.unwrap_or(None),
|
||||||
world_readable: services()
|
world_readable: services().rooms.state_accessor.is_world_readable(&room_id)?,
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(&room_id, &StateEventType::RoomHistoryVisibility, "")?
|
|
||||||
.map_or(Ok(false), |s| {
|
|
||||||
serde_json::from_str(s.content.get())
|
|
||||||
.map(|c: RoomHistoryVisibilityEventContent| {
|
|
||||||
c.history_visibility == HistoryVisibility::WorldReadable
|
|
||||||
})
|
|
||||||
.map_err(|e| {
|
|
||||||
error!(
|
|
||||||
"Invalid room history visibility event in database for room {room_id}, assuming is \"shared\": {e}",
|
|
||||||
);
|
|
||||||
Error::bad_database("Invalid room history visibility event in database.")
|
|
||||||
})}).unwrap_or(false),
|
|
||||||
guest_can_join: services()
|
guest_can_join: services()
|
||||||
.rooms
|
.rooms
|
||||||
.state_accessor
|
.state_accessor
|
||||||
|
|
|
@ -18,7 +18,6 @@ use ruma::{
|
||||||
canonical_alias::RoomCanonicalAliasEventContent,
|
canonical_alias::RoomCanonicalAliasEventContent,
|
||||||
create::RoomCreateEventContent,
|
create::RoomCreateEventContent,
|
||||||
guest_access::{GuestAccess, RoomGuestAccessEventContent},
|
guest_access::{GuestAccess, RoomGuestAccessEventContent},
|
||||||
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
|
|
||||||
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
|
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent, RoomMembership},
|
||||||
topic::RoomTopicEventContent,
|
topic::RoomTopicEventContent,
|
||||||
},
|
},
|
||||||
|
@ -591,7 +590,7 @@ impl Service {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.unwrap_or(None),
|
.unwrap_or(None),
|
||||||
world_readable: world_readable(room_id)?,
|
world_readable: services().rooms.state_accessor.is_world_readable(room_id)?,
|
||||||
guest_can_join: guest_can_join(room_id)?,
|
guest_can_join: guest_can_join(room_id)?,
|
||||||
avatar_url: services()
|
avatar_url: services()
|
||||||
.rooms
|
.rooms
|
||||||
|
@ -858,26 +857,6 @@ fn guest_can_join(room_id: &RoomId) -> Result<bool, Error> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if guests are able to view room content without joining
|
|
||||||
fn world_readable(room_id: &RoomId) -> Result<bool, Error> {
|
|
||||||
Ok(services()
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")?
|
|
||||||
.map_or(Ok(false), |s| {
|
|
||||||
serde_json::from_str(s.content.get())
|
|
||||||
.map(|c: RoomHistoryVisibilityEventContent| c.history_visibility == HistoryVisibility::WorldReadable)
|
|
||||||
.map_err(|e| {
|
|
||||||
error!(
|
|
||||||
"Invalid room history visibility event in database for room {room_id}, assuming is \
|
|
||||||
\"shared\": {e} "
|
|
||||||
);
|
|
||||||
Error::bad_database("Invalid room history visibility event in database.")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.unwrap_or(false))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the join rule for a given room
|
/// Returns the join rule for a given room
|
||||||
fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
|
fn get_join_rule(current_room: &RoomId) -> Result<(SpaceRoomJoinRule, Vec<OwnedRoomId>), Error> {
|
||||||
Ok(services()
|
Ok(services()
|
||||||
|
|
|
@ -259,20 +259,14 @@ impl Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name(&self, room_id: &RoomId) -> Result<Option<String>> {
|
pub fn get_name(&self, room_id: &RoomId) -> Result<Option<String>> {
|
||||||
services()
|
self.room_state_get(room_id, &StateEventType::RoomName, "")?
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(room_id, &StateEventType::RoomName, "")?
|
|
||||||
.map_or(Ok(None), |s| {
|
.map_or(Ok(None), |s| {
|
||||||
Ok(serde_json::from_str(s.content.get()).map_or_else(|_| None, |c: RoomNameEventContent| Some(c.name)))
|
Ok(serde_json::from_str(s.content.get()).map_or_else(|_| None, |c: RoomNameEventContent| Some(c.name)))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_avatar(&self, room_id: &RoomId) -> Result<ruma::JsOption<RoomAvatarEventContent>> {
|
pub fn get_avatar(&self, room_id: &RoomId) -> Result<ruma::JsOption<RoomAvatarEventContent>> {
|
||||||
services()
|
self.room_state_get(room_id, &StateEventType::RoomAvatar, "")?
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(room_id, &StateEventType::RoomAvatar, "")?
|
|
||||||
.map_or(Ok(ruma::JsOption::Undefined), |s| {
|
.map_or(Ok(ruma::JsOption::Undefined), |s| {
|
||||||
serde_json::from_str(s.content.get())
|
serde_json::from_str(s.content.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))
|
.map_err(|_| Error::bad_database("Invalid room avatar event in database."))
|
||||||
|
@ -280,10 +274,7 @@ impl Service {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_member(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<RoomMemberEventContent>> {
|
pub fn get_member(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<RoomMemberEventContent>> {
|
||||||
services()
|
self.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())?
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())?
|
|
||||||
.map_or(Ok(None), |s| {
|
.map_or(Ok(None), |s| {
|
||||||
serde_json::from_str(s.content.get())
|
serde_json::from_str(s.content.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid room member event in database."))
|
.map_err(|_| Error::bad_database("Invalid room member event in database."))
|
||||||
|
@ -310,4 +301,24 @@ impl Service {
|
||||||
.create_hash_and_sign_event(new_event, sender, room_id, state_lock)
|
.create_hash_and_sign_event(new_event, sender, room_id, state_lock)
|
||||||
.is_ok())
|
.is_ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if guests are able to view room content without joining
|
||||||
|
pub fn is_world_readable(&self, room_id: &RoomId) -> Result<bool, Error> {
|
||||||
|
Ok(self
|
||||||
|
.room_state_get(room_id, &StateEventType::RoomHistoryVisibility, "")?
|
||||||
|
.map_or(Ok(false), |s| {
|
||||||
|
serde_json::from_str(s.content.get())
|
||||||
|
.map(|c: RoomHistoryVisibilityEventContent| {
|
||||||
|
c.history_visibility == HistoryVisibility::WorldReadable
|
||||||
|
})
|
||||||
|
.map_err(|e| {
|
||||||
|
error!(
|
||||||
|
"Invalid room history visibility event in database for room {room_id}, assuming not world \
|
||||||
|
readable: {e} "
|
||||||
|
);
|
||||||
|
Error::bad_database("Invalid room history visibility event in database.")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_or(false))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue