add private message publishing rpc

This commit is contained in:
glyph 2022-03-10 09:47:46 +02:00
parent e8294241ec
commit d7ef6a62e0
4 changed files with 71 additions and 2 deletions

1
Cargo.lock generated
View File

@ -640,7 +640,6 @@ dependencies = [
[[package]]
name = "kuska-ssb"
version = "0.4.0"
source = "git+https://github.com/Kuska-ssb/ssb#87e3ea58ba00cc9dfe21e0122231768d90f89000"
dependencies = [
"async-std",
"async-stream 0.2.1",

View File

@ -20,7 +20,8 @@ hex = "0.4.3"
kuska-handshake = { version = "0.2.0", features = ["async_std"] }
kuska-sodiumoxide = "0.2.5-0"
#kuska-ssb = "0.4.0"
kuska-ssb = { git = "https://github.com/Kuska-ssb/ssb" }
#kuska-ssb = { git = "https://github.com/Kuska-ssb/ssb" }
kuska-ssb = { path = "../ssb" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha2 = "0.10.2"

View File

@ -4,6 +4,7 @@ pub mod friends;
pub mod get_subset;
pub mod history_stream;
pub mod invite;
pub mod private;
pub mod publish;
pub mod whoami;

68
src/api/private.rs Normal file
View File

@ -0,0 +1,68 @@
//! Publish and read Scuttlebutt private messages.
//!
//! Implements the following methods:
//!
//! - [`Sbot::publish_private`]
//! - [`Sbot::read_private`]
use crate::{error::GolgiError, messages::SsbMessageContent, sbot::Sbot, utils};
impl Sbot {
/// Publish a private message.
///
/// # Arguments
///
/// * `msg` - A `PrivateMessage` `struct` whose fields include `text` and
/// `recipients`.
///
/// # Example
///
/// ```rust
/// use golgi::{Sbot, GolgiError, messages::SsbMessageContent};
///
/// async fn publish_a_private_msg() -> Result<(), GolgiError> {
/// let mut sbot_client = Sbot::init(None, None).await?;
///
/// let text = String::new("Hi friends, I have a super secrect message to share with you about the wonders of intra-cellular transport mechanics.");
///
/// // We must also include the local identity (public key) here if we wish
/// // to be able to read the message. Ie. the sender must be included in
/// // the list of recipients.
/// let recipients = Vec::new(
/// String::new("@OKRij/n7Uu42A0Z75ty0JI0cZxcieD2NyjXrRdYKNOQ=.ed25519"),
/// String::new("@Sih4JGgs5oQPXehRyHS5qrYbx/0hQVUqChojX0LNtcQ=.ed25519"),
/// String::new("@BVA85B7a/a17v2ZVcLkMgPE+v7X5rQVAHEgQBbCaKMs=.ed25519"),
/// );
///
/// let msg_ref = sbot_client.publish_private(text, recipients).await?;
///
/// println!("msg reference for the private msg: {}", msg_ref);
///
/// Ok(())
/// }
/// ```
//pub async fn publish_private(&mut self, msg: PrivateMessage) -> Result<String, GolgiError> {
pub async fn publish_private(
&mut self,
text: String,
recipients: Vec<String>,
) -> Result<String, GolgiError> {
let msg = SsbMessageContent::Post {
text: text.to_string(),
mentions: None,
};
let mut sbot_connection = self.get_sbot_connection().await?;
let req_id = sbot_connection
.client
.private_publish_req_send(msg, recipients)
.await?;
utils::get_async(
&mut sbot_connection.rpc_reader,
req_id,
utils::string_res_parse,
)
.await
}
}