de-global server_is_ours / user_is_local

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-22 07:43:51 +00:00
parent 010e4ee35a
commit 59efabbbc2
34 changed files with 179 additions and 169 deletions

View file

@ -90,7 +90,7 @@ pub(super) async fn remote_user_in_rooms(_body: Vec<&str>, user_id: Box<UserId>)
.state_cache
.rooms_joined(&user_id)
.filter_map(Result::ok)
.map(|room_id| get_room_info(&room_id))
.map(|room_id| get_room_info(services(), &room_id))
.collect();
if rooms.is_empty() {

View file

@ -19,7 +19,7 @@ extern crate conduit_core as conduit;
extern crate conduit_service as service;
pub(crate) use conduit::{mod_ctor, mod_dtor, Result};
pub(crate) use service::{services, user_is_local};
pub(crate) use service::services;
pub(crate) use crate::utils::{escape_html, get_room_info};

View file

@ -39,7 +39,7 @@ pub(super) async fn list(
true
})
.map(|room_id| get_room_info(&room_id))
.map(|room_id| get_room_info(services(), &room_id))
})
.collect::<Vec<_>>();
rooms.sort_by_key(|r| r.1);

View file

@ -29,7 +29,7 @@ pub(super) async fn process(command: RoomDirectoryCommand, _body: Vec<&str>) ->
.directory
.public_rooms()
.filter_map(Result::ok)
.map(|id: OwnedRoomId| get_room_info(&id))
.map(|id: OwnedRoomId| get_room_info(services(), &id))
.collect::<Vec<_>>();
rooms.sort_by_key(|r| r.1);
rooms.reverse();

View file

@ -1,9 +1,9 @@
use api::client::leave_room;
use conduit::{debug, error, info, warn, Result};
use ruma::{events::room::message::RoomMessageEventContent, OwnedRoomId, RoomAliasId, RoomId, RoomOrAliasId};
use tracing::{debug, error, info, warn};
use super::RoomModerationCommand;
use crate::{get_room_info, services, user_is_local, Result};
use crate::{get_room_info, services};
pub(super) async fn process(command: RoomModerationCommand, body: Vec<&str>) -> Result<RoomMessageEventContent> {
match command {
@ -110,11 +110,11 @@ async fn ban_room(
.room_members(&room_id)
.filter_map(|user| {
user.ok().filter(|local_user| {
user_is_local(local_user)
services().globals.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)
&& (user_is_local(local_user)
&& (services().globals.user_is_local(local_user)
// since this is a force operation, assume user is an admin
// if somehow this fails
&& services()
@ -484,7 +484,7 @@ async fn list_banned_rooms(_body: Vec<&str>) -> Result<RoomMessageEventContent>
let mut rooms = room_ids
.into_iter()
.map(|room_id| get_room_info(&room_id))
.map(|room_id| get_room_info(services(), &room_id))
.collect::<Vec<_>>();
rooms.sort_by_key(|r| r.1);
rooms.reverse();

View file

@ -36,7 +36,7 @@ pub(super) async fn create(
_body: Vec<&str>, username: String, password: Option<String>,
) -> Result<RoomMessageEventContent> {
// Validate user id
let user_id = parse_local_user_id(&username)?;
let user_id = parse_local_user_id(services(), &username)?;
if services().users.exists(&user_id)? {
return Ok(RoomMessageEventContent::text_plain(format!("Userid {user_id} already exists")));
@ -134,7 +134,7 @@ pub(super) async fn deactivate(
_body: Vec<&str>, no_leave_rooms: bool, user_id: String,
) -> Result<RoomMessageEventContent> {
// Validate user id
let user_id = parse_local_user_id(&user_id)?;
let user_id = parse_local_user_id(services(), &user_id)?;
// don't deactivate the server service account
if user_id == services().globals.server_user {
@ -170,7 +170,7 @@ pub(super) async fn deactivate(
}
pub(super) async fn reset_password(_body: Vec<&str>, username: String) -> Result<RoomMessageEventContent> {
let user_id = parse_local_user_id(&username)?;
let user_id = parse_local_user_id(services(), &username)?;
if user_id == services().globals.server_user {
return Ok(RoomMessageEventContent::text_plain(
@ -211,7 +211,7 @@ pub(super) async fn deactivate_all(
let mut admins = Vec::new();
for username in usernames {
match parse_active_local_user_id(username) {
match parse_active_local_user_id(services(), username) {
Ok(user_id) => {
if services().users.is_admin(&user_id)? && !force {
services()
@ -292,14 +292,14 @@ pub(super) async fn deactivate_all(
pub(super) async fn list_joined_rooms(_body: Vec<&str>, user_id: String) -> Result<RoomMessageEventContent> {
// Validate user id
let user_id = parse_local_user_id(&user_id)?;
let user_id = parse_local_user_id(services(), &user_id)?;
let mut rooms: Vec<(OwnedRoomId, u64, String)> = services()
.rooms
.state_cache
.rooms_joined(&user_id)
.filter_map(Result::ok)
.map(|room_id| get_room_info(&room_id))
.map(|room_id| get_room_info(services(), &room_id))
.collect();
if rooms.is_empty() {
@ -344,10 +344,13 @@ pub(super) async fn list_joined_rooms(_body: Vec<&str>, user_id: String) -> Resu
pub(super) async fn force_join_room(
_body: Vec<&str>, user_id: String, room_id: OwnedRoomOrAliasId,
) -> Result<RoomMessageEventContent> {
let user_id = parse_local_user_id(&user_id)?;
let user_id = parse_local_user_id(services(), &user_id)?;
let room_id = services().rooms.alias.resolve(&room_id).await?;
assert!(service::user_is_local(&user_id), "Parsed user_id must be a local user");
assert!(
services().globals.user_is_local(&user_id),
"Parsed user_id must be a local user"
);
join_room_by_id_helper(services(), &user_id, &room_id, None, &[], None).await?;
Ok(RoomMessageEventContent::notice_markdown(format!(
@ -356,13 +359,16 @@ pub(super) async fn force_join_room(
}
pub(super) async fn make_user_admin(_body: Vec<&str>, user_id: String) -> Result<RoomMessageEventContent> {
let user_id = parse_local_user_id(&user_id)?;
let user_id = parse_local_user_id(services(), &user_id)?;
let displayname = services()
.users
.displayname(&user_id)?
.unwrap_or_else(|| user_id.to_string());
assert!(service::user_is_local(&user_id), "Parsed user_id must be a local user");
assert!(
services().globals.user_is_local(&user_id),
"Parsed user_id must be a local user"
);
services()
.admin
.make_user_admin(&user_id, displayname)
@ -376,7 +382,7 @@ pub(super) async fn make_user_admin(_body: Vec<&str>, user_id: String) -> Result
pub(super) async fn put_room_tag(
_body: Vec<&str>, user_id: String, room_id: Box<RoomId>, tag: String,
) -> Result<RoomMessageEventContent> {
let user_id = parse_active_local_user_id(&user_id)?;
let user_id = parse_active_local_user_id(services(), &user_id)?;
let event = services()
.account_data
@ -411,7 +417,7 @@ pub(super) async fn put_room_tag(
pub(super) async fn delete_room_tag(
_body: Vec<&str>, user_id: String, room_id: Box<RoomId>, tag: String,
) -> Result<RoomMessageEventContent> {
let user_id = parse_active_local_user_id(&user_id)?;
let user_id = parse_active_local_user_id(services(), &user_id)?;
let event = services()
.account_data
@ -443,7 +449,7 @@ pub(super) async fn delete_room_tag(
pub(super) async fn get_room_tags(
_body: Vec<&str>, user_id: String, room_id: Box<RoomId>,
) -> Result<RoomMessageEventContent> {
let user_id = parse_active_local_user_id(&user_id)?;
let user_id = parse_active_local_user_id(services(), &user_id)?;
let event = services()
.account_data

View file

@ -1,8 +1,6 @@
use conduit_core::{err, Err};
use conduit_core::{err, Err, Result};
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
use service::user_is_local;
use crate::{services, Result};
use service::Services;
pub(crate) fn escape_html(s: &str) -> String {
s.replace('&', "&amp;")
@ -10,17 +8,17 @@ pub(crate) fn escape_html(s: &str) -> String {
.replace('>', "&gt;")
}
pub(crate) fn get_room_info(id: &RoomId) -> (OwnedRoomId, u64, String) {
pub(crate) fn get_room_info(services: &Services, id: &RoomId) -> (OwnedRoomId, u64, String) {
(
id.into(),
services()
services
.rooms
.state_cache
.room_joined_count(id)
.ok()
.flatten()
.unwrap_or(0),
services()
services
.rooms
.state_accessor
.get_name(id)
@ -31,16 +29,16 @@ pub(crate) fn get_room_info(id: &RoomId) -> (OwnedRoomId, u64, String) {
}
/// Parses user ID
pub(crate) fn parse_user_id(user_id: &str) -> Result<OwnedUserId> {
UserId::parse_with_server_name(user_id.to_lowercase(), services().globals.server_name())
pub(crate) fn parse_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
UserId::parse_with_server_name(user_id.to_lowercase(), services.globals.server_name())
.map_err(|e| err!("The supplied username is not a valid username: {e}"))
}
/// Parses user ID as our local user
pub(crate) fn parse_local_user_id(user_id: &str) -> Result<OwnedUserId> {
let user_id = parse_user_id(user_id)?;
pub(crate) fn parse_local_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
let user_id = parse_user_id(services, user_id)?;
if !user_is_local(&user_id) {
if !services.globals.user_is_local(&user_id) {
return Err!("User {user_id:?} does not belong to our server.");
}
@ -48,14 +46,14 @@ pub(crate) fn parse_local_user_id(user_id: &str) -> Result<OwnedUserId> {
}
/// Parses user ID that is an active (not guest or deactivated) local user
pub(crate) fn parse_active_local_user_id(user_id: &str) -> Result<OwnedUserId> {
let user_id = parse_local_user_id(user_id)?;
pub(crate) fn parse_active_local_user_id(services: &Services, user_id: &str) -> Result<OwnedUserId> {
let user_id = parse_local_user_id(services, user_id)?;
if !services().users.exists(&user_id)? {
if !services.users.exists(&user_id)? {
return Err!("User {user_id:?} does not exist on this server.");
}
if services().users.is_deactivated(&user_id)? {
if services.users.is_deactivated(&user_id)? {
return Err!("User {user_id:?} is deactivated.");
}