use single global function for server name local and user local checking

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-28 11:35:25 -04:00 committed by June
parent 8f17d965b2
commit 9931e60050
14 changed files with 77 additions and 41 deletions

View file

@ -9,7 +9,9 @@ use super::RoomModerationCommand;
use crate::{
api::client_server::{get_alias_helper, leave_room},
service::admin::{escape_html, Service},
services, Result,
services,
utils::user_id::user_is_local,
Result,
};
pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) -> Result<RoomMessageEventContent> {
@ -102,11 +104,10 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) ->
.room_members(&room_id)
.filter_map(|user| {
user.ok().filter(|local_user| {
local_user.server_name() == services().globals.server_name()
user_is_local(local_user)
// additional wrapped check here is to avoid adding remote users
// who are in the admin room to the list of local users (would fail auth check)
&& (local_user.server_name()
== services().globals.server_name()
&& (user_is_local(local_user)
&& services()
.users
.is_admin(local_user)

View file

@ -13,7 +13,11 @@ use serde::{Deserialize, Serialize};
use tokio::{sync::Mutex, time::sleep};
use tracing::{debug, error};
use crate::{services, utils, Config, Error, Result};
use crate::{
services,
utils::{self, user_id::user_is_local},
Config, Error, Result,
};
/// Represents data required to be kept in order to implement the presence
/// specification.
@ -154,7 +158,7 @@ impl Service {
self.db
.set_presence(user_id, presence_state, currently_active, last_active_ago, status_msg)?;
if self.timeout_remote_users || user_id.server_name() == services().globals.server_name() {
if self.timeout_remote_users || user_is_local(user_id) {
let timeout = match presence_state {
PresenceState::Online => services().globals.config.presence_idle_timeout_s,
_ => services().globals.config.presence_offline_timeout_s,

View file

@ -8,7 +8,11 @@ use ruma::{
use tokio::sync::{broadcast, RwLock};
use tracing::debug;
use crate::{services, utils, Result};
use crate::{
services,
utils::{self, user_id::user_is_local},
Result,
};
pub(crate) struct Service {
pub(crate) typing: RwLock<BTreeMap<OwnedRoomId, BTreeMap<OwnedUserId, u64>>>, // u64 is unix timestamp of timeout
@ -37,7 +41,7 @@ impl Service {
_ = self.typing_update_sender.send(room_id.to_owned());
// update federation
if user_id.server_name() == services().globals.server_name() {
if user_is_local(user_id) {
self.federation_send(room_id, user_id, true)?;
}
@ -61,7 +65,7 @@ impl Service {
_ = self.typing_update_sender.send(room_id.to_owned());
// update federation
if user_id.server_name() == services().globals.server_name() {
if user_is_local(user_id) {
self.federation_send(room_id, user_id, false)?;
}
@ -115,7 +119,7 @@ impl Service {
// update federation
for user in removable {
if user.server_name() == services().globals.server_name() {
if user_is_local(&user) {
self.federation_send(room_id, &user, false)?;
}
}
@ -154,10 +158,7 @@ impl Service {
}
fn federation_send(&self, room_id: &RoomId, user_id: &UserId, typing: bool) -> Result<()> {
debug_assert!(
user_id.server_name() == services().globals.server_name(),
"tried to broadcast typing status of remote user",
);
debug_assert!(user_is_local(user_id), "tried to broadcast typing status of remote user",);
if !services().globals.config.allow_outgoing_typing {
return Ok(());
}

View file

@ -23,7 +23,12 @@ use ruma::{
use tracing::{debug, error, warn};
use super::{appservice, send, Destination, Msg, SendingEvent, Service};
use crate::{service::presence::Presence, services, utils::calculate_hash, Error, PduEvent, Result};
use crate::{
service::presence::Presence,
services,
utils::{calculate_hash, user_id::user_is_local},
Error, PduEvent, Result,
};
#[derive(Debug)]
enum TransactionStatus {
@ -244,7 +249,7 @@ impl Service {
.users
.keys_changed(room_id.as_ref(), since, None)
.filter_map(Result::ok)
.filter(|user_id| user_id.server_name() == services().globals.server_name()),
.filter(|user_id| user_is_local(user_id)),
);
if services().globals.allow_outgoing_read_receipts()