/*
* Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
use std::sync::Arc;
use axum::{Json, http::HeaderMap, response::IntoResponse};
use reqwest::StatusCode;
use serde::Deserialize;
use crate::{routes::extract_token, state::State};
#[derive(Deserialize)]
pub struct MessageForm {
pub title: String,
pub message: String,
#[serde(default)]
pub format_commonmark: bool,
}
pub async fn message(
state: Arc<State>,
headers: HeaderMap,
Json(message): Json<MessageForm>,
) -> impl IntoResponse {
let token = extract_token!(headers);
let notifier = match state.find_notifier(token) {
Some(n) => n,
None => return (StatusCode::UNAUTHORIZED, "unauthorized"),
};
if let Err(err) = state.send_message(notifier, &message).await {
tracing::error!(?err, "message sending failed");
return (StatusCode::INTERNAL_SERVER_ERROR, "failed to send message");
};
(StatusCode::OK, "")
}