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

This commit is contained in:
Andriy Kushnir (Orhideous) 2022-11-28 01:42:24 +02:00
parent 5ae551b101
commit f13673e2b0
No known key found for this signature in database
GPG key ID: 62E078AB621B0D15
3 changed files with 33 additions and 46 deletions

View file

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

View file

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

View file

@ -7,7 +7,9 @@ use std::{
pub use data::Data;
use lru_cache::LruCache;
use ruma::{
events::{room::history_visibility::HistoryVisibility, StateEventType},
events::{
room::history_visibility::HistoryVisibility, room::member::MembershipState, StateEventType,
},
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)
pub fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> {
self.db.user_was_joined(shortstatehash, user_id)
pub fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> bool {
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
/// in the past)
pub fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> Result<bool> {
self.db.user_was_invited(shortstatehash, user_id)
pub fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> bool {
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.