refactor: Replace specialized interface for user's membership with more generic one in state accessor

Nyaaori/temp-combined-feature-branch
Andriy Kushnir (Orhideous) 2 years ago
parent 5ae551b101
commit f13673e2b0
No known key found for this signature in database
GPG Key ID: 62E078AB621B0D15

@ -9,6 +9,7 @@ use ruma::{
events::{room::member::MembershipState, StateEventType}, events::{room::member::MembershipState, StateEventType},
EventId, RoomId, UserId, EventId, RoomId, UserId,
}; };
use serde_json::Value;
#[async_trait] #[async_trait]
impl service::rooms::state_accessor::Data for KeyValueDatabase { impl service::rooms::state_accessor::Data for KeyValueDatabase {
@ -131,7 +132,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
shortstatehash: u64, shortstatehash: u64,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<serde_json::Value>> { ) -> Result<Option<Value>> {
let content = self let content = self
.state_get(shortstatehash, event_type, state_key)? .state_get(shortstatehash, event_type, state_key)?
.map(|event| serde_json::from_str(event.content.get())) .map(|event| serde_json::from_str(event.content.get()))
@ -159,40 +160,21 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
}) })
} }
/// The user was a joined member at this state (potentially in the past) /// Get membership for given user in state
fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> { fn user_membership(&self, shortstatehash: u64, user_id: &UserId) -> Result<MembershipState> {
Ok(self self.state_get_content(
.state_get_content( shortstatehash,
shortstatehash, &StateEventType::RoomMember,
&StateEventType::RoomMember, user_id.as_str(),
user_id.as_str(), )?
)? .map(|content| match content.get("membership") {
.map(|content| match content.get("membership") { Some(Value::String(membership)) => Ok(MembershipState::from(membership.as_str())),
Some(membership) => MembershipState::from(membership.as_str().unwrap_or("")), None => Ok(MembershipState::Leave),
None => MembershipState::Leave, _ => Err(Error::bad_database(
} == MembershipState::Join) "Malformed membership, expected Value::String",
.unwrap_or(false)) )),
} })
.unwrap_or(Ok(MembershipState::Leave))
/// The user was an invited or joined room member at this state (potentially
/// in the past)
fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> {
Ok(self
.state_get_content(
shortstatehash,
&StateEventType::RoomMember,
user_id.as_str(),
)?
.map(|content| {
let membership = match content.get("membership") {
Some(membership) => MembershipState::from(membership.as_str().unwrap_or("")),
None => MembershipState::Leave,
};
let joined = membership == MembershipState::Join;
let invited = membership == MembershipState::Invite;
invited || joined
})
.unwrap_or(false))
} }
/// Returns the full room state. /// Returns the full room state.

@ -4,6 +4,7 @@ use std::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use ruma::events::room::member::MembershipState;
use ruma::{events::StateEventType, EventId, RoomId, UserId}; use ruma::{events::StateEventType, EventId, RoomId, UserId};
use crate::{PduEvent, Result}; use crate::{PduEvent, Result};
@ -45,12 +46,8 @@ pub trait Data: Send + Sync {
/// Returns the state hash for this pdu. /// Returns the state hash for this pdu.
fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>>; fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>>;
/// The user was a joined member at this state (potentially in the past) /// Get membership for given user in state
fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool>; fn user_membership(&self, shortstatehash: u64, user_id: &UserId) -> Result<MembershipState>;
/// The user was an invited or joined room member at this state (potentially
/// in the past)
fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool>;
/// Returns the full room state. /// Returns the full room state.
async fn room_state_full( async fn room_state_full(

@ -7,7 +7,9 @@ use std::{
pub use data::Data; pub use data::Data;
use lru_cache::LruCache; use lru_cache::LruCache;
use ruma::{ use ruma::{
events::{room::history_visibility::HistoryVisibility, StateEventType}, events::{
room::history_visibility::HistoryVisibility, room::member::MembershipState, StateEventType,
},
EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId, EventId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
}; };
@ -151,14 +153,20 @@ impl Service {
} }
/// The user was a joined member at this state (potentially in the past) /// The user was a joined member at this state (potentially in the past)
pub fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> { pub fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> bool {
self.db.user_was_joined(shortstatehash, user_id) self.db
.user_membership(shortstatehash, user_id)
.map(|s| s == MembershipState::Join)
.unwrap_or_default() // Return sensible default, i.e. false
} }
/// The user was an invited or joined room member at this state (potentially /// The user was an invited or joined room member at this state (potentially
/// in the past) /// in the past)
pub fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> { pub fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> bool {
self.db.user_was_invited(shortstatehash, user_id) self.db
.user_membership(shortstatehash, user_id)
.map(|s| s == MembershipState::Join || s == MembershipState::Invite)
.unwrap_or_default() // Return sensible default, i.e. false
} }
/// Returns the full room state. /// Returns the full room state.

Loading…
Cancel
Save