matrix_bridge_telegram/
matrix.rs1pub mod command_handler;
2pub mod event_handler;
3
4use std::sync::Arc;
5
6use anyhow::Result;
7
8use crate::config::Config;
9
10pub struct MatrixAppservice {
11 config: Arc<Config>,
12}
13
14impl MatrixAppservice {
15 pub async fn new(config: Arc<Config>) -> Result<Self> {
16 Ok(Self { config })
17 }
18
19 pub async fn set_processor(&self, _processor: Arc<MatrixEventProcessor>) {}
20
21 pub async fn start(&self) -> Result<()> {
22 tracing::info!("Matrix Appservice started");
23 Ok(())
24 }
25
26 pub async fn get_room_members(&self, _room_id: &str) -> Result<Vec<String>> {
27 Ok(Vec::new())
28 }
29
30 pub async fn send_message(&self, _room_id: &str, _content: serde_json::Value) -> Result<String> {
31 Ok(String::new())
32 }
33
34 pub async fn send_text(&self, room_id: &str, text: &str) -> Result<String> {
35 let content = serde_json::json!({
36 "msgtype": "m.text",
37 "body": text
38 });
39 self.send_message(room_id, content).await
40 }
41
42 pub async fn send_emote(&self, room_id: &str, text: &str) -> Result<String> {
43 let content = serde_json::json!({
44 "msgtype": "m.emote",
45 "body": text
46 });
47 self.send_message(room_id, content).await
48 }
49
50 pub async fn send_notice(&self, room_id: &str, text: &str) -> Result<String> {
51 let content = serde_json::json!({
52 "msgtype": "m.notice",
53 "body": text
54 });
55 self.send_message(room_id, content).await
56 }
57
58 pub async fn send_image(&self, room_id: &str, url: &str, body: &str) -> Result<String> {
59 let content = serde_json::json!({
60 "msgtype": "m.image",
61 "url": url,
62 "body": body
63 });
64 self.send_message(room_id, content).await
65 }
66
67 pub async fn send_file(&self, room_id: &str, url: &str, filename: &str) -> Result<String> {
68 let content = serde_json::json!({
69 "msgtype": "m.file",
70 "url": url,
71 "body": filename
72 });
73 self.send_message(room_id, content).await
74 }
75
76 pub async fn redact_event(&self, _room_id: &str, _event_id: &str, _reason: Option<&str>) -> Result<()> {
77 Ok(())
78 }
79
80 pub async fn get_user_mxid(&self, telegram_user_id: i64) -> String {
81 let template = &self.config.portal.username_template;
82 template.replace("{userid}", &telegram_user_id.to_string())
83 }
84}
85
86pub struct MatrixEventProcessor {
87 handler: Arc<dyn MatrixEventHandler + Send + Sync>,
88 age_limit_ms: u64,
89}
90
91impl MatrixEventProcessor {
92 pub fn with_age_limit(handler: Arc<dyn MatrixEventHandler + Send + Sync>, age_limit_ms: u64) -> Self {
93 Self { handler, age_limit_ms }
94 }
95
96 pub fn age_limit_ms(&self) -> u64 {
97 self.age_limit_ms
98 }
99}
100
101#[async_trait::async_trait]
102pub trait MatrixEventHandler: Send + Sync {
103 async fn handle_room_message(&self, room_id: &str, event: &serde_json::Value);
104 async fn handle_room_redaction(&self, room_id: &str, event: &serde_json::Value);
105 async fn handle_room_member(&self, room_id: &str, event: &serde_json::Value);
106}
107
108pub struct MatrixEventHandlerImpl {
109 matrix_client: Arc<MatrixAppservice>,
110 bridge: Option<Arc<crate::bridge::BridgeCore>>,
111}
112
113impl MatrixEventHandlerImpl {
114 pub fn new(matrix_client: Arc<MatrixAppservice>) -> Self {
115 Self {
116 matrix_client,
117 bridge: None,
118 }
119 }
120
121 pub fn set_bridge(&mut self, bridge: Arc<crate::bridge::BridgeCore>) {
122 self.bridge = Some(bridge);
123 }
124}
125
126#[async_trait::async_trait]
127impl MatrixEventHandler for MatrixEventHandlerImpl {
128 async fn handle_room_message(&self, room_id: &str, event: &serde_json::Value) {
129 tracing::debug!("Handling Matrix message in room {}: {:?}", room_id, event);
130
131 if let Some(content) = event.get("content") {
132 if let Some(msgtype) = content.get("msgtype").and_then(|m| m.as_str()) {
133 match msgtype {
134 "m.text" | "m.emote" | "m.notice" => {
135 if let Some(body) = content.get("body").and_then(|b| b.as_str()) {
136 tracing::info!("Matrix text message: {}", body);
137 }
138 }
139 "m.image" => {
140 if let Some(url) = content.get("url").and_then(|u| u.as_str()) {
141 tracing::info!("Matrix image: {}", url);
142 }
143 }
144 "m.file" => {
145 if let Some(url) = content.get("url").and_then(|u| u.as_str()) {
146 tracing::info!("Matrix file: {}", url);
147 }
148 }
149 "m.video" => {
150 if let Some(url) = content.get("url").and_then(|u| u.as_str()) {
151 tracing::info!("Matrix video: {}", url);
152 }
153 }
154 "m.audio" => {
155 if let Some(url) = content.get("url").and_then(|u| u.as_str()) {
156 tracing::info!("Matrix audio: {}", url);
157 }
158 }
159 _ => {}
160 }
161 }
162 }
163 }
164
165 async fn handle_room_redaction(&self, room_id: &str, event: &serde_json::Value) {
166 tracing::debug!("Handling Matrix redaction in room {}: {:?}", room_id, event);
167 }
168
169 async fn handle_room_member(&self, room_id: &str, event: &serde_json::Value) {
170 tracing::debug!("Handling Matrix member event in room {}: {:?}", room_id, event);
171 }
172}