iroh_docs/rpc/client/
authors.rs

1//! API for document management.
2//!
3//! The main entry point is the [`Client`].
4
5use anyhow::Result;
6use futures_lite::{Stream, StreamExt};
7use quic_rpc::{client::BoxedConnector, Connector};
8
9use super::flatten;
10#[doc(inline)]
11pub use crate::engine::{Origin, SyncEvent, SyncReason};
12use crate::{
13    rpc::proto::{
14        AuthorCreateRequest, AuthorDeleteRequest, AuthorExportRequest, AuthorGetDefaultRequest,
15        AuthorImportRequest, AuthorListRequest, AuthorSetDefaultRequest, RpcService,
16    },
17    Author, AuthorId,
18};
19
20/// Iroh docs client.
21#[derive(Debug, Clone)]
22#[repr(transparent)]
23pub struct Client<C = BoxedConnector<RpcService>> {
24    pub(super) rpc: quic_rpc::RpcClient<RpcService, C>,
25}
26
27impl<C: Connector<RpcService>> Client<C> {
28    /// Creates a new docs client.
29    pub fn new(rpc: quic_rpc::RpcClient<RpcService, C>) -> Self {
30        Self { rpc }
31    }
32
33    /// Creates a new document author.
34    ///
35    /// You likely want to save the returned [`AuthorId`] somewhere so that you can use this author
36    /// again.
37    ///
38    /// If you need only a single author, use [`Self::default`].
39    pub async fn create(&self) -> Result<AuthorId> {
40        let res = self.rpc.rpc(AuthorCreateRequest).await??;
41        Ok(res.author_id)
42    }
43
44    /// Returns the default document author of this node.
45    ///
46    /// On persistent nodes, the author is created on first start and its public key is saved
47    /// in the data directory.
48    ///
49    /// The default author can be set with [`Self::set_default`].
50    pub async fn default(&self) -> Result<AuthorId> {
51        let res = self.rpc.rpc(AuthorGetDefaultRequest).await??;
52        Ok(res.author_id)
53    }
54
55    /// Sets the node-wide default author.
56    ///
57    /// If the author does not exist, an error is returned.
58    ///
59    /// On a persistent node, the author id will be saved to a file in the data directory and
60    /// reloaded after a restart.
61    pub async fn set_default(&self, author_id: AuthorId) -> Result<()> {
62        self.rpc
63            .rpc(AuthorSetDefaultRequest { author_id })
64            .await??;
65        Ok(())
66    }
67
68    /// Lists document authors for which we have a secret key.
69    ///
70    /// It's only possible to create writes from authors that we have the secret key of.
71    pub async fn list(&self) -> Result<impl Stream<Item = Result<AuthorId>>> {
72        let stream = self.rpc.server_streaming(AuthorListRequest {}).await?;
73        Ok(flatten(stream).map(|res| res.map(|res| res.author_id)))
74    }
75
76    /// Exports the given author.
77    ///
78    /// Warning: The [`Author`] struct contains sensitive data.
79    pub async fn export(&self, author: AuthorId) -> Result<Option<Author>> {
80        let res = self.rpc.rpc(AuthorExportRequest { author }).await??;
81        Ok(res.author)
82    }
83
84    /// Imports the given author.
85    ///
86    /// Warning: The [`Author`] struct contains sensitive data.
87    pub async fn import(&self, author: Author) -> Result<()> {
88        self.rpc.rpc(AuthorImportRequest { author }).await??;
89        Ok(())
90    }
91
92    /// Deletes the given author by id.
93    ///
94    /// Warning: This permanently removes this author.
95    ///
96    /// Returns an error if attempting to delete the default author.
97    pub async fn delete(&self, author: AuthorId) -> Result<()> {
98        self.rpc.rpc(AuthorDeleteRequest { author }).await??;
99        Ok(())
100    }
101}