Add wrapper types for short IDs

This commit is contained in:
Lambda 2024-08-27 14:27:12 +00:00
parent f1642c92d1
commit b0f33207fe
28 changed files with 427 additions and 232 deletions

View file

@ -26,6 +26,7 @@ use ruma::{
use serde_json::value::to_raw_value;
use tracing::{error, warn};
use super::short::{ShortStateHash, ShortStateKey};
use crate::{
observability::{FoundIn, Lookup, METRICS},
service::{globals::marker, pdu::PduBuilder},
@ -37,8 +38,9 @@ use crate::{
pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
pub(crate) server_visibility_cache:
Mutex<LruCache<(OwnedServerName, u64), bool>>,
pub(crate) user_visibility_cache: Mutex<LruCache<(OwnedUserId, u64), bool>>,
Mutex<LruCache<(OwnedServerName, ShortStateHash), bool>>,
pub(crate) user_visibility_cache:
Mutex<LruCache<(OwnedUserId, ShortStateHash), bool>>,
}
impl Service {
@ -47,15 +49,15 @@ impl Service {
#[tracing::instrument(skip(self))]
pub(crate) async fn state_full_ids(
&self,
shortstatehash: u64,
) -> Result<HashMap<u64, Arc<EventId>>> {
shortstatehash: ShortStateHash,
) -> Result<HashMap<ShortStateKey, Arc<EventId>>> {
self.db.state_full_ids(shortstatehash).await
}
#[tracing::instrument(skip(self))]
pub(crate) async fn state_full(
&self,
shortstatehash: u64,
shortstatehash: ShortStateHash,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
self.db.state_full(shortstatehash).await
}
@ -65,7 +67,7 @@ impl Service {
#[tracing::instrument(skip(self))]
pub(crate) fn state_get_id(
&self,
shortstatehash: u64,
shortstatehash: ShortStateHash,
event_type: &StateEventType,
state_key: &str,
) -> Result<Option<Arc<EventId>>> {
@ -77,7 +79,7 @@ impl Service {
#[tracing::instrument(skip(self))]
pub(crate) fn state_get(
&self,
shortstatehash: u64,
shortstatehash: ShortStateHash,
event_type: &StateEventType,
state_key: &str,
) -> Result<Option<Arc<PduEvent>>> {
@ -88,7 +90,7 @@ impl Service {
#[tracing::instrument(skip(self))]
fn user_membership(
&self,
shortstatehash: u64,
shortstatehash: ShortStateHash,
user_id: &UserId,
) -> Result<MembershipState> {
self.state_get(
@ -109,7 +111,11 @@ impl Service {
/// The user was a joined member at this state (potentially in the past)
#[tracing::instrument(skip(self), ret(level = "trace"))]
fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> bool {
fn user_was_joined(
&self,
shortstatehash: ShortStateHash,
user_id: &UserId,
) -> bool {
self.user_membership(shortstatehash, user_id)
.is_ok_and(|s| s == MembershipState::Join)
}
@ -117,7 +123,11 @@ impl Service {
/// The user was an invited or joined room member at this state (potentially
/// in the past)
#[tracing::instrument(skip(self), ret(level = "trace"))]
fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> bool {
fn user_was_invited(
&self,
shortstatehash: ShortStateHash,
user_id: &UserId,
) -> bool {
self.user_membership(shortstatehash, user_id).is_ok_and(|s| {
s == MembershipState::Join || s == MembershipState::Invite
})
@ -315,7 +325,7 @@ impl Service {
pub(crate) fn pdu_shortstatehash(
&self,
event_id: &EventId,
) -> Result<Option<u64>> {
) -> Result<Option<ShortStateHash>> {
self.db.pdu_shortstatehash(event_id)
}