|
| 1 | +// Copyright 2024 New Vector Ltd. |
| 2 | +// Copyright 2024 The Matrix.org Foundation C.I.C. |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 5 | +// Please see LICENSE in the repository root for full details. |
| 6 | + |
| 7 | +use aide::{transform::TransformOperation, OperationIo}; |
| 8 | +use axum::{response::IntoResponse, Json}; |
| 9 | +use hyper::StatusCode; |
| 10 | +use ulid::Ulid; |
| 11 | +use mas_storage::{Pagination, RepositoryAccess}; |
| 12 | +use mas_storage::user::{UserEmailFilter, UserEmailRepository}; |
| 13 | +use crate::{ |
| 14 | + admin::{ |
| 15 | + call_context::CallContext, |
| 16 | + model::User, |
| 17 | + params::UlidPathParam, |
| 18 | + response::{ErrorResponse, SingleResponse}, |
| 19 | + }, |
| 20 | + impl_from_error_for_route, |
| 21 | +}; |
| 22 | +use crate::admin::model::UserEmail; |
| 23 | + |
| 24 | +#[derive(Debug, thiserror::Error, OperationIo)] |
| 25 | +#[aide(output_with = "Json<ErrorResponse>")] |
| 26 | +pub enum RouteError { |
| 27 | + #[error(transparent)] |
| 28 | + Internal(Box<dyn std::error::Error + Send + Sync + 'static>), |
| 29 | + |
| 30 | + #[error("User ID {0} not found")] |
| 31 | + NotFound(Ulid), |
| 32 | +} |
| 33 | + |
| 34 | +impl_from_error_for_route!(mas_storage::RepositoryError); |
| 35 | + |
| 36 | +impl IntoResponse for RouteError { |
| 37 | + fn into_response(self) -> axum::response::Response { |
| 38 | + let error = ErrorResponse::from_error(&self); |
| 39 | + let status = match self { |
| 40 | + Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, |
| 41 | + Self::NotFound(_) => StatusCode::NOT_FOUND, |
| 42 | + }; |
| 43 | + (status, Json(error)).into_response() |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub fn doc(operation: TransformOperation) -> TransformOperation { |
| 48 | + operation |
| 49 | + .id("getUserEmails") |
| 50 | + .summary("Get a user's emails") |
| 51 | + .tag("user") |
| 52 | + .response_with::<200, Json<SingleResponse<Vec<UserEmail>>>, _>(|t| { |
| 53 | + let [sample, ..] = UserEmail::samples(); |
| 54 | + let response = SingleResponse::new_canonical(sample); |
| 55 | + t.description("User was found").example(response) |
| 56 | + }) |
| 57 | + .response_with::<404, RouteError, _>(|t| { |
| 58 | + let response = ErrorResponse::from_error(&RouteError::NotFound(Ulid::nil())); |
| 59 | + t.description("User was not found").example(response) |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +#[tracing::instrument(name = "handler.admin.v1.users.get_emails", skip_all, err)] |
| 64 | +pub async fn handler( |
| 65 | + CallContext { mut repo, .. }: CallContext, |
| 66 | + id: UlidPathParam, |
| 67 | +) -> Result<Json<SingleResponse<Vec<UserEmail>>>, RouteError> { |
| 68 | + let user = repo |
| 69 | + .user() |
| 70 | + .lookup(*id) |
| 71 | + .await? |
| 72 | + .ok_or(RouteError::NotFound(*id))?; |
| 73 | + |
| 74 | + let emails: Vec<UserEmail> = repo |
| 75 | + .user_email() |
| 76 | + .all(&user) |
| 77 | + .await? |
| 78 | + .iter() |
| 79 | + .map(|e| UserEmail::from(e)) |
| 80 | + .into(); |
| 81 | + |
| 82 | + Ok(Json(SingleResponse::new_canonical(emails))) |
| 83 | +} |
0 commit comments