DEVELOPMENT ENVIRONMENT

~liljamo/canwa

ref: 59fec95b232dd0b1b8348e52c2f31062459a2cba canwa/src/service/matrix.rs -rw-r--r-- 3.0 KiB
59fec95bJonni Liljamo feat: optionally render commonmark to html for matrix 27 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
/*
 * 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::{
    MessageForm,
    config::{ServiceConfig, deserialize_token},
};

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) -> Self {
        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>> {
        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(())
    }
}