Skip to content
Snippets Groups Projects
Commit 0857fe79 authored by 🥺's avatar 🥺 :transgender_flag:
Browse files

abstract+add more "users in room" accessors, check membership state on...

abstract+add more "users in room" accessors, check membership state on `active_local_joined_users_in_room`

`roomuserid_joined` cf seems unreliable, so in the mean time we need to check
membership state (or maybe this is a more reliable check anyways)

Signed-off-by: default avatarstrawberry <strawberry@puppygock.gay>
parent c738c119
No related branches found
No related tags found
1 merge request!426Delete unnecessary real_users_cache, fix receiving notifs for rooms we've left, fix suspicious push_target logic
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
use itertools::Itertools; use itertools::Itertools;
use ruma::{ use ruma::{
events::{AnyStrippedStateEvent, AnySyncStateEvent}, events::{room::member::MembershipState, AnyStrippedStateEvent, AnySyncStateEvent},
serde::Raw, serde::Raw,
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId, OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
}; };
...@@ -43,12 +43,23 @@ fn mark_as_invited( ...@@ -43,12 +43,23 @@ fn mark_as_invited(
/// know). /// know).
fn server_rooms<'a>(&'a self, server: &ServerName) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a>; fn server_rooms<'a>(&'a self, server: &ServerName) -> Box<dyn Iterator<Item = Result<OwnedRoomId>> + 'a>;
/// Returns an iterator over all joined members of a room. /// Returns an iterator of all joined members of a room.
fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a>; fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a>;
/// Returns a vec of all the users joined in a room who are active /// Returns an iterator of all our local users
/// (not guests, not deactivated users) /// in the room, even if they're deactivated/guests
fn active_local_users_in_room(&self, room_id: &RoomId) -> Vec<OwnedUserId>; fn local_users_in_room<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = OwnedUserId> + 'a>;
/// Returns an iterator of all our local users in a room who are active (not
/// deactivated, not guest)
fn active_local_users_in_room<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = OwnedUserId> + 'a>;
/// Returns an iterator of all our local users joined in a room who are
/// active (not deactivated, not guest) and have a joined membership state
/// in the room
fn active_local_joined_users_in_room<'a>(
&'a self, room_id: &'a RoomId,
) -> Box<dyn Iterator<Item = OwnedUserId> + 'a>;
fn room_joined_count(&self, room_id: &RoomId) -> Result<Option<u64>>; fn room_joined_count(&self, room_id: &RoomId) -> Result<Option<u64>>;
...@@ -381,7 +392,7 @@ fn server_rooms<'a>(&'a self, server: &ServerName) -> Box<dyn Iterator<Item = Re ...@@ -381,7 +392,7 @@ fn server_rooms<'a>(&'a self, server: &ServerName) -> Box<dyn Iterator<Item = Re
})) }))
} }
/// Returns an iterator over all joined members of a room. /// Returns an iterator of all joined members of a room.
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a> { fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Result<OwnedUserId>> + 'a> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
...@@ -400,14 +411,43 @@ fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Resul ...@@ -400,14 +411,43 @@ fn room_members<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = Resul
})) }))
} }
/// Returns a vec of all our local users joined in a room who are active /// Returns an iterator of all our local users in the room, even if they're
/// (not guests / not deactivated users) /// deactivated/guests
fn local_users_in_room<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = OwnedUserId> + 'a> {
Box::new(
self.room_members(room_id)
.filter_map(Result::ok)
.filter(|user| user_is_local(user)),
)
}
/// Returns an iterator of all our local users in a room who are active (not
/// deactivated, not guest)
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn active_local_users_in_room(&self, room_id: &RoomId) -> Vec<OwnedUserId> { fn active_local_users_in_room<'a>(&'a self, room_id: &RoomId) -> Box<dyn Iterator<Item = OwnedUserId> + 'a> {
self.room_members(room_id) Box::new(
.filter_map(Result::ok) self.local_users_in_room(room_id)
.filter(|user| user_is_local(user) && !services().users.is_deactivated(user).unwrap_or(true)) .filter(|user| !services().users.is_deactivated(user).unwrap_or(true)),
.collect_vec() )
}
/// Returns an iterator of all our local users joined in a room who are
/// active (not deactivated, not guest) and have a joined membership state
/// in the room
///
/// TODO: why is `roomuserid_joined` not reliable?
#[tracing::instrument(skip(self))]
fn active_local_joined_users_in_room<'a>(
&'a self, room_id: &'a RoomId,
) -> Box<dyn Iterator<Item = OwnedUserId> + 'a> {
Box::new(self.active_local_users_in_room(room_id).filter(|user_id| {
services()
.rooms
.state_accessor
.get_member(room_id, user_id)
.unwrap_or(None)
.map_or(false, |membership| membership.membership == MembershipState::Join)
}))
} }
/// Returns the number of users which are currently in a room /// Returns the number of users which are currently in a room
......
...@@ -274,12 +274,29 @@ pub fn room_members<'a>(&'a self, room_id: &RoomId) -> impl Iterator<Item = Resu ...@@ -274,12 +274,29 @@ pub fn room_members<'a>(&'a self, room_id: &RoomId) -> impl Iterator<Item = Resu
pub fn room_joined_count(&self, room_id: &RoomId) -> Result<Option<u64>> { self.db.room_joined_count(room_id) } pub fn room_joined_count(&self, room_id: &RoomId) -> Result<Option<u64>> { self.db.room_joined_count(room_id) }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
/// Returns a vec of all the users joined in a room who are active /// Returns an iterator of all our local users in the room, even if they're
/// (not guests, not deactivated users) /// deactivated/guests
pub fn active_local_users_in_room(&self, room_id: &RoomId) -> Vec<OwnedUserId> { pub fn local_users_in_room<'a>(&'a self, room_id: &RoomId) -> impl Iterator<Item = OwnedUserId> + 'a {
self.db.local_users_in_room(room_id)
}
#[tracing::instrument(skip(self))]
/// Returns an iterator of all our local users in a room who are active (not
/// deactivated, not guest)
pub fn active_local_users_in_room<'a>(&'a self, room_id: &RoomId) -> impl Iterator<Item = OwnedUserId> + 'a {
self.db.active_local_users_in_room(room_id) self.db.active_local_users_in_room(room_id)
} }
#[tracing::instrument(skip(self))]
/// Returns an iterator of all our local users joined in a room who are
/// active (not deactivated, not guest) and have a joined membership state
/// in the room
pub fn active_local_joined_users_in_room<'a>(
&'a self, room_id: &'a RoomId,
) -> impl Iterator<Item = OwnedUserId> + 'a {
self.db.active_local_joined_users_in_room(room_id)
}
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn room_invited_count(&self, room_id: &RoomId) -> Result<Option<u64>> { self.db.room_invited_count(room_id) } pub fn room_invited_count(&self, room_id: &RoomId) -> Result<Option<u64>> { self.db.room_invited_count(room_id) }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
}; };
use data::Data; use data::Data;
use itertools::Itertools;
use rand::prelude::SliceRandom; use rand::prelude::SliceRandom;
use ruma::{ use ruma::{
api::{client::error::ErrorKind, federation}, api::{client::error::ErrorKind, federation},
...@@ -309,7 +310,8 @@ pub async fn append_pdu( ...@@ -309,7 +310,8 @@ pub async fn append_pdu(
let mut push_target = services() let mut push_target = services()
.rooms .rooms
.state_cache .state_cache
.active_local_users_in_room(&pdu.room_id); .active_local_joined_users_in_room(&pdu.room_id)
.collect_vec();
if pdu.kind == TimelineEventType::RoomMember { if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment