Add delete messages endpoint

This commit is contained in:
Niels Andriesse 2021-04-29 10:21:27 +10:00
parent bb15ca25de
commit 1f34858cbd
2 changed files with 31 additions and 5 deletions

View File

@ -556,6 +556,17 @@ pub fn get_messages(
// Message deletion
/// Deletes the messages with the given `ids` from the database, if present.
pub fn delete_messages(
ids: Vec<i64>, auth_token: &str, pool: &storage::DatabaseConnectionPool,
) -> Result<Response, Rejection> {
for id in ids {
delete_message(id, auth_token, pool)?;
}
let json = models::StatusCode { status_code: StatusCode::OK.as_u16() };
return Ok(warp::reply::json(&json).into_response());
}
/// Deletes the message with the given `id` from the database, if it's present.
pub fn delete_message(
id: i64, auth_token: &str, pool: &storage::DatabaseConnectionPool,

View File

@ -202,7 +202,7 @@ async fn handle_post_request(
file: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(message) => message,
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
@ -221,7 +221,7 @@ async fn handle_post_request(
file: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(message) => message,
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
@ -253,7 +253,7 @@ async fn handle_post_request(
public_key: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(message) => message,
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
@ -268,7 +268,7 @@ async fn handle_post_request(
public_key: String,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(message) => message,
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
@ -280,7 +280,7 @@ async fn handle_post_request(
reject_if_file_server_mode(path)?;
let body: models::ChangeModeratorRequestBody =
match serde_json::from_str(&rpc_call.body) {
Ok(message) => message,
Ok(body) => body,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
@ -288,6 +288,21 @@ async fn handle_post_request(
};
return handlers::add_moderator_public(body, &auth_token).await;
}
"delete_messages" => {
reject_if_file_server_mode(path)?;
#[derive(Debug, Deserialize)]
struct JSON {
ids: Vec<i64>,
}
let json: JSON = match serde_json::from_str(&rpc_call.body) {
Ok(json) => json,
Err(e) => {
warn!("Couldn't parse JSON from: {} due to error: {}.", rpc_call.body, e);
return Err(warp::reject::custom(Error::InvalidRpcCall));
}
};
return handlers::delete_messages(json.ids, &auth_token, &pool);
}
_ => {
warn!("Ignoring RPC call with invalid or unused endpoint: {}.", path);
return Err(warp::reject::custom(Error::InvalidRpcCall));