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
//! Create and use invite codes.
//!
//! Implements the following methods:
//!
//! - [`Sbot::invite_create`]
//! - [`Sbot::invite_use`]

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

impl Sbot {
    /// Generate an invite code.
    ///
    /// Calls the `invite.create` RPC method and returns the code.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError};
    ///
    /// async fn invite_code_generator() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let invite_code = sbot_client.invite_create(5).await?;
    ///
    ///     println!("this invite code can be used 5 times: {}", invite_code);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn invite_create(&mut self, uses: u16) -> Result<String, GolgiError> {
        let mut sbot_connection = self.get_sbot_connection().await?;
        let req_id = sbot_connection.client.invite_create_req_send(uses).await?;

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

    /// Use an invite code.
    ///
    /// Calls the `invite.use` RPC method and returns a reference to the follow
    /// message.
    ///
    /// # Example
    ///
    /// ```rust
    /// use golgi::{Sbot, GolgiError};
    ///
    /// async fn invite_code_consumer() -> Result<(), GolgiError> {
    ///     let mut sbot_client = Sbot::init(None, None).await?;
    ///
    ///     let invite_code = "127.0.0.1:8008:@0iMa+vP7B2aMrV3dzRxlch/iqZn/UM3S3Oo2oVeILY8=.ed25519~ZHNjeajPB/84NjjsrglZInlh46W55RcNDPcffTPgX/Q=";
    ///     
    ///     match sbot_client.invite_use(invite_code).await {
    ///         Ok(msg_ref) => println!("consumed invite code. msg reference: {}", msg_ref),
    ///         Err(e) => eprintln!("failed to consume the invite code: {}", e),
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn invite_use(&mut self, invite_code: &str) -> Result<String, GolgiError> {
        let mut sbot_connection = self.get_sbot_connection().await?;
        let req_id = sbot_connection
            .client
            .invite_use_req_send(invite_code)
            .await?;

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