refactor: Ruma upstreaming, half-baked edition

Co-authored-by: Jade Ellis <jade@ellis.link>
This commit is contained in:
Ginger
2026-03-29 12:25:42 -04:00
parent 1cc9dbf2a4
commit 204bc1367e
141 changed files with 2715 additions and 2279 deletions
+39 -35
View File
@@ -12,9 +12,7 @@ use conduwuit::{
use database::{Deserialized, Ignore, Interfix, Map};
use futures::{Stream, StreamExt, future::join5, pin_mut};
use ruma::{
OwnedRoomId, OwnedUserId, RoomId, ServerName, UserId,
events::{AnyStrippedStateEvent, room::member::MembershipState},
serde::Raw,
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId, events::{AnyStrippedStateEvent, room::member::MembershipState}, serde::Raw
};
use crate::{Dep, account_data, appservice::RegistrationInfo, config, globals, rooms, users};
@@ -147,13 +145,13 @@ pub fn clear_appservice_in_room_cache(&self) { self.appservice_in_room_cache.wri
pub fn room_servers<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a ServerName> + Send + 'a {
) -> impl Stream<Item = OwnedServerName> + Send + 'a {
let prefix = (room_id, Interfix);
self.db
.roomserverids
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, server): (Ignore, &ServerName)| server)
.map(|(_, server): (Ignore, OwnedServerName)| server)
}
#[implement(Service)]
@@ -170,13 +168,13 @@ pub async fn server_in_room<'a>(&'a self, server: &'a ServerName, room_id: &'a R
pub fn server_rooms<'a>(
&'a self,
server: &'a ServerName,
) -> impl Stream<Item = &'a RoomId> + Send + 'a {
) -> impl Stream<Item = OwnedRoomId> + Send + 'a {
let prefix = (server, Interfix);
self.db
.serverroomids
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, room_id): (Ignore, &RoomId)| room_id)
.map(|(_, room_id): (Ignore, OwnedRoomId)| room_id)
}
/// Returns true if server can see user by sharing at least one room.
@@ -184,7 +182,7 @@ pub fn server_rooms<'a>(
#[tracing::instrument(skip(self), level = "trace")]
pub async fn server_sees_user(&self, server: &ServerName, user_id: &UserId) -> bool {
self.server_rooms(server)
.any(|room_id| self.is_joined(user_id, room_id))
.any(async |room_id| self.is_joined(user_id, &room_id).await)
.await
}
@@ -205,7 +203,7 @@ pub fn get_shared_rooms<'a>(
&'a self,
user_a: &'a UserId,
user_b: &'a UserId,
) -> impl Stream<Item = &'a RoomId> + Send + 'a {
) -> impl Stream<Item = OwnedRoomId> + Send + 'a {
use conduwuit::utils::set;
let a = self.rooms_joined(user_a);
@@ -219,13 +217,13 @@ pub fn get_shared_rooms<'a>(
pub fn room_members<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
let prefix = (room_id, Interfix);
self.db
.roomuserid_joined
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, user_id): (Ignore, &UserId)| user_id)
.map(|(_, user_id): (Ignore, OwnedUserId)| user_id)
}
/// Returns the number of users which are currently in a room
@@ -242,7 +240,7 @@ pub async fn room_joined_count(&self, room_id: &RoomId) -> Result<u64> {
pub fn local_users_in_room<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
self.room_members(room_id)
.ready_filter(|user| self.services.globals.user_is_local(user))
}
@@ -254,9 +252,15 @@ pub fn local_users_in_room<'a>(
pub fn active_local_users_in_room<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
self.local_users_in_room(room_id)
.filter(|user| self.services.users.is_active(user))
.filter_map(async |user_id| {
if self.services.users.is_active(&user_id).await {
Some(user_id)
} else {
None
}
})
}
/// Returns the number of users which are currently invited to a room
@@ -276,13 +280,13 @@ pub async fn room_invited_count(&self, room_id: &RoomId) -> Result<u64> {
pub fn room_useroncejoined<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
let prefix = (room_id, Interfix);
self.db
.roomuseroncejoinedids
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, user_id): (Ignore, &UserId)| user_id)
.map(|(_, user_id): (Ignore, OwnedUserId)| user_id)
}
/// Returns an iterator over all invited members of a room.
@@ -291,13 +295,13 @@ pub fn room_useroncejoined<'a>(
pub fn room_members_invited<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
let prefix = (room_id, Interfix);
self.db
.roomuserid_invitecount
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, user_id): (Ignore, &UserId)| user_id)
.map(|(_, user_id): (Ignore, OwnedUserId)| user_id)
}
/// Returns an iterator over all knocked members of a room.
@@ -306,13 +310,13 @@ pub fn room_members_invited<'a>(
pub fn room_members_knocked<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a UserId> + Send + 'a {
) -> impl Stream<Item = OwnedUserId> + Send + 'a {
let prefix = (room_id, Interfix);
self.db
.roomuserid_knockedcount
.keys_prefix(&prefix)
.ignore_err()
.map(|(_, user_id): (Ignore, &UserId)| user_id)
.map(|(_, user_id): (Ignore, OwnedUserId)| user_id)
}
#[implement(Service)]
@@ -350,12 +354,12 @@ pub async fn get_left_count(&self, room_id: &RoomId, user_id: &UserId) -> Result
pub fn rooms_joined<'a>(
&'a self,
user_id: &'a UserId,
) -> impl Stream<Item = &'a RoomId> + Send + 'a {
) -> impl Stream<Item = OwnedRoomId> + Send + 'a {
self.db
.userroomid_joined
.keys_raw_prefix(user_id)
.ignore_err()
.map(|(_, room_id): (Ignore, &RoomId)| room_id)
.map(|(_, room_id): (Ignore, OwnedRoomId)| room_id)
}
/// Returns an iterator over all rooms a user was invited to.
@@ -365,16 +369,16 @@ pub fn rooms_invited<'a>(
&'a self,
user_id: &'a UserId,
) -> impl Stream<Item = StrippedStateEventItem> + Send + 'a {
type KeyVal<'a> = (Key<'a>, Raw<Vec<AnyStrippedStateEvent>>);
type Key<'a> = (&'a UserId, &'a RoomId);
type KeyVal = (Key, Raw<Vec<AnyStrippedStateEvent>>);
type Key = (OwnedUserId, OwnedRoomId);
let prefix = (user_id, Interfix);
self.db
.userroomid_invitestate
.stream_prefix(&prefix)
.ignore_err()
.map(|((_, room_id), state): KeyVal<'_>| (room_id.to_owned(), state))
.map(|(room_id, state)| Ok((room_id, state.deserialize_as()?)))
.map(|((_, room_id), state): KeyVal| (room_id, state))
.map(|(room_id, state)| Ok((room_id, state.deserialize_as_unchecked()?)))
.ignore_err()
}
@@ -385,16 +389,16 @@ pub fn rooms_knocked<'a>(
&'a self,
user_id: &'a UserId,
) -> impl Stream<Item = StrippedStateEventItem> + Send + 'a {
type KeyVal<'a> = (Key<'a>, Raw<Vec<AnyStrippedStateEvent>>);
type Key<'a> = (&'a UserId, &'a RoomId);
type KeyVal = (Key, Raw<Vec<AnyStrippedStateEvent>>);
type Key = (OwnedUserId, OwnedRoomId);
let prefix = (user_id, Interfix);
self.db
.userroomid_knockedstate
.stream_prefix(&prefix)
.ignore_err()
.map(|((_, room_id), state): KeyVal<'_>| (room_id.to_owned(), state))
.map(|(room_id, state)| Ok((room_id, state.deserialize_as()?)))
.map(|((_, room_id), state): KeyVal| (room_id, state))
.map(|(room_id, state)| Ok((room_id, state.deserialize_as_unchecked()?)))
.ignore_err()
}
@@ -411,7 +415,7 @@ pub async fn invite_state(
.qry(&key)
.await
.deserialized()
.and_then(|val: Raw<Vec<AnyStrippedStateEvent>>| val.deserialize_as().map_err(Into::into))
.and_then(|val: Raw<Vec<AnyStrippedStateEvent>>| val.deserialize_as_unchecked().map_err(Into::into))
}
#[implement(Service)]
@@ -427,7 +431,7 @@ pub async fn knock_state(
.qry(&key)
.await
.deserialized()
.and_then(|val: Raw<Vec<AnyStrippedStateEvent>>| val.deserialize_as().map_err(Into::into))
.and_then(|val: Raw<Vec<AnyStrippedStateEvent>>| val.deserialize_as_unchecked().map_err(Into::into))
}
#[implement(Service)]
@@ -444,15 +448,15 @@ pub fn rooms_left<'a>(
&'a self,
user_id: &'a UserId,
) -> impl Stream<Item = (OwnedRoomId, Option<Pdu>)> + Send + 'a {
type KeyVal<'a> = (Key<'a>, Raw<Option<Pdu>>);
type Key<'a> = (&'a UserId, &'a RoomId);
type KeyVal = (Key, Raw<Option<Pdu>>);
type Key = (OwnedUserId, OwnedRoomId);
let prefix = (user_id, Interfix);
self.db
.userroomid_leftstate
.stream_prefix(&prefix)
.ignore_err()
.map(|((_, room_id), state): KeyVal<'_>| (room_id.to_owned(), state))
.map(|((_, room_id), state): KeyVal| (room_id, state))
.map(|(room_id, state)| Ok((room_id, state.deserialize()?)))
.ignore_err()
}
+3 -3
View File
@@ -9,7 +9,6 @@ use ruma::{
AnyStrippedStateEvent, GlobalAccountDataEventType, RoomAccountDataEventType,
StateEventType,
direct::DirectEvent,
invite_permission_config::FilterLevel,
room::{
create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
@@ -17,6 +16,7 @@ use ruma::{
},
serde::Raw,
};
use ruminuwuity::invite_permission_config::FilterLevel;
/// Update current membership data.
#[implement(super::Service)]
@@ -174,12 +174,12 @@ pub async fn update_joined_count(&self, room_id: &RoomId) {
self.room_servers(room_id)
.ready_for_each(|old_joined_server| {
if joined_servers.remove(old_joined_server) {
if joined_servers.remove(&old_joined_server) {
return;
}
// Server not in room anymore
let roomserver_id = (room_id, old_joined_server);
let roomserver_id = (room_id, old_joined_server.clone());
let serverroom_id = (old_joined_server, room_id);
self.db.roomserverids.del(roomserver_id);
+3 -4
View File
@@ -17,7 +17,6 @@ use ruma::{
pub async fn add_servers_invite_via(&self, room_id: &RoomId, servers: Vec<OwnedServerName>) {
let mut servers: Vec<_> = self
.servers_invite_via(room_id)
.map(ToOwned::to_owned)
.chain(iter(servers.into_iter()))
.collect()
.await;
@@ -81,12 +80,12 @@ pub async fn servers_route_via(&self, room_id: &RoomId) -> Result<Vec<OwnedServe
pub fn servers_invite_via<'a>(
&'a self,
room_id: &'a RoomId,
) -> impl Stream<Item = &'a ServerName> + Send + 'a {
type KeyVal<'a> = (Ignore, Vec<&'a ServerName>);
) -> impl Stream<Item = OwnedServerName> + Send + 'a {
type KeyVal = (Ignore, Vec<OwnedServerName>);
self.db
.roomid_inviteviaservers
.stream_raw_prefix(room_id)
.ignore_err()
.map(|(_, servers): KeyVal<'_>| *servers.last().expect("at least one server"))
.map(|(_, mut servers): KeyVal| servers.pop().expect("at least one server"))
}