|
| 1 | +// Copyright 2025 New Vector Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 4 | +// Please see LICENSE in the repository root for full details. |
| 5 | + |
| 6 | +use std::collections::HashSet; |
| 7 | + |
| 8 | +use crate::{HomeserverConnection, MatrixUser, ProvisionRequest}; |
| 9 | + |
| 10 | +/// A wrapper around a [`HomeserverConnection`] that only allows read |
| 11 | +/// operations. |
| 12 | +pub struct ReadOnlyHomeserverConnection<C> { |
| 13 | + inner: C, |
| 14 | +} |
| 15 | + |
| 16 | +impl<C> ReadOnlyHomeserverConnection<C> { |
| 17 | + pub fn new(inner: C) -> Self |
| 18 | + where |
| 19 | + C: HomeserverConnection, |
| 20 | + { |
| 21 | + Self { inner } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[async_trait::async_trait] |
| 26 | +impl<C: HomeserverConnection> HomeserverConnection for ReadOnlyHomeserverConnection<C> { |
| 27 | + fn homeserver(&self) -> &str { |
| 28 | + self.inner.homeserver() |
| 29 | + } |
| 30 | + |
| 31 | + async fn query_user(&self, mxid: &str) -> Result<MatrixUser, anyhow::Error> { |
| 32 | + self.inner.query_user(mxid).await |
| 33 | + } |
| 34 | + |
| 35 | + async fn provision_user(&self, _request: &ProvisionRequest) -> Result<bool, anyhow::Error> { |
| 36 | + anyhow::bail!("Provisioning is not supported in read-only mode"); |
| 37 | + } |
| 38 | + |
| 39 | + async fn is_localpart_available(&self, localpart: &str) -> Result<bool, anyhow::Error> { |
| 40 | + self.inner.is_localpart_available(localpart).await |
| 41 | + } |
| 42 | + |
| 43 | + async fn create_device(&self, _mxid: &str, _device_id: &str) -> Result<(), anyhow::Error> { |
| 44 | + anyhow::bail!("Device creation is not supported in read-only mode"); |
| 45 | + } |
| 46 | + |
| 47 | + async fn delete_device(&self, _mxid: &str, _device_id: &str) -> Result<(), anyhow::Error> { |
| 48 | + anyhow::bail!("Device deletion is not supported in read-only mode"); |
| 49 | + } |
| 50 | + |
| 51 | + async fn sync_devices( |
| 52 | + &self, |
| 53 | + _mxid: &str, |
| 54 | + _devices: HashSet<String>, |
| 55 | + ) -> Result<(), anyhow::Error> { |
| 56 | + anyhow::bail!("Device synchronization is not supported in read-only mode"); |
| 57 | + } |
| 58 | + |
| 59 | + async fn delete_user(&self, _mxid: &str, _erase: bool) -> Result<(), anyhow::Error> { |
| 60 | + anyhow::bail!("User deletion is not supported in read-only mode"); |
| 61 | + } |
| 62 | + |
| 63 | + async fn reactivate_user(&self, _mxid: &str) -> Result<(), anyhow::Error> { |
| 64 | + anyhow::bail!("User reactivation is not supported in read-only mode"); |
| 65 | + } |
| 66 | + |
| 67 | + async fn set_displayname(&self, _mxid: &str, _displayname: &str) -> Result<(), anyhow::Error> { |
| 68 | + anyhow::bail!("User displayname update is not supported in read-only mode"); |
| 69 | + } |
| 70 | + |
| 71 | + async fn unset_displayname(&self, _mxid: &str) -> Result<(), anyhow::Error> { |
| 72 | + anyhow::bail!("User displayname update is not supported in read-only mode"); |
| 73 | + } |
| 74 | + |
| 75 | + async fn allow_cross_signing_reset(&self, _mxid: &str) -> Result<(), anyhow::Error> { |
| 76 | + anyhow::bail!("Allowing cross-signing reset is not supported in read-only mode"); |
| 77 | + } |
| 78 | +} |
0 commit comments