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
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! Publish Scuttlebutt messages.
//!
//! Implements the following methods:
//!
//! - [`Sbot::publish`]
//! - [`Sbot::publish_description`]
//! - [`Sbot::publish_name`]
//! - [`Sbot::publish_post`]

use crate::{error::GolgiError, messages::SsbMessageContent, sbot::Sbot, utils};

impl Sbot {
    /// Publish a message.
    ///
    /// # Arguments
    ///
    /// * `msg` - A `SsbMessageContent` `enum` whose variants include `Pub`,
    /// `Post`, `Contact`, `About`, `Channel` and `Vote`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError, messages::SsbMessageContent};
    ///
    /// async fn publish_a_msg() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     // Construct an SSB message of type `post`.
    ///     let post = SsbMessageContent::Post {
    ///         text: "And then those vesicles, filled with the Golgi products, move to the rest of the cell".to_string(),
    ///         mentions: None,
    ///     };
    ///
    ///     let msg_ref = sbot_client.publish(post).await?;
    ///
    ///     println!("msg reference for the golgi post: {}", msg_ref);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn publish(&mut self, msg: SsbMessageContent) -> Result<String, GolgiError> {
        let mut sbot_connection = self.get_sbot_connection().await?;
        let req_id = sbot_connection.client.publish_req_send(msg).await?;

        utils::get_async(
            &mut sbot_connection.rpc_reader,
            req_id,
            utils::string_res_parse,
        )
        .await
    }

    /// Publish a post.
    ///
    /// Convenient wrapper around the `publish` method which constructs and
    /// publishes a `post` type message appropriately from a string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError};
    ///
    /// async fn publish_a_post() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let text = "The Golgi is located right near the nucleus.";
    ///
    ///     let msg_ref = sbot_client.publish_post(text).await?;
    ///
    ///     println!("msg reference for the golgi post: {}", msg_ref);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn publish_post(&mut self, text: &str) -> Result<String, GolgiError> {
        let msg = SsbMessageContent::Post {
            text: text.to_string(),
            mentions: None,
        };
        self.publish(msg).await
    }

    /// Publish a description for the local identity.
    ///
    /// Convenient wrapper around the `publish` method which constructs and
    /// publishes an `about` type description message appropriately from a string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError};
    ///
    /// async fn publish_a_description() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let description = "The Golgi apparatus was identified by the Italian scientist Camillo Golgi in 1897.";
    ///
    ///     let msg_ref = sbot_client.publish_description(description).await?;
    ///
    ///     println!("msg reference for the golgi description: {}", msg_ref);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn publish_description(&mut self, description: &str) -> Result<String, GolgiError> {
        let msg = SsbMessageContent::About {
            about: self.id.to_string(),
            name: None,
            title: None,
            branch: None,
            image: None,
            description: Some(description.to_string()),
            location: None,
            start_datetime: None,
        };
        self.publish(msg).await
    }

    /// Publish a name for the local identity.
    ///
    /// Convenient wrapper around the `publish` method which constructs and
    /// publishes an `about` type name message appropriately from a string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError};
    ///
    /// async fn publish_a_name() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let name = "glyphski_golgionikus";
    ///
    ///     let msg_ref = sbot_client.publish_name(name).await?;
    ///
    ///     println!("msg reference: {}", msg_ref);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn publish_name(&mut self, name: &str) -> Result<String, GolgiError> {
        let msg = SsbMessageContent::About {
            about: self.id.to_string(),
            name: Some(name.to_string()),
            title: None,
            branch: None,
            image: None,
            description: None,
            location: None,
            start_datetime: None,
        };
        self.publish(msg).await
    }
}