diff --git a/Cargo.toml b/Cargo.toml index d14f1f1..ea335e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = ["crates/core", "crates/matrix", "crates/server", "crates/test"] default-members = ["crates/server"] -resolver = "1" +resolver = "2" [workspace.dependencies] async-trait = "0.1.74" diff --git a/crates/matrix/Cargo.toml b/crates/matrix/Cargo.toml index 4304525..68b8b70 100644 --- a/crates/matrix/Cargo.toml +++ b/crates/matrix/Cargo.toml @@ -6,7 +6,7 @@ publish = false [dependencies] ruma-events = { version = "0.27.11", features = ["html", "markdown"] } -ruma-common = { version = "0.12.0", features = ["api", "rand"], default_features = false } +ruma-common = { version = "0.12.0", default_features = false, features = ["api", "rand"] } ruma-macros = "0.12.0" # Workspace Dependencies @@ -18,3 +18,7 @@ serde = { workspace = true } tokio = { workspace = true } tracing = { workspace = true } url = { workspace = true, features = ["serde"] } + +[features] +client = [] +server = [] diff --git a/crates/matrix/src/admin/user.rs b/crates/matrix/src/admin/user.rs index 905b02f..eb59f60 100644 --- a/crates/matrix/src/admin/user.rs +++ b/crates/matrix/src/admin/user.rs @@ -1,271 +1,3 @@ -//! [User Admin API](https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#user-admin-api) -//! -//! To use it, you will need to authenticate by providing an `access_token` -//! for a server admin: see Admin API. - -mod get_user; -mod get_users; -mod set_user; - -//use anyhow::Result; -//use ruma_common::UserId; -//use serde::{Deserialize, Serialize}; -//use tracing::instrument; -//use url::Url; - -//#[derive(Default)] -//pub struct UserService; - -//#[derive(Debug, Serialize, Deserialize)] -//pub struct ExternalId { -// pub auth_provider: String, -// pub external_id: String, -//} - -//#[derive(Debug, Serialize, Deserialize)] -//pub struct ThreePid { -// pub medium: String, -// pub address: String, -// pub added_at: u64, -// pub validated_at: u64, -//} - -//#[derive(Debug, Serialize, Deserialize)] -//pub struct User { -// /// User name postfixed with Matrix instance Host -// /// E.g. `@user:example.com` -// pub name: String, -// pub displayname: Option, -// pub threepids: Vec, -// pub avatar_url: Option, -// pub is_guest: bool, -// pub admin: bool, -// pub deactivated: bool, -// pub erased: bool, -// pub shadow_banned: bool, -// pub creation_ts: u64, -// pub appservice_id: Option, -// pub consent_server_notice_sent: Option, -// pub consent_version: Option, -// pub consent_ts: Option, -// pub external_ids: Vec, -// pub user_type: Option, -// pub locked: bool, -//} - -//#[derive(Debug, Serialize, Deserialize)] -//pub struct CreateUserBody { -// pub password: String, -// pub logout_devices: bool, -// pub displayname: Option, -// pub avatar_url: Option, -// pub threepids: Vec, -// pub external_ids: Vec, -// pub admin: bool, -// pub deactivated: bool, -// pub user_type: Option, -// pub locked: bool, -//} - -//#[derive(Debug, Default, Serialize, Deserialize)] -//pub struct ListUsersQuery { -// pub user_id: Option, -// pub name: Option, -// pub guests: Option, -// pub admins: Option, -// pub deactivated: Option, -// pub limit: Option, -// pub from: Option, -//} - -///// Data type for the response of the `GET /_synapse/admin/v2/users` endpoint. -//#[derive(Debug, Default, Serialize, Deserialize)] -//pub struct ListUser { -// pub name: String, -// pub user_type: Option, -// pub is_guest: usize, -// pub admin: usize, -// pub deactivated: usize, -// pub shadow_banned: bool, -// pub avatar_url: Option, -// pub creation_ts: u64, -// pub last_seen_ts: Option, -// pub erased: bool, -// pub locked: bool, -//} - -//#[derive(Debug, Default, Serialize, Deserialize)] -//pub struct ListUsersResponse { -// pub users: Vec, -// pub total: u64, -// #[serde(default)] -// pub next_token: Option, -//} - -//#[derive(Debug, Serialize, Deserialize)] -//pub struct UpdateUserBody { -// pub password: String, -// pub logout_devices: bool, -// pub displayname: Option, -// pub avatar_url: Option, -// pub threepids: Vec, -// pub external_ids: Vec, -// pub admin: bool, -// pub deactivated: bool, -// pub user_type: Option, -// pub locked: bool, -//} - -//#[derive(Debug, Default, Serialize, Deserialize)] -//pub struct LoginAsUserBody { -// pub valid_until_ms: Option, -//} - -//#[derive(Debug, Default, Serialize, Deserialize)] -//pub struct LoginAsUserResponse { -// pub access_token: String, -//} - -//pub struct QueryUserDataResponse { -// pub name: String, -// pub displayname: Option, -// pub threepids: Vec, -// pub avatar_url: Option, -// pub is_guest: bool, -// pub admin: bool, -// pub deactivated: bool, -// pub erased: bool, -// pub shadow_banned: bool, -// pub creation_ts: i64, -// pub appservice_id: Option, -// pub consent_server_notice_sent: Option, -// pub consent_version: Option, -// pub consent_ts: Option, -// pub external_ids: Vec>, -// pub user_type: Option, -//} - -//impl UserService { -// /// This API returns information about a specific user account. -// /// -// /// 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 { -// let resp = client -// .get(format!( -// "/_synapse/admin/v2/users/{user_id}", -// user_id = user_id -// )) -// .await?; - -// if resp.status().is_success() { -// return Ok(resp.json().await?); -// } - -// let error = resp.json::().await?; - -// Err(anyhow::anyhow!(error.error)) -// } - -// /// Allows an administrator to create a user account. -// /// -// /// Note that internally Synapse uses this same endpoint to modify an -// /// existing user account, so this method will modify the existing user -// /// if [`UserId`] matches. -// /// -// /// 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 { -// let resp = client -// .put_json( -// format!("/_synapse/admin/v2/users/{user_id}", user_id = user_id), -// &body, -// ) -// .await?; - -// if resp.status().is_success() { -// return Ok(resp.json().await?); -// } - -// let error = resp.json::().await?; - -// Err(anyhow::anyhow!(error.error)) -// } - -// /// Returns all local user accounts. By default, the response is ordered by -// /// ascending user ID. -// /// -// /// 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 { -// let resp = client.get_query("/_synapse/admin/v2/users", &query).await?; - -// if resp.status().is_success() { -// return Ok(resp.json().await?); -// } - -// let error = resp.json::().await?; - -// Err(anyhow::anyhow!(error.error)) -// } - -// /// Allows an administrator to modify a user account -// /// -// /// 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 { -// let resp = client -// .put_json( -// format!("/_synapse/admin/v2/users/{user_id}", user_id = user_id), -// &body, -// ) -// .await?; - -// if resp.status().is_success() { -// return Ok(resp.json().await?); -// } - -// let error = resp.json::().await?; - -// Err(anyhow::anyhow!(error.error)) -// } - -// /// **Note: This API is disabled when MSC3861 is enabled. [See #15582][1]** -// /// -// /// Get an access token that can be used to authenticate as that user. -// /// Useful for when admins wish to do actions on behalf of a user. -// /// -// /// An optional `valid_until_ms` field can be specified in the request body -// /// as an integer timestamp that specifies when the token should expire. -// /// -// /// **By default tokens do not expire. Note that this API does not allow a -// /// user to login as themselves (to create more tokens).** -// /// -// /// Refer: https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#login-as-a-user -// /// -// /// [1]: https://github.com/matrix-org/synapse/pull/15582 -// #[instrument(skip(client))] -// pub async fn login_as_user( -// client: &Client, -// user_id: &UserId, -// body: LoginAsUserBody, -// ) -> Result { -// let resp = client -// .post_json( -// format!( -// "/_synapse/admin/v1/users/{user_id}/login", -// user_id = user_id -// ), -// &body, -// ) -// .await?; - -// if resp.status().is_success() { -// return Ok(resp.json().await?); -// } - -// let error = resp.json::().await?; - -// Err(anyhow::anyhow!(error.error)) -// } -//} +pub mod get_user; +pub mod get_users; +pub mod set_user; diff --git a/crates/matrix/src/admin/user/get_user.rs b/crates/matrix/src/admin/user/get_user.rs index c306f4e..f19b5b1 100644 --- a/crates/matrix/src/admin/user/get_user.rs +++ b/crates/matrix/src/admin/user/get_user.rs @@ -1,11 +1,58 @@ -use ruma_common::OwnedUserId; -use ruma_macros::request; - - +use ruma_common::{ + api::{request, response, Metadata}, + metadata, + thirdparty::ThirdPartyIdentifier, + OwnedMxcUri, OwnedUserId, +}; +#[allow(dead_code)] +const METADATA: Metadata = metadata! { + method: GET, + rate_limited: false, + authentication: AccessToken, + history: { + unstable => "/_synapse/admin/v2/users/:name", + } +}; #[request(error = crate::Error)] pub struct Request { - #[ruma_api(query)] - pub user_id: OwnedUserId, + #[ruma_api(path)] + pub name: OwnedUserId, +} + +#[response(error = crate::Error)] +pub struct Response { + pub name: OwnedUserId, + + pub displayname: Option, + + pub threepids: Vec, + + pub avatar_url: Option, + + pub admin: bool, + + pub deactivated: bool, + + pub erased: bool, + + pub shadow_banned: bool, + + pub creation_ts: u64, + + pub consent_server_notice_sent: Option, + + pub consent_ts: Option, + + pub external_ids: Vec, + + pub locked: bool, +} + +#[derive(Clone, Debug)] +pub struct ExternalId { + pub auth_provider: String, + + pub external_id: String, } diff --git a/crates/matrix/src/admin/user/get_users.rs b/crates/matrix/src/admin/user/get_users.rs index e69de29..eeab703 100644 --- a/crates/matrix/src/admin/user/get_users.rs +++ b/crates/matrix/src/admin/user/get_users.rs @@ -0,0 +1,97 @@ +use ruma_common::{ + api::{request, response, Metadata, Direction}, + thirdparty::ThirdPartyIdentifier, + OwnedMxcUri, OwnedUserId, metadata, +}; +use ruma_macros::{request, response}; + +#[allow(dead_code)] +const METADATA: Metadata = metadata! { + method: GET, + rate_limited: false, + authentication: AccessToken, + history: { + unstable => "/_synapse/admin/v2/users", + } +}; + +#[request(error = crate::Error)] +pub struct Request { + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub user_id: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub name: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub admins: Option, + + #[serde(skip_serializing_if = "ruma_common::serde::is_default")] + #[ruma_api(query)] + pub deactivated: bool, + + #[serde(skip_serializing_if = "Option::is_none")] + #[ruma_api(query)] + pub limit: Option, + + #[serde(skip_serializing_if = "ruma_common::serde::is_default")] + #[ruma_api(query)] + pub from: u64, + + #[serde(skip_serializing_if = "ruma_common::serde::is_default")] + #[ruma_api(query)] + pub order_by: OrderBy, + + #[serde(skip_serializing_if = "ruma_common::serde::is_default")] + #[ruma_api(query)] + pub dir: Direction, +} + +#[response(error = crate::Error)] +pub struct Response { + pub name: OwnedUserId, + + pub displayname: Option, + + pub threepids: Vec, + + pub avatar_url: Option, + + pub admin: bool, + + pub deactivated: bool, + + pub erased: bool, + + pub shadow_banned: bool, + + pub creation_ts: u64, + + pub locked: bool, +} + +#[derive(Clone, Debug, Default)] +#[allow(dead_code)] +pub enum OrderBy { + #[default] + Name, + + Admin, + + UserType, + + Deactivated, + + ShadowBanned, + + Displayname, + + AvatarUrl, + + CreationTs, + + LastSeenTs, +} diff --git a/crates/matrix/src/lib.rs b/crates/matrix/src/lib.rs index 2d6ce8a..16bf545 100644 --- a/crates/matrix/src/lib.rs +++ b/crates/matrix/src/lib.rs @@ -1,20 +1,6 @@ -//! Crate to centralize all Matrix dependencies. -//! -//! Reexports `matrix_sdk` and provides implementations on Matrix Admin API. - -// mod http; - +pub mod admin; +pub mod client; // pub mod filter; -// pub use http::Client; - -/// Ruma re-exports -// pub use ruma_common; -// pub use ruma_events; - -// mod api { -mod admin; - // mod client; -// } - +#[allow(unused_imports)] use ruma_common::api::error::MatrixError as Error;