Skip to content

Commit

Permalink
feat: Ruma API macro to clear up HTTP boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 24, 2024
1 parent 63de180 commit cd39d60
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 296 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 5 additions & 1 deletion crates/matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,3 +18,7 @@ serde = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
url = { workspace = true, features = ["serde"] }

[features]
client = []
server = []
274 changes: 3 additions & 271 deletions crates/matrix/src/admin/user.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
// pub threepids: Vec<ThreePid>,
// pub avatar_url: Option<Url>,
// 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<String>,
// pub consent_server_notice_sent: Option<u64>,
// pub consent_version: Option<String>,
// pub consent_ts: Option<u64>,
// pub external_ids: Vec<ExternalId>,
// pub user_type: Option<String>,
// pub locked: bool,
//}

//#[derive(Debug, Serialize, Deserialize)]
//pub struct CreateUserBody {
// pub password: String,
// pub logout_devices: bool,
// pub displayname: Option<String>,
// pub avatar_url: Option<Url>,
// pub threepids: Vec<ThreePid>,
// pub external_ids: Vec<ExternalId>,
// pub admin: bool,
// pub deactivated: bool,
// pub user_type: Option<String>,
// pub locked: bool,
//}

//#[derive(Debug, Default, Serialize, Deserialize)]
//pub struct ListUsersQuery {
// pub user_id: Option<String>,
// pub name: Option<String>,
// pub guests: Option<bool>,
// pub admins: Option<bool>,
// pub deactivated: Option<bool>,
// pub limit: Option<u64>,
// pub from: Option<u64>,
//}

///// 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<String>,
// pub is_guest: usize,
// pub admin: usize,
// pub deactivated: usize,
// pub shadow_banned: bool,
// pub avatar_url: Option<Url>,
// pub creation_ts: u64,
// pub last_seen_ts: Option<u64>,
// pub erased: bool,
// pub locked: bool,
//}

//#[derive(Debug, Default, Serialize, Deserialize)]
//pub struct ListUsersResponse {
// pub users: Vec<ListUser>,
// pub total: u64,
// #[serde(default)]
// pub next_token: Option<String>,
//}

//#[derive(Debug, Serialize, Deserialize)]
//pub struct UpdateUserBody {
// pub password: String,
// pub logout_devices: bool,
// pub displayname: Option<String>,
// pub avatar_url: Option<Url>,
// pub threepids: Vec<ThreePid>,
// pub external_ids: Vec<ExternalId>,
// pub admin: bool,
// pub deactivated: bool,
// pub user_type: Option<String>,
// pub locked: bool,
//}

//#[derive(Debug, Default, Serialize, Deserialize)]
//pub struct LoginAsUserBody {
// pub valid_until_ms: Option<u64>,
//}

//#[derive(Debug, Default, Serialize, Deserialize)]
//pub struct LoginAsUserResponse {
// pub access_token: String,
//}

//pub struct QueryUserDataResponse {
// pub name: String,
// pub displayname: Option<String>,
// pub threepids: Vec<ThreePid>,
// pub avatar_url: Option<Url>,
// 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<String>,
// pub consent_server_notice_sent: Option<bool>,
// pub consent_version: Option<bool>,
// pub consent_ts: Option<bool>,
// pub external_ids: Vec<Vec<ExternalId>>,
// pub user_type: Option<String>,
//}

//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<User> {
// 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::<MatrixError>().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<User> {
// 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::<MatrixError>().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<ListUsersResponse> {
// 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::<MatrixError>().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<User> {
// 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::<MatrixError>().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<LoginAsUserResponse> {
// 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::<MatrixError>().await?;

// Err(anyhow::anyhow!(error.error))
// }
//}
pub mod get_user;
pub mod get_users;
pub mod set_user;
59 changes: 53 additions & 6 deletions crates/matrix/src/admin/user/get_user.rs
Original file line number Diff line number Diff line change
@@ -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<String>,

pub threepids: Vec<ThirdPartyIdentifier>,

pub avatar_url: Option<OwnedMxcUri>,

pub admin: bool,

pub deactivated: bool,

pub erased: bool,

pub shadow_banned: bool,

pub creation_ts: u64,

pub consent_server_notice_sent: Option<u64>,

pub consent_ts: Option<u64>,

pub external_ids: Vec<ExternalId>,

pub locked: bool,
}

#[derive(Clone, Debug)]
pub struct ExternalId {
pub auth_provider: String,

pub external_id: String,
}
Loading

0 comments on commit cd39d60

Please sign in to comment.