DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: efb58ac0c57a9d4dc8e6f8ea6776696bd93e9979 canwa/src/routes/message.rs -rw-r--r-- 1.0 KiB
efb58ac0Jonni Liljamo feat: route helpers 10 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * 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, "")
}