public-appservice 0.2.0

An appservice to make Matrix spaces publicly accessible.
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::config::Config;

use ruma::{
    OwnedRoomId,
    OwnedEventId,
    OwnedUserId,
    OwnedTransactionId,
    UserId,
    RoomAliasId,
    api::client::{
        appservice::request_ping,
        alias::get_alias,
        account::whoami, 
        state::{
            get_state_events, 
            get_state_events_for_key
        },
        room::get_room_event,
        membership::{
            join_room_by_id, 
            joined_rooms,
            leave_room
        },
        profile::get_profile,
        space::{get_hierarchy, SpaceHierarchyRoomsChunk}
    },
    events::{
        AnyTimelineEvent,
        AnyStateEvent, 
        StateEventType,
        room::{
            name::RoomNameEventContent,
            canonical_alias::RoomCanonicalAliasEventContent,
            avatar::RoomAvatarEventContent,
            topic::RoomTopicEventContent,
        }
    }
};

use anyhow;

use serde::{Serialize, Deserialize};

pub type HttpClient = ruma::client::http_client::HyperNativeTls;

#[derive(Clone)]
pub struct AppService {
    client: ruma::Client<HttpClient>,
    config: Config,
    pub appservice_id: String,
    pub user_id: Box<OwnedUserId>,
}

pub type RoomState = Vec<ruma::serde::Raw<AnyStateEvent>>;

#[derive(Clone)]
pub struct JoinedRoomState {
    pub room_id: OwnedRoomId,
    pub state: Option<RoomState>,
}

impl AppService {
    pub async fn new(config: &Config) -> Result<Self, anyhow::Error> {

        let client = ruma::Client::builder()
            .homeserver_url(config.matrix.homeserver.clone())
            .access_token(Some(config.appservice.access_token.clone()))
            .build::<HttpClient>()
            .await?;

        let user_id = UserId::parse(format!("@{}:{}", config.appservice.sender_localpart, config.matrix.server_name))?;

        let whoami = client
            .send_request(whoami::v3::Request::new())
            .await;

        if whoami.is_err() {
            tracing::info!("Failed to authenticate with homeserver. Check your access token.");
            std::process::exit(1);
        }

        Ok(Self { 
            client, 
            config: config.clone(),
            appservice_id: config.appservice.id.clone(),
            user_id: Box::new(user_id)
        })
    }

    pub async fn health_check(&self) -> Result<(), anyhow::Error> {
        // Perform a simple request to check if the appservice is healthy
        let response = self.client
            .send_request(whoami::v3::Request::new())
            .await?;

        if response.user_id == *self.user_id {
            Ok(())
        } else {
            Err(anyhow::anyhow!("Health check failed: User ID mismatch"))
        }
    }

    pub async fn ping_homeserver(&self, id: String) -> Result<request_ping::v1::Response, anyhow::Error> {

        let mut req = request_ping::v1::Request::new(
            self.appservice_id.to_string()
        );

        req.transaction_id = Some(OwnedTransactionId::from(id));

        let response = self.client
            .send_request(req)
            .await?;
        Ok(response)
    }

    pub fn user_id(&self) -> String {
        self.user_id.to_string()
    }

    pub async fn whoami(&self) -> Result<whoami::v3::Response, anyhow::Error> {
        let r = self.client
            .send_request(whoami::v3::Request::new())
            .await?;
        Ok(r)
    }

    pub async fn join_room(&self, room_id: OwnedRoomId) -> Result<(), anyhow::Error>{

        let jr = self.client
            .send_request(join_room_by_id::v3::Request::new(
                room_id
            ))
            .await?;

        tracing::info!("Joined room: {:#?}", jr);
        Ok(())
    }

    pub async fn has_joined_room(&self, room_id: OwnedRoomId) -> Result<bool, anyhow::Error> {

        let jr = self.client
            .send_request(get_state_events_for_key::v3::Request::new(
                room_id,
                StateEventType::RoomMember,
                self.user_id()
            ))
            .await?;

        let membership = jr.content.get_field::<String>("membership")?;

        Ok(membership == Some("join".to_string()))
    }

    pub async fn get_room_state(&self, room_id: OwnedRoomId) ->
    Result<RoomState, anyhow::Error> {

        let state = self.client
            .send_request(get_state_events::v3::Request::new(
                room_id,
            ))
            .await?;

        Ok(state.room_state)
    }

    pub async fn leave_room(&self, room_id: OwnedRoomId) -> Result<(), anyhow::Error>{
        // First leave all child rooms
        let hierarchy = self.client
            .send_request(get_hierarchy::v1::Request::new(
                room_id.clone()
            ))
            .await?;

        tracing::info!("Hierarchy rooms: {:#?}", hierarchy.rooms.len());

        for room in hierarchy.rooms {
            if room.room_id == room_id {
                continue;
            }
            let left = self.client
                .send_request(leave_room::v3::Request::new(
                    room.room_id.clone()
                ))
                .await?;
            tracing::info!("Left child room: {:#?}", room.room_id);
            tracing::info!("Left child room: {:#?}", left);
        }

        let left = self.client
            .send_request(leave_room::v3::Request::new(
                room_id
            ))
            .await?;

        tracing::info!("Left room: {:#?}", left);

        Ok(())
    }

    pub async fn joined_rooms(&self) -> Result<Vec<ruma::OwnedRoomId>, anyhow::Error> {
        let jr = self.client
            .send_request(joined_rooms::v3::Request::new())
            .await?;

        Ok(jr.joined_rooms)
    }

    pub async fn room_id_from_alias(&self, room_alias: ruma::OwnedRoomAliasId) -> Result<ruma::OwnedRoomId, anyhow::Error> {

        let room_id = self.client
            .send_request(get_alias::v3::Request::new(
                room_alias,
            ))
            .await?;

        Ok(room_id.room_id)
    }

    pub async fn joined_rooms_state(&self) -> Result<Option<Vec<JoinedRoomState>>, anyhow::Error> {

        let curated = self.config.public_rooms.curated;
        let include_rooms = &self.config.public_rooms.include_rooms;

        if curated && !include_rooms.is_empty() {
            // Get subset of joined rooms from config
            let mut joined_rooms: Vec<JoinedRoomState> = Vec::new();

            // first get top level spaces
            for local_part in include_rooms {

                let alias = format!("#{}:{}", local_part, self.config.matrix.server_name);

                let alias = RoomAliasId::parse(&alias)?;

                let room_id = self.room_id_from_alias(alias).await?;

                let mut jrs = JoinedRoomState {
                    room_id: room_id.clone(),
                    state: None,
                };

                let st = self.client
                    .send_request(get_state_events::v3::Request::new(
                        room_id.clone(),
                    ))
                    .await?;

                jrs.state = Some(st.room_state);

                joined_rooms.push(jrs);


                // find child rooms and add to list
                let hierarchy = self.client
                    .send_request(get_hierarchy::v1::Request::new(
                        room_id
                    ))
                    .await?;

                for room in hierarchy.rooms {
                    let mut jrs = JoinedRoomState {
                        room_id: room.room_id.clone(),
                        state: None,
                    };
                    let st = self.client
                        .send_request(get_state_events::v3::Request::new(
                            room.room_id.clone(),
                        ))
                        .await?;

                    jrs.state = Some(st.room_state);

                    let exists = joined_rooms.iter().any(|r| r.room_id == room.room_id);
                    if !exists {
                        joined_rooms.push(jrs);
                    }
                }
            }

            return Ok(Some(joined_rooms));
        }


        let mut joined_rooms: Vec<JoinedRoomState> = Vec::new();

        let jr = self.client
            .send_request(joined_rooms::v3::Request::new())
            .await?;

        if jr.joined_rooms.is_empty() {
            return Ok(None);
        }

        for room_id in jr.joined_rooms {

            let mut jrs = JoinedRoomState {
                room_id: room_id.clone(),
                state: None,
            };


            let st = self.client
                .send_request(get_state_events::v3::Request::new(
                    room_id,
                ))
                .await?;

            jrs.state = Some(st.room_state);

            joined_rooms.push(jrs);

        }

        Ok(Some(joined_rooms))
    }

    pub async fn get_room_event(&self, room_id: OwnedRoomId, event_id: OwnedEventId) -> Result<ruma::serde::Raw<AnyTimelineEvent>, anyhow::Error> {

        let event = self.client
            .send_request(get_room_event::v3::Request::new(
                room_id,
                event_id,
            ))
            .await?;

        Ok(event.event)
    }

    pub async fn get_profile(&self, user_id: String) -> Result<get_profile::v3::Response, anyhow::Error> {

        let parsed_id = ruma::OwnedUserId::try_from(user_id.clone())?;

        let profile = self.client
            .send_request(get_profile::v3::Request::new(
                parsed_id,
            ))
            .await?;

        Ok(profile)
    }

    pub async fn get_room_summary(&self, room_id: OwnedRoomId) -> Result<RoomSummary, anyhow::Error> {

        // find out if appservice has joined the room or not
        let has_joined = self.has_joined_room(room_id.clone()).await?;

        if !has_joined {
            // If not joined, we cannot get the state
            return Err(anyhow::anyhow!("Appservice has not joined the room: {}", room_id));
        }

        let mut room_info = RoomSummary {
            room_id: room_id.to_string(),
            ..Default::default()
        };

        let state = self.client
            .send_request(get_state_events::v3::Request::new(room_id))
            .await?;

        for state_event in state.room_state {
            let event_type = match state_event.get_field::<String>("type") {
                Ok(Some(t)) => t,
                _ => continue, 
            };

            match event_type.as_str() {
                "m.room.name" => {
                    if let Ok(Some(content)) = state_event.get_field::<RoomNameEventContent>("content") {
                        room_info.name = if content.name.is_empty() { 
                            None 
                        } else { 
                            Some(content.name.to_string()) 
                        };
                    }
                }
                "m.room.canonical_alias" => {
                    if let Ok(Some(content)) = state_event.get_field::<RoomCanonicalAliasEventContent>("content") {
                        room_info.canonical_alias = content.alias.map(|a| a.to_string());
                    }
                }
                "m.room.avatar" => {
                    if let Ok(Some(content)) = state_event.get_field::<RoomAvatarEventContent>("content") {
                        room_info.avatar_url = match content.url {
                            Some(url) => {
                                if url.to_string().is_empty() {
                                    None
                                } else {
                                    Some(url.to_string())
                                }
                            }
                            None => None,
                        };
                    }
                }
                "commune.room.banner" => {
                    if let Ok(Some(content)) = state_event.get_field::<RoomAvatarEventContent>("content") {
                        room_info.banner_url = match content.url {
                            Some(url) => Some(url.to_string()),
                            None => None,
                        };
                    }
                }
                "m.room.topic" => {
                    if let Ok(Some(content)) = state_event.get_field::<RoomTopicEventContent>("content") {
                        room_info.topic = if content.topic.is_empty() { 
                            None 
                        } else { 
                            Some(content.topic.to_string()) 
                        };
                    }
                }
                _ => {} 
            }
        }

        Ok(room_info)
    }

    pub async fn get_room_hierarchy(&self, room_id: OwnedRoomId) -> Result<Vec<SpaceHierarchyRoomsChunk>, anyhow::Error> {

        let hierarchy = self.client
            .send_request(get_hierarchy::v1::Request::new(
                room_id
            ))
            .await?;

        Ok(hierarchy.rooms)
    }

    pub async fn get_public_spaces(&self) -> Result<Option<Vec<RoomSummary>>, anyhow::Error> {

        let mut spaces = Vec::new();

        let default_spaces = self.config.spaces.default.clone();

        if default_spaces.is_empty() {
            return Ok(None);
        }

        for space in default_spaces {

            let server_name = self.config.matrix.server_name.clone();

            let raw_alias = format!("#{}:{}", space, server_name);

            let alias = match RoomAliasId::parse(&raw_alias) {
                Ok(alias) => alias,
                Err(_) => continue,
            };

            let room_id = match self.room_id_from_alias(alias).await {
                Ok(room_id) => room_id,
                Err(_) => continue,
            };

            let summary = match self.get_room_summary(room_id).await {
                Ok(summary) => summary,
                Err(_) => continue,
            };

            spaces.push(summary);
        }

        Ok(Some(spaces))

    }

}

#[derive(Default, Clone, Debug, Deserialize, Serialize)]
pub struct RoomSummary {
    pub room_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub canonical_alias: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub banner_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub topic: Option<String>,
}