Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformatting effort and test organization #25

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
87 changes: 27 additions & 60 deletions crates/matrix/src/admin/resources/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,17 @@

use anyhow::Result;
use ruma_common::{serde::Raw, EventId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomId};
use ruma_events::{AnyMessageLikeEvent, AnyStateEvent};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use ruma_events::{AnyMessageLikeEvent, AnyStateEvent, AnyTimelineEvent};
use serde::{Deserialize, Serialize};
use tracing::instrument;

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

#[derive(Default)]
pub struct RoomService;

#[derive(Default, Debug, Serialize)]
pub struct RoomEventFilter {
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub not_types: Vec<String>,

#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub not_rooms: Vec<OwnedRoomId>,

#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,

#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub rooms: Vec<OwnedRoomId>,

#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub not_senders: Vec<OwnedUserId>,

#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub senders: Vec<OwnedUserId>,

#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub types: Vec<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub include_urls: Option<bool>,

pub lazy_load_members: bool,

pub unread_thread_notifications: bool,
}

#[derive(Default, Debug, Serialize)]
pub struct ListBody {
pub struct ListRoomQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<u64>,

Expand All @@ -62,7 +31,7 @@ pub struct ListBody {
}

#[derive(Debug, Serialize)]
pub struct MessagesBody {
pub struct MessagesQuery {
pub from: String,

#[serde(skip_serializing_if = "String::is_empty")]
Expand All @@ -78,15 +47,15 @@ pub struct MessagesBody {
}

#[derive(Default, Debug, Serialize)]
pub struct TimestampToEventBody {
pub struct TimestampToEventQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub ts: Option<u64>,

pub direction: Direction,
}

#[derive(Default, Debug, Serialize)]
pub struct EventContextBody {
pub struct EventContextQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,

Expand All @@ -95,7 +64,7 @@ pub struct EventContextBody {
}

#[derive(Debug, Serialize)]
pub struct ReplaceRoomBody {
pub struct ReplaceRoomQuery {
#[serde(rename = "new_room_user_id")]
pub admin: OwnedUserId,

Expand All @@ -107,17 +76,17 @@ pub struct ReplaceRoomBody {
}

#[derive(Default, Debug, Serialize)]
pub struct DeleteBody {
pub struct DeleteQuery {
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub new_room: Option<ReplaceRoomBody>,
pub new_room: Option<ReplaceRoomQuery>,

pub block: bool,

pub purge: bool,
}

#[derive(Debug, Deserialize)]
pub struct ListResponse {
pub struct ListRoomResponse {
pub rooms: Vec<Room>,
pub offset: Option<u64>,
pub total_rooms: Option<u64>,
Expand Down Expand Up @@ -176,8 +145,8 @@ pub struct RoomDetails {
}

#[derive(Debug, Deserialize)]
pub struct GetEventsResponse<T> {
pub chunk: T,
pub struct GetEventsResponse {
pub chunk: Raw<Vec<AnyTimelineEvent>>,
pub start: String,
pub end: String,
pub state: Option<Vec<State>>,
Expand Down Expand Up @@ -219,7 +188,7 @@ pub struct EventContextResponse {
}

#[derive(Debug, Deserialize)]
pub struct DeleteResponse {
pub struct DeleteRoomResponse {
pub kicked_users: Vec<OwnedUserId>,
pub failed_to_kick_users: Vec<OwnedUserId>,
pub local_aliases: Vec<OwnedRoomAliasId>,
Expand Down Expand Up @@ -253,10 +222,8 @@ 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, params: ListBody) -> Result<ListResponse> {
let resp = client
.get_query("/_synapse/admin/v1/rooms", &params)
.await?;
pub async fn get_all(client: &Client, query: ListRoomQuery) -> Result<ListRoomResponse> {
let resp = client.get_query("/_synapse/admin/v1/rooms", &query).await?;

if resp.status().is_success() {
return Ok(resp.json().await?);
Expand Down Expand Up @@ -318,15 +285,15 @@ impl RoomService {
pub async fn get_timestamp_to_event(
client: &Client,
room_id: &RoomId,
params: TimestampToEventBody,
query: TimestampToEventQuery,
) -> Result<TimestampToEventResponse> {
let resp = client
.get_query(
format!(
"/_synapse/admin/v1/rooms/{room_id}/timestamp_to_event",
room_id = room_id
),
&params,
&query,
)
.await?;

Expand Down Expand Up @@ -397,12 +364,12 @@ impl RoomService {
pub async fn delete_room(
client: &Client,
room_id: &RoomId,
params: DeleteBody,
) -> Result<DeleteResponse> {
query: DeleteQuery,
) -> Result<DeleteRoomResponse> {
let resp = client
.delete_json(
format!("/_synapse/admin/v1/rooms/{room_id}", room_id = room_id),
&params,
&query,
)
.await?;

Expand All @@ -421,18 +388,18 @@ impl RoomService {
///
/// Refer: https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#room-state-api
#[instrument(skip(client))]
pub async fn get_room_events<M: DeserializeOwned>(
pub async fn get_room_events(
client: &Client,
room_id: &RoomId,
params: MessagesBody,
) -> Result<GetEventsResponse<Raw<Vec<M>>>> {
query: MessagesQuery,
) -> Result<GetEventsResponse> {
let resp = client
.get_query(
format!(
"/_synapse/admin/v1/rooms/{room_id}/messages",
room_id = room_id
),
&params,
&query,
)
.await?;

Expand All @@ -454,7 +421,7 @@ impl RoomService {
client: &Client,
room_id: &RoomId,
event_id: &EventId,
params: EventContextBody,
query: EventContextQuery,
) -> Result<EventContextResponse> {
let resp = client
.get_query(
Expand All @@ -463,7 +430,7 @@ impl RoomService {
room_id = room_id,
event_id = event_id,
),
&params,
&query,
)
.await?;

Expand Down
Loading
Loading