public-appservice 0.2.2

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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use crate::config::Config;
use futures::future::join_all;
use std::collections::HashSet;
use std::sync::Arc;
use tokio::sync::Semaphore;

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

use anyhow;

use serde::{Deserialize, Serialize};

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

use std::sync::Mutex;

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

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.");
            return Err(anyhow::anyhow!(
                "Failed to authenticate with homeserver. Check your appservice access token."
            ));
        }

        let joined_rooms = match client.send_request(joined_rooms::v3::Request::new()).await {
            Ok(r) => r.joined_rooms,
            Err(_) => vec![],
        };

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

    pub fn add_to_joined_rooms(&self, room_id: OwnedRoomId) -> Result<(), anyhow::Error> {
        let mut rooms = self
            .joined_rooms
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to acquire lock on joined_rooms"))?;

        if !rooms.contains(&room_id) {
            rooms.push(room_id.clone());
        }
        tracing::info!(
            "Added room {} to joined rooms. Current count: {}",
            room_id,
            rooms.len()
        );
        Ok(())
    }

    pub fn remove_from_joined_rooms(&self, room_id: &OwnedRoomId) -> Result<(), anyhow::Error> {
        let mut rooms = self
            .joined_rooms
            .lock()
            .map_err(|_| anyhow::anyhow!("Failed to acquire lock on joined_rooms"))?;

        if let Some(pos) = rooms.iter().position(|x| x == room_id) {
            rooms.remove(pos);
        }
        tracing::info!(
            "Removed room {} from joined rooms. Current count: {}",
            room_id,
            rooms.len()
        );
        Ok(())
    }

    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<bool, anyhow::Error> {
        let jr = self
            .client
            .send_request(join_room_by_id::v3::Request::new(room_id.clone()))
            .await?;

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

        Ok(jr.room_id == room_id)
    }

    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 is_space(&self, room_id: OwnedRoomId) -> Result<bool, anyhow::Error> {
        let hierarchy = self
            .client
            .send_request(get_hierarchy::v1::Request::new(room_id.clone()))
            .await?;

        // hierarchy contains the room itself and all child rooms
        // so we check if there is more than one room in the hierarchy
        Ok(hierarchy.rooms.len() > 1)
    }

    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<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 semaphore = Arc::new(Semaphore::new(10));
        let curated = self.config.public_rooms.curated;
        let include_rooms = &self.config.public_rooms.include_rooms;

        if curated && !include_rooms.is_empty() {
            let mut all_room_ids = Vec::new();

            let space_futures: Vec<_> = include_rooms
                .iter()
                .map(|local_part| {
                    let sem = semaphore.clone();
                    let server_name = self.config.matrix.server_name.clone();
                    let self_ref = self;
                    async move {
                        let _permit = sem.acquire().await.ok()?;

                        let alias = format!("#{local_part}:{server_name}");
                        let alias = RoomAliasId::parse(&alias).ok()?;
                        let room_id = self_ref.room_id_from_alias(alias).await.ok()?;

                        // Get hierarchy for this space
                        let hierarchy = self_ref
                            .client
                            .send_request(get_hierarchy::v1::Request::new(room_id.clone()))
                            .await
                            .ok()?;

                        let mut room_ids = vec![room_id];
                        room_ids.extend(hierarchy.rooms.into_iter().map(|room| room.room_id));

                        Some(room_ids)
                    }
                })
                .collect();

            let hierarchy_results = join_all(space_futures).await;

            let mut unique_room_ids = HashSet::new();
            for room_ids in hierarchy_results.into_iter().flatten() {
                for room_id in room_ids {
                    unique_room_ids.insert(room_id);
                }
            }
            all_room_ids.extend(unique_room_ids);

            let state_futures: Vec<_> = all_room_ids
                .into_iter()
                .map(|room_id| {
                    let sem = semaphore.clone();
                    let self_ref = self;
                    async move {
                        let _permit = sem.acquire().await.ok()?;

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

                        Some(JoinedRoomState {
                            room_id,
                            state: Some(st.room_state),
                        })
                    }
                })
                .collect();

            let state_results = join_all(state_futures).await;
            let joined_rooms: Vec<_> = state_results.into_iter().flatten().collect();

            return Ok(Some(joined_rooms));
        }

        // Handle the non-curated case
        let jr = self
            .client
            .send_request(joined_rooms::v3::Request::new())
            .await?;

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

        let state_futures: Vec<_> = jr
            .joined_rooms
            .into_iter()
            .map(|room_id| {
                let sem = semaphore.clone();
                let self_ref = self;
                async move {
                    let _permit = sem.acquire().await.ok()?;

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

                    Some(JoinedRoomState {
                        room_id,
                        state: Some(st.room_state),
                    })
                }
            })
            .collect();

        let results = join_all(state_futures).await;
        let joined_rooms: Vec<_> = results.into_iter().flatten().collect();

        Ok(Some(joined_rooms))
    }

    pub async fn joined_rooms_state_alt(
        &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: &str,
    ) -> Result<get_profile::v3::Response, anyhow::Error> {
        let parsed_id = ruma::OwnedUserId::try_from(user_id.to_string())?;

        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 = content.url.map(|url| url.to_string());
                    }
                }
                "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 semaphore = Arc::new(Semaphore::new(10));

        if self.config.spaces.include_all {
            let jr = self
                .client
                .send_request(joined_rooms::v3::Request::new())
                .await?;

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

            let space_futures: Vec<_> = jr
                .joined_rooms
                .into_iter()
                .map(|room_id| {
                    let sem = semaphore.clone();
                    let self_ref = self;
                    async move {
                        let _permit = sem.acquire().await.ok()?;

                        let is_space = self_ref.is_space(room_id.clone()).await.ok()?;
                        if !is_space {
                            return None;
                        }

                        let summary = self_ref.get_room_summary(room_id).await.ok()?;
                        Some(summary)
                    }
                })
                .collect();

            let results = join_all(space_futures).await;
            let spaces: Vec<_> = results.into_iter().flatten().collect();

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

            return Ok(Some(spaces));
        }

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

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

        let space_futures: Vec<_> = default_spaces
            .into_iter()
            .map(|space| {
                let sem = semaphore.clone();
                let server_name = self.config.matrix.server_name.clone();
                let self_ref = self;
                async move {
                    let _permit = sem.acquire().await.ok()?;

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

                    let alias = RoomAliasId::parse(&raw_alias).ok()?;

                    let room_id = self_ref.room_id_from_alias(alias).await.ok()?;

                    let summary = self_ref.get_room_summary(room_id).await.ok()?;
                    Some(summary)
                }
            })
            .collect();

        let results = join_all(space_futures).await;
        let spaces: Vec<_> = results.into_iter().flatten().collect();

        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>,
}