move and improve common-rooms related
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
aea82183b2
commit
3968d03868
6 changed files with 21 additions and 38 deletions
|
@ -52,8 +52,8 @@ pub(crate) async fn get_presence_route(
|
||||||
|
|
||||||
let has_shared_rooms = services
|
let has_shared_rooms = services
|
||||||
.rooms
|
.rooms
|
||||||
.user
|
.state_cache
|
||||||
.has_shared_rooms(sender_user, &body.user_id)
|
.user_sees_user(sender_user, &body.user_id)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if has_shared_rooms {
|
if has_shared_rooms {
|
||||||
|
|
|
@ -52,7 +52,7 @@ async fn share_encrypted_room(
|
||||||
) -> bool {
|
) -> bool {
|
||||||
services
|
services
|
||||||
.rooms
|
.rooms
|
||||||
.user
|
.state_cache
|
||||||
.get_shared_rooms(sender_user, user_id)
|
.get_shared_rooms(sender_user, user_id)
|
||||||
.ready_filter(|&room_id| Some(room_id) != ignore_room)
|
.ready_filter(|&room_id| Some(room_id) != ignore_room)
|
||||||
.any(|other_room_id| {
|
.any(|other_room_id| {
|
||||||
|
|
|
@ -55,7 +55,7 @@ pub(crate) async fn get_mutual_rooms_route(
|
||||||
|
|
||||||
let mutual_rooms: Vec<OwnedRoomId> = services
|
let mutual_rooms: Vec<OwnedRoomId> = services
|
||||||
.rooms
|
.rooms
|
||||||
.user
|
.state_cache
|
||||||
.get_shared_rooms(sender_user, &body.user_id)
|
.get_shared_rooms(sender_user, &body.user_id)
|
||||||
.map(ToOwned::to_owned)
|
.map(ToOwned::to_owned)
|
||||||
.collect()
|
.collect()
|
||||||
|
|
|
@ -71,8 +71,8 @@ pub(crate) async fn search_users_route(
|
||||||
} else {
|
} else {
|
||||||
let user_is_in_shared_rooms = services
|
let user_is_in_shared_rooms = services
|
||||||
.rooms
|
.rooms
|
||||||
.user
|
.state_cache
|
||||||
.has_shared_rooms(sender_user, &user.user_id)
|
.user_sees_user(sender_user, &user.user_id)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if user_is_in_shared_rooms {
|
if user_is_in_shared_rooms {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use conduit::{
|
||||||
warn, Result,
|
warn, Result,
|
||||||
};
|
};
|
||||||
use database::{serialize_to_vec, Deserialized, Ignore, Interfix, Json, Map};
|
use database::{serialize_to_vec, Deserialized, Ignore, Interfix, Json, Map};
|
||||||
use futures::{future::join4, stream::iter, Stream, StreamExt};
|
use futures::{future::join4, pin_mut, stream::iter, Stream, StreamExt};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
events::{
|
events::{
|
||||||
|
@ -385,16 +385,21 @@ impl Service {
|
||||||
/// Returns true if user_a and user_b share at least one room.
|
/// Returns true if user_a and user_b share at least one room.
|
||||||
#[tracing::instrument(skip(self), level = "debug")]
|
#[tracing::instrument(skip(self), level = "debug")]
|
||||||
pub async fn user_sees_user(&self, user_a: &UserId, user_b: &UserId) -> bool {
|
pub async fn user_sees_user(&self, user_a: &UserId, user_b: &UserId) -> bool {
|
||||||
// Minimize number of point-queries by iterating user with least nr rooms
|
let get_shared_rooms = self.get_shared_rooms(user_a, user_b);
|
||||||
let (a, b) = if self.rooms_joined(user_a).count().await < self.rooms_joined(user_b).count().await {
|
|
||||||
(user_a, user_b)
|
|
||||||
} else {
|
|
||||||
(user_b, user_a)
|
|
||||||
};
|
|
||||||
|
|
||||||
self.rooms_joined(a)
|
pin_mut!(get_shared_rooms);
|
||||||
.any(|room_id| self.is_joined(b, room_id))
|
get_shared_rooms.next().await.is_some()
|
||||||
.await
|
}
|
||||||
|
|
||||||
|
/// List the rooms common between two users
|
||||||
|
pub fn get_shared_rooms<'a>(
|
||||||
|
&'a self, user_a: &'a UserId, user_b: &'a UserId,
|
||||||
|
) -> impl Stream<Item = &RoomId> + Send + 'a {
|
||||||
|
use conduit::utils::set;
|
||||||
|
|
||||||
|
let a = self.rooms_joined(user_a);
|
||||||
|
let b = self.rooms_joined(user_b);
|
||||||
|
set::intersection_sorted_stream2(a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator of all joined members of a room.
|
/// Returns an iterator of all joined members of a room.
|
||||||
|
|
|
@ -2,7 +2,6 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use conduit::{implement, Result};
|
use conduit::{implement, Result};
|
||||||
use database::{Deserialized, Map};
|
use database::{Deserialized, Map};
|
||||||
use futures::{pin_mut, Stream, StreamExt};
|
|
||||||
use ruma::{RoomId, UserId};
|
use ruma::{RoomId, UserId};
|
||||||
|
|
||||||
use crate::{globals, rooms, rooms::short::ShortStateHash, Dep};
|
use crate::{globals, rooms, rooms::short::ShortStateHash, Dep};
|
||||||
|
@ -22,7 +21,6 @@ struct Data {
|
||||||
struct Services {
|
struct Services {
|
||||||
globals: Dep<globals::Service>,
|
globals: Dep<globals::Service>,
|
||||||
short: Dep<rooms::short::Service>,
|
short: Dep<rooms::short::Service>,
|
||||||
state_cache: Dep<rooms::state_cache::Service>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl crate::Service for Service {
|
impl crate::Service for Service {
|
||||||
|
@ -38,7 +36,6 @@ impl crate::Service for Service {
|
||||||
services: Services {
|
services: Services {
|
||||||
globals: args.depend::<globals::Service>("globals"),
|
globals: args.depend::<globals::Service>("globals"),
|
||||||
short: args.depend::<rooms::short::Service>("rooms::short"),
|
short: args.depend::<rooms::short::Service>("rooms::short"),
|
||||||
state_cache: args.depend::<rooms::state_cache::Service>("rooms::state_cache"),
|
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -118,22 +115,3 @@ pub async fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Re
|
||||||
.await
|
.await
|
||||||
.deserialized()
|
.deserialized()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[implement(Service)]
|
|
||||||
pub async fn has_shared_rooms<'a>(&'a self, user_a: &'a UserId, user_b: &'a UserId) -> bool {
|
|
||||||
let get_shared_rooms = self.get_shared_rooms(user_a, user_b);
|
|
||||||
|
|
||||||
pin_mut!(get_shared_rooms);
|
|
||||||
get_shared_rooms.next().await.is_some()
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: optimize; replace point-queries with dual iteration
|
|
||||||
#[implement(Service)]
|
|
||||||
pub fn get_shared_rooms<'a>(
|
|
||||||
&'a self, user_a: &'a UserId, user_b: &'a UserId,
|
|
||||||
) -> impl Stream<Item = &RoomId> + Send + 'a {
|
|
||||||
self.services
|
|
||||||
.state_cache
|
|
||||||
.rooms_joined(user_a)
|
|
||||||
.filter(|room_id| self.services.state_cache.is_joined(user_b, room_id))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue