DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: 48cdec8c4b46ff488355395941e0e428635a24ef canwa/src/service/matrix.rs -rw-r--r-- 3.6 KiB
48cdec8cJonni Liljamo feat(nix): hydraJobs 3 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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 async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::{
    config::{ServiceConfig, deserialize_token},
    routes::message::MessageForm,
};

use super::Service;

const COMMONMARK_OPTIONS: pulldown_cmark::Options =
    pulldown_cmark::Options::ENABLE_TABLES.union(pulldown_cmark::Options::ENABLE_STRIKETHROUGH);

#[derive(Clone, Deserialize, Serialize)]
pub struct MatrixConfig {
    pub instance: String,
    #[serde(deserialize_with = "deserialize_token")]
    pub token: String,
    pub room: String,
}

#[typetag::serde(name = "matrix")]
impl ServiceConfig for MatrixConfig {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

pub struct MatrixService {
    client: reqwest::Client,
    config: MatrixConfig,
}

impl MatrixService {
    pub fn new(
        client: reqwest::Client,
        config: MatrixConfig,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self { client, config })
    }

    fn gen_txn_id(&self) -> String {
        format!(
            "{}_{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or(std::time::Duration::new(0, 0))
                .as_nanos()
        )
    }
}

#[async_trait]
impl Service for MatrixService {
    async fn send(
        &self,
        form: &MessageForm,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let body = if form.format_commonmark {
            let bold_title = format!("**{}**", form.title.trim());
            let title_parser = pulldown_cmark::Parser::new_ext(&bold_title, COMMONMARK_OPTIONS);
            let mut html_title = String::new();
            pulldown_cmark::html::push_html(&mut html_title, title_parser);
            let body_parser =
                pulldown_cmark::Parser::new_ext(form.message.trim(), COMMONMARK_OPTIONS);
            let mut html_body = String::new();
            pulldown_cmark::html::push_html(&mut html_body, body_parser);
            json!({
                "msgtype": "m.notice",
                "body": format!("{}\n\n{}", form.title, form.message),
                "format": "org.matrix.custom.html",
                "formatted_body": format!("{}\n{}", html_title, html_body),
            })
        } else {
            json!({
                "msgtype": "m.notice",
                "body": format!("**{}**\n\n{}", form.title, form.message),
                "format": "org.matrix.custom.html",
                "formatted_body": format!("<p><strong>{}</strong></p>\n<p>{}</p>", form.title, form.message),
            })
        };
        let _ = self
            .client
            .put(format!(
                "{}/_matrix/client/v3/rooms/{}/send/m.room.message/{}",
                self.config.instance,
                self.config.room,
                self.gen_txn_id()
            ))
            .query(&[("access_token", &self.config.token)])
            .body(body.to_string())
            .send()
            .await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::config::Config;

    use super::*;

    #[tokio::test]
    async fn config() {
        Config::from_str(
            r#"
        [services.m]
        type = "matrix"
        instance = "matrix.tld"
        token = "secret"
        room = "!room:matrix.tld"
        "#,
        )
        .await
        .unwrap()
        .services["m"]
            .as_any()
            .downcast_ref::<MatrixConfig>()
            .unwrap();
    }
}