Skip to content

Commit

Permalink
chore: big formatting effort and removing duplicate code from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 14, 2024
1 parent ec7183a commit 9878fd6
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 403 deletions.
18 changes: 9 additions & 9 deletions crates/core/src/account/service.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

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

use matrix::{
admin::resources::{
user::{ListUsersParams, LoginAsUserDto, ThreePid, User as MatrixUser, UserCreateDto},
user::{CreateUserBody, ListUsersQuery, LoginAsUserBody, ThreePid},
user_id::UserId,
},
Client as MatrixAdminClient,
Expand Down Expand Up @@ -118,9 +118,9 @@ impl AccountService {
/// Matrix Server
pub async fn is_email_available(&self, email: &str) -> Result<bool> {
let user_id = UserId::new(email, self.admin.server_name());
let exists = MatrixUser::list(
let exists = UserService::list(
&self.admin,
ListUsersParams {
ListUsersQuery {
user_id: Some(user_id.to_string()),
..Default::default()
},
Expand Down Expand Up @@ -212,10 +212,10 @@ impl AccountService {
Error::Unknown
})?;

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

let matrix_account = MatrixUser::query_user_account(&self.admin, user_id.clone())
let matrix_account = UserService::query_user_account(&self.admin, user_id.clone())
.await
.map_err(|err| {
tracing::error!(?err, "Failed to query user account");
Expand All @@ -266,7 +266,7 @@ impl AccountService {
/// Creates an access token for the given user
pub async fn issue_user_token(&self, user_id: UserId) -> Result<String> {
let credentials =
MatrixUser::login_as_user(&self.admin, user_id.clone(), LoginAsUserDto::default())
UserService::login_as_user(&self.admin, user_id.clone(), LoginAsUserBody::default())
.await
.map_err(|err| {
tracing::error!(?err, ?user_id, "Failed to login as user");
Expand All @@ -283,7 +283,7 @@ impl AccountService {
tracing::error!(?err, "Failed to get session from matrix as client");
Error::Unknown
})?;
let matrix_account = MatrixUser::query_user_account(&self.admin, session.user_id.clone())
let matrix_account = UserService::query_user_account(&self.admin, session.user_id.clone())
.await
.map_err(|err| {
tracing::error!(?err, "Failed to query user account");
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/room/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl RoomService {
.await
{
Ok(room) => Ok(Room {
room_id: room.room_id,
room_id: room.room_id.to_string(),
}),
Err(err) => {
tracing::error!("Failed to create public room: {}", err);
Expand Down
4 changes: 1 addition & 3 deletions crates/matrix/src/admin/resources/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ impl RoomService {
/// Refer: https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#list-room-api
#[instrument(skip(client))]
pub async fn get_all(client: &Client, query: ListRoomQuery) -> Result<ListRoomResponse> {
let resp = client
.get_query("/_synapse/admin/v1/rooms", &query)
.await?;
let resp = client.get_query("/_synapse/admin/v1/rooms", &query).await?;

if resp.status().is_success() {
return Ok(resp.json().await?);
Expand Down
12 changes: 5 additions & 7 deletions crates/matrix/src/admin/resources/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub struct User {
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserCreateBody {
pub struct CreateUserBody {
pub password: String,
pub logout_devices: bool,
pub displayname: Option<String>,
Expand Down Expand Up @@ -102,7 +102,7 @@ pub struct ListUsersResponse {
}

#[derive(Debug, Serialize, Deserialize)]
pub struct UserUpdateBody {
pub struct UpdateUserBody {
pub password: String,
pub logout_devices: bool,
pub displayname: Option<String>,
Expand Down Expand Up @@ -174,7 +174,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: UserCreateBody) -> 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 All @@ -197,9 +197,7 @@ impl UserService {
/// Refer: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#list-accounts
#[instrument(skip(client))]
pub async fn list(client: &Client, query: ListUsersQuery) -> Result<ListUsersResponse> {
let resp = client
.get_query("/_synapse/admin/v2/users", &query)
.await?;
let resp = client.get_query("/_synapse/admin/v2/users", &query).await?;

if resp.status().is_success() {
return Ok(resp.json().await?);
Expand All @@ -214,7 +212,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: UserUpdateBody) -> 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
6 changes: 3 additions & 3 deletions crates/matrix/src/client/resources/room.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::Result;
use ruma_common::{serde::Raw, OwnedUserId, RoomId, RoomOrAliasId};
use ruma_common::{serde::Raw, OwnedRoomId, OwnedUserId, RoomId, RoomOrAliasId};
use ruma_events::{room::power_levels::RoomPowerLevelsEventContent, AnyInitialStateEvent};
use serde::{Deserialize, Serialize};
use tracing::instrument;
Expand Down Expand Up @@ -86,12 +86,12 @@ pub struct RoomKickOrBanBody {

#[derive(Debug, Deserialize)]
pub struct CreateRoomResponse {
pub room_id: String,
pub room_id: OwnedRoomId,
}

#[derive(Debug, Deserialize)]
pub struct JoinRoomResponse {
pub room_id: String,
pub room_id: OwnedRoomId,
}

#[derive(Debug, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/test/src/matrix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod room_admin;
mod room_client;
mod shared_token_registration;
mod util;
Loading

0 comments on commit 9878fd6

Please sign in to comment.