Added route to send message

This commit is contained in:
xeovalyte 2025-02-28 13:48:30 +01:00
parent e5aace6e68
commit 923a42ada6
Signed by: xeovalyte
SSH Key Fingerprint: SHA256:GWI1hq+MNKR2UOcvk7n9tekASXT8vyazK7vDF9Xyciw
4 changed files with 58 additions and 5 deletions

View File

@ -0,0 +1 @@
ALTER TYPE message_status ADD VALUE 'draft';

View File

@ -85,4 +85,24 @@ impl Message {
Ok(messages)
}
pub async fn update_status(
transaction: &mut sqlx::Transaction<'_, Postgres>,
message_id: &uuid::Uuid,
status: MessageStatus,
) -> Result<(), sqlx::Error> {
sqlx::query!(
"
UPDATE ONLY messages
SET status = $1
WHERE message_id = $2
",
status as MessageStatus,
message_id,
)
.execute(&mut **transaction)
.await?;
Ok(())
}
}

View File

@ -22,6 +22,7 @@ pub enum MessageStatus {
Pending,
Sent,
Canceled,
Draft,
}
bitflags! {
@ -92,7 +93,7 @@ impl Message {
thumbnail_url: message_create.thumbnail_url,
member_groups,
member_roles,
status: MessageStatus::Pending,
status: MessageStatus::Draft,
})
}
}

View File

@ -1,4 +1,9 @@
use axum::{extract::State, http::HeaderMap, routing::post, Json, Router};
use axum::{
extract::{Path, State},
http::HeaderMap,
routing::post,
Json, Router,
};
use bitflags::Flags;
use serde::Deserialize;
@ -7,14 +12,16 @@ use crate::{
database::model::Message as DbMessage,
model::{
member::{Groups, Roles},
message::{Channel, MessageCreate},
message::{Channel, MessageCreate, MessageStatus},
Message,
},
AppState,
};
pub fn routes() -> Router<AppState> {
Router::new().route("/messages", post(message_create))
Router::new()
.route("/messages", post(message_create))
.route("/messages/{message_id}/send", post(message_send))
}
#[derive(Debug, Deserialize)]
@ -30,7 +37,7 @@ pub async fn message_create(
State(state): State<AppState>,
headers: HeaderMap,
Json(request): Json<MessageCreateRequest>,
) -> Result<(), crate::Error> {
) -> Result<String, crate::Error> {
let user = get_user_from_header(&state.pool, &headers).await?;
user.authorize(&state.pool, Some(Roles::ADMIN | Roles::MESSAGES), None)
@ -53,5 +60,29 @@ pub async fn message_create(
transaction.commit().await?;
Ok(db_message.message_id.to_string())
}
pub async fn message_send(
State(state): State<AppState>,
headers: HeaderMap,
Path(message_id): Path<String>,
) -> Result<(), crate::Error> {
let user = get_user_from_header(&state.pool, &headers).await?;
user.authorize(&state.pool, Some(Roles::ADMIN | Roles::MESSAGES), None)
.await?;
let message_id: uuid::Uuid =
uuid::Uuid::parse_str(&message_id).map_err(|_| crate::Error::BadRequest {
expected: "Could not convert message id to uuid".to_string(),
})?;
let mut transaction = state.pool.begin().await?;
DbMessage::update_status(&mut transaction, &message_id, MessageStatus::Sent).await?;
transaction.commit().await?;
Ok(())
}