Skip to content

Commit

Permalink
make tests concurrent and deprecate our UserId
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 17, 2024
1 parent ab2db5f commit 1c9df89
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 407 deletions.
4 changes: 2 additions & 2 deletions crates/core/src/account/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use url::Url;

use matrix::admin::resources::user_id::UserId;
use matrix::ruma_common::OwnedUserId;

#[derive(Debug, Clone)]
pub struct Account {
pub user_id: UserId,
pub user_id: OwnedUserId,
pub username: String,
pub email: String,
pub display_name: String,
Expand Down
37 changes: 24 additions & 13 deletions crates/core/src/account/service.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::sync::Arc;

use matrix::{admin::resources::user::UserService, client::resources::session::Session};
use matrix::{
admin::resources::user::UserService, client::resources::session::Session, ruma_common::UserId,
};
use tracing::instrument;
use url::Url;
use uuid::Uuid;
use validator::{Validate, ValidationError};

use matrix::{
admin::resources::{
user::{CreateUserBody, ListUsersQuery, LoginAsUserBody, ThreePid},
user_id::UserId,
},
admin::resources::user::{CreateUserBody, ListUsersQuery, LoginAsUserBody, ThreePid},
Client as MatrixAdminClient,
};

Expand Down Expand Up @@ -117,7 +116,13 @@ impl AccountService {
/// Returs `true` if the given `email address` is NOT registered in the
/// Matrix Server
pub async fn is_email_available(&self, email: &str) -> Result<bool> {
let user_id = UserId::new(email, self.admin.server_name());
let user_id = format!("@{}:{}", email, self.admin.server_name());
let user_id = <&UserId>::try_from(user_id.as_str()).map_err(|err| {
// TODO
tracing::error!(?err, "Failed to parse username");
Error::Unknown
})?;

let exists = UserService::list(
&self.admin,
ListUsersQuery {
Expand Down Expand Up @@ -206,15 +211,21 @@ impl AccountService {
return Err(AccountErrorCode::EmailTaken(dto.email).into());
}

let user_id = UserId::new(dto.username.clone(), self.admin.server_name().to_string());
let user_id = format!("@{}:{}", dto.username, self.admin.server_name());
let user_id = <&UserId>::try_from(user_id.as_str()).map_err(|err| {
// TODO
tracing::error!(?err, "Failed to parse username");
Error::Unknown
})?;

let avatar_url = Url::parse(DEFAULT_AVATAR_URL).map_err(|err| {
tracing::error!(?err, "Failed to parse default avatar url");
Error::Unknown
})?;

UserService::create(
&self.admin,
user_id.clone(),
user_id,
CreateUserBody {
displayname: Some(dto.username),
password: dto.password.to_string(),
Expand All @@ -239,14 +250,14 @@ impl AccountService {
Error::Unknown
})?;

let matrix_account = UserService::query_user_account(&self.admin, user_id.clone())
let matrix_account = UserService::query_user_account(&self.admin, user_id)
.await
.map_err(|err| {
tracing::error!(?err, "Failed to query user account");
Error::Unknown
})?;
let account = Account {
user_id,
user_id: user_id.into(),
username: matrix_account.name,
email: matrix_account
.threepids
Expand All @@ -264,9 +275,9 @@ impl AccountService {
}

/// Creates an access token for the given user
pub async fn issue_user_token(&self, user_id: UserId) -> Result<String> {
pub async fn issue_user_token(&self, user_id: &UserId) -> Result<String> {
let credentials =
UserService::login_as_user(&self.admin, user_id.clone(), LoginAsUserBody::default())
UserService::login_as_user(&self.admin, user_id, LoginAsUserBody::default())
.await
.map_err(|err| {
tracing::error!(?err, ?user_id, "Failed to login as user");
Expand All @@ -283,7 +294,7 @@ impl AccountService {
tracing::error!(?err, "Failed to get session from matrix as client");
Error::Unknown
})?;
let matrix_account = UserService::query_user_account(&self.admin, session.user_id.clone())
let matrix_account = UserService::query_user_account(&self.admin, &session.user_id)
.await
.map_err(|err| {
tracing::error!(?err, "Failed to query user account");
Expand Down
11 changes: 5 additions & 6 deletions crates/matrix/src/admin/resources/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
//! for a server admin: see Admin API.
use anyhow::Result;
use ruma_common::UserId;
use serde::{Deserialize, Serialize};
use tracing::instrument;
use url::Url;

use crate::{error::MatrixError, http::Client};

use super::user_id::UserId;

#[derive(Default)]
pub struct UserService;

Expand Down Expand Up @@ -149,7 +148,7 @@ impl UserService {
///
/// Refer: https://matrix-org.github.io/synapse/v1.88/admin_api/user_admin_api.html#query-user-account
#[instrument(skip(client))]
pub async fn query_user_account(client: &Client, user_id: UserId) -> Result<User> {
pub async fn query_user_account(client: &Client, user_id: &UserId) -> Result<User> {
let resp = client
.get(format!(
"/_synapse/admin/v2/users/{user_id}",
Expand All @@ -174,7 +173,7 @@ impl UserService {
///
/// Refer: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#create-or-modify-account
#[instrument(skip(client, body))]
pub async fn create(client: &Client, user_id: UserId, body: CreateUserBody) -> Result<User> {
pub async fn create(client: &Client, user_id: &UserId, body: CreateUserBody) -> Result<User> {
let resp = client
.put_json(
format!("/_synapse/admin/v2/users/{user_id}", user_id = user_id),
Expand Down Expand Up @@ -212,7 +211,7 @@ impl UserService {
///
/// Refer: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#create-or-modify-account
#[instrument(skip(client))]
pub async fn update(client: &Client, user_id: UserId, body: UpdateUserBody) -> Result<User> {
pub async fn update(client: &Client, user_id: &UserId, body: UpdateUserBody) -> Result<User> {
let resp = client
.put_json(
format!("/_synapse/admin/v2/users/{user_id}", user_id = user_id),
Expand Down Expand Up @@ -246,7 +245,7 @@ impl UserService {
#[instrument(skip(client))]
pub async fn login_as_user(
client: &Client,
user_id: UserId,
user_id: &UserId,
body: LoginAsUserBody,
) -> Result<LoginAsUserResponse> {
let resp = client
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix/src/client/resources/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use ruma_common::{serde::Raw, EventId, OwnedEventId, RoomId, TransactionId, OwnedTransactionId};
use ruma_common::{serde::Raw, EventId, OwnedEventId, RoomId, OwnedTransactionId};

use ruma_events::{
relation::RelationType, AnyStateEvent, AnyStateEventContent,
Expand All @@ -9,7 +9,7 @@ use ruma_events::{
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::instrument;

use crate::{admin::resources::room::Direction, filter::RoomEventFilter, Client, error::MatrixError};
use crate::{admin::resources::room::Direction, Client, error::MatrixError};

pub struct EventsService;

Expand Down
5 changes: 3 additions & 2 deletions crates/matrix/src/client/resources/session.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use anyhow::Result;
use ruma_common::{OwnedUserId};
use serde::{Deserialize, Serialize};
use tracing::instrument;

use crate::{admin::resources::user_id::UserId, error::MatrixError};
use crate::error::MatrixError;

#[derive(Debug, Serialize, Deserialize)]
pub struct Session {
pub device_id: String,
pub is_guest: bool,
pub user_id: UserId,
pub user_id: OwnedUserId,
}

impl Session {
Expand Down
2 changes: 1 addition & 1 deletion crates/server/src/router/api/v1/account/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn handler(
let access_token = services
.commune
.account
.issue_user_token(account.user_id.clone())
.issue_user_token(&account.user_id)
.await
.unwrap();
let payload = AccountRegisterResponse {
Expand Down
Loading

0 comments on commit 1c9df89

Please sign in to comment.