Merge pull request 'Add private message publishing' (#36) from private_msgs into main

Reviewed-on: #36
This commit is contained in:
glyph 2022-03-13 09:06:21 +00:00
commit 77dd75bcd4
4 changed files with 68 additions and 2 deletions

2
Cargo.lock generated
View File

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

View File

@ -19,7 +19,6 @@ futures = "0.3.21"
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" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"

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;

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

@ -0,0 +1,66 @@
//! Publish Scuttlebutt private messages.
//!
//! Implements the following method:
//!
//! - [`Sbot::publish_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,
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
}
}