Skip to content

Commit

Permalink
refactor(api_admin)!: use query instead of body (#69)
Browse files Browse the repository at this point in the history
* refactor(api_admin)!: use query instead of body

* chore(justfile): update recipe

* docs: update create-account
  • Loading branch information
kwaa authored Sep 26, 2024
1 parent dfbf4fb commit 0911e39
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ account method name:
fi

_account method name:
curl -X POST "http://localhost:${HATSU_LISTEN_PORT}/api/v0/admin/{{method}}-account?token=${HATSU_ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"name\": \"{{name}}\"}"
curl -X POST "http://localhost:${HATSU_LISTEN_PORT}/api/v0/admin/{{method}}-account?name={{name}}&token=${HATSU_ACCESS_TOKEN}"

# /api/v0/admin/block-url
block-url url:
Expand Down
6 changes: 3 additions & 3 deletions crates/api_admin/src/entities/create_remove_account.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use utoipa::{IntoParams, ToSchema};

#[derive(Deserialize, ToSchema)]
pub struct CreateRemoveAccount {
#[derive(Deserialize, IntoParams)]
pub struct CreateRemoveAccountQuery {
pub name: String,
}

Expand Down
2 changes: 1 addition & 1 deletion crates/api_admin/src/entities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod block_url_or_acct;
mod create_remove_account;

pub use block_url_or_acct::{BlockUrlQuery, BlockUrlResult};
pub use create_remove_account::{CreateRemoveAccount, CreateRemoveAccountResult};
pub use create_remove_account::{CreateRemoveAccountQuery, CreateRemoveAccountResult};
11 changes: 6 additions & 5 deletions crates/api_admin/src/routes/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::ops::Deref;

use activitypub_federation::config::Data;
use axum::{debug_handler, http::StatusCode, Json};
use axum::{debug_handler, extract::Query, http::StatusCode, Json};
use hatsu_apub::actors::ApubUser;
use hatsu_db_schema::prelude::User;
use hatsu_utils::{AppData, AppError};
use sea_orm::{ActiveModelTrait, EntityTrait, IntoActiveModel};

use crate::{
entities::{CreateRemoveAccount, CreateRemoveAccountResult},
entities::{CreateRemoveAccountQuery, CreateRemoveAccountResult},
TAG,
};

Expand All @@ -17,6 +17,7 @@ use crate::{
post,
tag = TAG,
path = "/api/v0/admin/create-account",
params(CreateRemoveAccountQuery),
responses(
(status = CREATED, description = "create successfully", body = CreateRemoveAccountResult),
(status = BAD_REQUEST, description = "error", body = AppError)
Expand All @@ -26,10 +27,10 @@ use crate::{
#[debug_handler]
pub async fn create_account(
data: Data<AppData>,
Json(payload): Json<CreateRemoveAccount>,
query: Query<CreateRemoveAccountQuery>,
) -> Result<(StatusCode, Json<CreateRemoveAccountResult>), AppError> {
if let Some(account) = User::find_by_id(
hatsu_utils::url::generate_user_url(data.domain(), &payload.name)?.to_string(),
hatsu_utils::url::generate_user_url(data.domain(), &query.name)?.to_string(),
)
.one(&data.conn)
.await?
Expand All @@ -40,7 +41,7 @@ pub async fn create_account(
Some(StatusCode::BAD_REQUEST),
))
} else {
let account = ApubUser::new(data.domain(), &payload.name)
let account = ApubUser::new(data.domain(), &query.name)
.await?
.deref()
.clone()
Expand Down
3 changes: 1 addition & 2 deletions crates/api_admin/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use utoipa::{
};
use utoipa_axum::{router::OpenApiRouter, routes};

use crate::entities::{BlockUrlResult, CreateRemoveAccount, CreateRemoveAccountResult};
use crate::entities::{BlockUrlResult, CreateRemoveAccountResult};

mod block_url;
mod create_account;
Expand All @@ -26,7 +26,6 @@ pub const TAG: &str = "hatsu::admin";
#[openapi(
components(schemas(
BlockUrlResult,
CreateRemoveAccount,
CreateRemoveAccountResult
)),
modifiers(&SecurityAddon),
Expand Down
18 changes: 8 additions & 10 deletions crates/api_admin/src/routes/remove_account.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use activitypub_federation::config::Data;
use axum::{debug_handler, http::StatusCode, Json};
use axum::{debug_handler, extract::Query, http::StatusCode, Json};
use hatsu_db_schema::prelude::User;
use hatsu_utils::{AppData, AppError};
use sea_orm::EntityTrait;

use crate::{
entities::{CreateRemoveAccount, CreateRemoveAccountResult},
entities::{CreateRemoveAccountQuery, CreateRemoveAccountResult},
TAG,
};

Expand All @@ -14,6 +14,7 @@ use crate::{
post,
tag = TAG,
path = "/api/v0/admin/remove-account",
params(CreateRemoveAccountQuery),
responses(
// (status = OK, description = "remove successfully", body = CreateRemoveAccountResult),
(status = METHOD_NOT_ALLOWED, description = "not implemented", body = CreateRemoveAccountResult),
Expand All @@ -24,10 +25,10 @@ use crate::{
#[debug_handler]
pub async fn remove_account(
data: Data<AppData>,
Json(payload): Json<CreateRemoveAccount>,
query: Query<CreateRemoveAccountQuery>,
) -> Result<(StatusCode, Json<CreateRemoveAccountResult>), AppError> {
match User::find_by_id(
hatsu_utils::url::generate_user_url(data.domain(), &payload.name)?.to_string(),
hatsu_utils::url::generate_user_url(data.domain(), &query.name)?.to_string(),
)
.one(&data.conn)
.await?
Expand All @@ -47,18 +48,15 @@ pub async fn remove_account(
Ok((
StatusCode::METHOD_NOT_ALLOWED,
Json(CreateRemoveAccountResult {
name: payload.name.clone(),
name: query.name.clone(),
// message: format!("Successfully removed account: {}", payload.name),
message: format!(
"Remove account API not yet implemented: {}",
payload.name
),
message: format!("Remove account API not yet implemented: {}", query.name),
}),
))
}
},
None => Err(AppError::new(
format!("The account does not exist: {}", payload.name),
format!("The account does not exist: {}", query.name),
None,
Some(StatusCode::BAD_REQUEST),
)),
Expand Down
4 changes: 1 addition & 3 deletions docs/src/admins/create-account.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ docker exec -it hatsu /bin/bash
You can also access the API via curl, as `Justfile` does.

```bash
curl -X POST "http://localhost:$(echo $HATSU_LISTEN_PORT)/api/v0/admin/create-account?token=$(echo $HATSU_ACCESS_TOKEN)" \
-H "Content-Type: application/json" \
-d "{\"name\": \"example.com\"}"
NAME="example.com" curl -X POST "http://localhost:$(echo $HATSU_LISTEN_PORT)/api/v0/admin/create-account?name=$(echo $NAME)&token=$(echo $HATSU_ACCESS_TOKEN)"
```

0 comments on commit 0911e39

Please sign in to comment.