mas_matrix/
readonly.rs

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
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.

use std::collections::HashSet;

use crate::{HomeserverConnection, MatrixUser, ProvisionRequest};

/// A wrapper around a [`HomeserverConnection`] that only allows read
/// operations.
pub struct ReadOnlyHomeserverConnection<C> {
    inner: C,
}

impl<C> ReadOnlyHomeserverConnection<C> {
    pub fn new(inner: C) -> Self
    where
        C: HomeserverConnection,
    {
        Self { inner }
    }
}

#[async_trait::async_trait]
impl<C: HomeserverConnection> HomeserverConnection for ReadOnlyHomeserverConnection<C> {
    fn homeserver(&self) -> &str {
        self.inner.homeserver()
    }

    async fn query_user(&self, mxid: &str) -> Result<MatrixUser, anyhow::Error> {
        self.inner.query_user(mxid).await
    }

    async fn provision_user(&self, _request: &ProvisionRequest) -> Result<bool, anyhow::Error> {
        anyhow::bail!("Provisioning is not supported in read-only mode");
    }

    async fn is_localpart_available(&self, localpart: &str) -> Result<bool, anyhow::Error> {
        self.inner.is_localpart_available(localpart).await
    }

    async fn create_device(&self, _mxid: &str, _device_id: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("Device creation is not supported in read-only mode");
    }

    async fn delete_device(&self, _mxid: &str, _device_id: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("Device deletion is not supported in read-only mode");
    }

    async fn sync_devices(
        &self,
        _mxid: &str,
        _devices: HashSet<String>,
    ) -> Result<(), anyhow::Error> {
        anyhow::bail!("Device synchronization is not supported in read-only mode");
    }

    async fn delete_user(&self, _mxid: &str, _erase: bool) -> Result<(), anyhow::Error> {
        anyhow::bail!("User deletion is not supported in read-only mode");
    }

    async fn reactivate_user(&self, _mxid: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("User reactivation is not supported in read-only mode");
    }

    async fn set_displayname(&self, _mxid: &str, _displayname: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("User displayname update is not supported in read-only mode");
    }

    async fn unset_displayname(&self, _mxid: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("User displayname update is not supported in read-only mode");
    }

    async fn allow_cross_signing_reset(&self, _mxid: &str) -> Result<(), anyhow::Error> {
        anyhow::bail!("Allowing cross-signing reset is not supported in read-only mode");
    }
}