refactor: Fix (most) errors in api/client/account/

This commit is contained in:
Ginger
2026-04-07 12:57:36 -04:00
parent 24f7e1d658
commit ccd6072f2d
2 changed files with 38 additions and 60 deletions
+25 -50
View File
@@ -1,7 +1,7 @@
use axum::extract::State; use axum::extract::State;
use axum_client_ip::ClientIp; use axum_client_ip::ClientIp;
use conduwuit::{ use conduwuit::{
Err, Event, Result, err, info, Err, Result, err, info,
pdu::PduBuilder, pdu::PduBuilder,
utils::{ReadyExt, stream::BroadbandExt}, utils::{ReadyExt, stream::BroadbandExt},
}; };
@@ -9,7 +9,7 @@ use conduwuit_service::Services;
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use lettre::{Address, message::Mailbox}; use lettre::{Address, message::Mailbox};
use ruma::{ use ruma::{
OwnedRoomId, OwnedUserId, UserId, OwnedRoomId, UserId,
api::client::{ api::client::{
account::{ account::{
ThirdPartyIdRemovalStatus, change_password, check_registration_token_validity, ThirdPartyIdRemovalStatus, change_password, check_registration_token_validity,
@@ -18,12 +18,10 @@ use ruma::{
}, },
uiaa::{AuthFlow, AuthType}, uiaa::{AuthFlow, AuthType},
}, },
events::{ assign,
StateEventType, events::room::{
room::{ member::{MembershipState, RoomMemberEventContent},
member::{MembershipState, RoomMemberEventContent}, power_levels::RoomPowerLevelsEventContent,
power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
},
}, },
}; };
use service::{mailer::messages, uiaa::Identity}; use service::{mailer::messages, uiaa::Identity};
@@ -143,7 +141,7 @@ pub(crate) async fn change_password_route(
.await? .await?
}; };
let sender_user = OwnedUserId::parse(format!( let sender_user = UserId::parse(format!(
"@{}:{}", "@{}:{}",
identity.localpart.expect("localpart should be known"), identity.localpart.expect("localpart should be known"),
services.globals.server_name() services.globals.server_name()
@@ -161,7 +159,7 @@ pub(crate) async fn change_password_route(
.users .users
.all_device_ids(&sender_user) .all_device_ids(&sender_user)
.ready_filter(|id| *id != body.sender_device()) .ready_filter(|id| *id != body.sender_device())
.for_each(|id| services.users.remove_device(&sender_user, id)) .for_each(async |id| services.users.remove_device(&sender_user, &id).await)
.await; .await;
// Remove all pushers except the ones associated with this session // Remove all pushers except the ones associated with this session
@@ -215,7 +213,7 @@ pub(crate) async fn request_password_change_token_via_email_route(
}; };
let user_id = let user_id =
OwnedUserId::parse(format!("@{localpart}:{}", services.globals.server_name())).unwrap(); UserId::parse(format!("@{localpart}:{}", services.globals.server_name())).unwrap();
let display_name = services.users.displayname(&user_id).await.ok(); let display_name = services.users.displayname(&user_id).await.ok();
let session = services let session = services
@@ -251,11 +249,10 @@ pub(crate) async fn whoami_route(
.map_err(|_| { .map_err(|_| {
err!(Request(Forbidden("Application service has not registered this user."))) err!(Request(Forbidden("Application service has not registered this user.")))
})? && body.appservice_info.is_none(); })? && body.appservice_info.is_none();
Ok(whoami::v3::Response {
user_id: body.sender_user().to_owned(), Ok(assign!(whoami::v3::Response::new(body.sender_user().to_owned(), is_guest), {
device_id: body.sender_device.clone(), device_id: body.sender_device.clone(),
is_guest, }))
})
} }
/// # `POST /_matrix/client/r0/account/deactivate` /// # `POST /_matrix/client/r0/account/deactivate`
@@ -310,9 +307,7 @@ pub(crate) async fn deactivate_route(
.await; .await;
} }
Ok(deactivate::v3::Response { Ok(deactivate::v3::Response::new(ThirdPartyIdRemovalStatus::Success))
id_server_unbind_result: ThirdPartyIdRemovalStatus::Success,
})
} }
/// # `GET /_matrix/client/v1/register/m.login.registration_token/validity` /// # `GET /_matrix/client/v1/register/m.login.registration_token/validity`
@@ -330,7 +325,7 @@ pub(crate) async fn check_registration_token_validity(
.await .await
.is_some(); .is_some();
Ok(check_registration_token_validity::v1::Response { valid }) Ok(check_registration_token_validity::v1::Response::new(valid))
} }
/// Runs through all the deactivation steps: /// Runs through all the deactivation steps:
@@ -378,29 +373,16 @@ pub async fn full_user_deactivate(
let room_power_levels = services let room_power_levels = services
.rooms .rooms
.state_accessor .state_accessor
.room_state_get_content::<RoomPowerLevelsEventContent>( .get_room_power_levels(&room_id)
room_id, .await;
&StateEventType::RoomPowerLevels,
"",
)
.await
.ok();
let user_can_demote_self = let user_can_demote_self =
room_power_levels room_power_levels.user_can_change_user_power_level(user_id, user_id);
.as_ref()
.is_some_and(|power_levels_content| {
RoomPowerLevels::from(power_levels_content.clone())
.user_can_change_user_power_level(user_id, user_id)
}) || services
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomCreate, "")
.await
.is_ok_and(|event| event.sender() == user_id);
if user_can_demote_self { if user_can_demote_self
let mut power_levels_content = room_power_levels.unwrap_or_default(); && let Ok(mut power_levels_content) =
RoomPowerLevelsEventContent::try_from(room_power_levels)
{
power_levels_content.users.remove(user_id); power_levels_content.users.remove(user_id);
let pl_evt = PduBuilder::state(String::new(), &power_levels_content); let pl_evt = PduBuilder::state(String::new(), &power_levels_content);
pdu_queue.push((pl_evt, room_id)); pdu_queue.push((pl_evt, room_id));
@@ -408,17 +390,10 @@ pub async fn full_user_deactivate(
// Leave the room // Leave the room
pdu_queue.push(( pdu_queue.push((
PduBuilder::state(user_id.to_string(), &RoomMemberEventContent { PduBuilder::state(
avatar_url: None, user_id.to_string(),
blurhash: None, &RoomMemberEventContent::new(MembershipState::Leave),
membership: MembershipState::Leave, ),
displayname: None,
join_authorized_via_users_server: None,
reason: None,
is_direct: None,
third_party_invite: None,
redact_events: None,
}),
room_id, room_id,
)); ));
+13 -10
View File
@@ -20,7 +20,11 @@ use ruma::{
}, },
uiaa::{AuthFlow, AuthType}, uiaa::{AuthFlow, AuthType},
}, },
events::{GlobalAccountDataEventType, room::message::RoomMessageEventContent}, assign,
events::{
GlobalAccountDataEventType, push_rules::PushRulesEvent,
room::message::RoomMessageEventContent,
},
push, push,
}; };
use serde_json::value::RawValue; use serde_json::value::RawValue;
@@ -209,11 +213,10 @@ pub(crate) async fn register_route(
None, None,
&user_id, &user_id,
GlobalAccountDataEventType::PushRules.to_string().into(), GlobalAccountDataEventType::PushRules.to_string().into(),
&serde_json::to_value(ruma::events::push_rules::PushRulesEvent { &serde_json::to_value(PushRulesEvent::new(
content: ruma::events::push_rules::PushRulesEventContent { push::Ruleset::server_default(&user_id).into(),
global: push::Ruleset::server_default(&user_id), ))
}, .expect("should be able to serialize push rules"),
})?,
) )
.await?; .await?;
@@ -241,7 +244,8 @@ pub(crate) async fn register_route(
// Don't create a device for inhibited logins // Don't create a device for inhibited logins
(None, None) (None, None)
}; };
debug_info!(%user_id, %device_id, "User account was created");
debug_info!(%user_id, ?device, "User account was created");
// If the user registered with an email, associate it with their account. // If the user registered with an email, associate it with their account.
if let Some(identity) = identity if let Some(identity) = identity
@@ -387,13 +391,12 @@ pub(crate) async fn register_route(
} }
} }
Ok(register::v3::Response { Ok(assign!(register::v3::Response::new(user_id), {
access_token: token, access_token: token,
user_id,
device_id: device, device_id: device,
refresh_token: None, refresh_token: None,
expires_in: None, expires_in: None,
}) }))
} }
/// Determine which flows and parameters should be presented when /// Determine which flows and parameters should be presented when