deso_sdk/
post_lib.rs

1use serde::Deserialize;
2use serde::Serialize;
3use std::collections::HashMap;
4
5use crate::errors;
6
7#[derive(Serialize, Deserialize, Debug)]
8pub struct PostEntryResponse {
9    #[serde(rename = "PostHashHex")]
10    pub post_hash_hex: String, // Hex of the Post Hash. Used as the unique identifier of this post.
11    #[serde(rename = "PosterPublicKeyBase58Check")]
12    pub poster_public_key: String,
13    #[serde(rename = "Body")]
14    pub body: String,
15    #[serde(rename = "ImageURLs")]
16    pub image_urls: Option<Vec<String>>,
17    #[serde(rename = "HasUnlockable")]
18    pub has_unlockable: bool,
19    #[serde(rename = "PostExtraData")]
20    pub extra_data: HashMap<String, String>,
21    #[serde(rename = "NumNFTCopies")]
22    #[serde(default)]
23    pub copies_minted: u64,
24    #[serde(rename = "TimestampNanos")]
25    pub timestamp: u128,
26}
27
28#[derive(Serialize, Deserialize, Debug)]
29pub struct SinglePostResponse {
30    #[serde(rename = "PostFound")]
31    pub post: PostEntryResponse,
32}
33
34#[derive(Serialize, Deserialize, Debug)]
35pub struct SubmittedTransaction {
36    #[serde(rename = "PostEntryResponse")]
37    pub post_entry_response: PostEntryResponse,
38}
39
40/// The main data to post a new post on Deso
41#[derive(Serialize, Deserialize, Debug)]
42pub struct SubmitPostData {
43    /// Public key of the user making a new post, editing a post, or making a comment
44    #[serde(rename = "UpdaterPublicKeyBase58Check")]
45    pub public_key: String,
46
47    /// Only used if making a comment. The post hash hex of the post your commenting on.
48    #[serde(rename = "ParentStakeID")]
49    pub parent_post_hash_hex: Option<String>,
50
51    /// The body of your post
52    #[serde(rename = "BodyObj")]
53    pub body_obj: SubmitPostBodyObject,
54
55    /// Min fee rate nanos per kb, defaults to 1250
56    #[serde(rename = "MinFeeRateNanosPerKB")]
57    pub fee_rate: u64,
58
59    /// Used to "delete" a post. Defaults to false.
60    #[serde(rename = "IsHidden")]
61    pub is_hidden: bool,
62
63    /// An optional map of any meta data for the post
64    #[serde(rename = "PostExtraData")]
65    pub extra_data: Option<HashMap<String, String>>,
66}
67
68/// Builder for building a submit post data
69#[derive(Serialize, Deserialize, Debug)]
70pub struct SubmitPostDataBuilder {
71    /// Public key of the user making a new post, editing a post, or making a comment
72    pub public_key: Option<String>,
73
74    /// Only used if making a comment. The post hash hex of the post your commenting on.
75    pub parent_post_hash_hex: Option<String>,
76
77    /// The body of your post
78    pub body: Option<String>,
79
80    /// Any and all images for the post
81    pub image_urls: Option<Vec<String>>,
82
83    /// Any and all videos for the post
84    pub video_urls: Option<Vec<String>>,
85
86    /// Min fee rate nanos per kb, defaults to 1250
87    pub fee_rate: Option<u64>,
88
89    /// Used to "delete" a post, defaults to false
90    pub is_hidden: Option<bool>,
91
92    /// An optional map of any meta data for the post
93    pub extra_data: Option<HashMap<String, String>>,
94}
95
96#[allow(dead_code)]
97impl SubmitPostDataBuilder {
98    pub fn new() -> Self {
99        SubmitPostDataBuilder {
100            public_key: None,
101            parent_post_hash_hex: None,
102            body: None,
103            image_urls: None,
104            video_urls: None,
105            fee_rate: Some(1250),
106            is_hidden: Some(false),
107            extra_data: None,
108        }
109    }
110    /// Public key of the user making a new post, editing a post, or making a comment
111    pub fn public_key(mut self, public_key: String) -> Self {
112        self.public_key = Some(public_key);
113        self
114    }
115    /// Only used if making a comment. The post hash hex of the post your commenting on.
116    pub fn parent_post_hash_hex(mut self, post_hash_hex: String) -> Self {
117        self.parent_post_hash_hex = Some(post_hash_hex);
118        self
119    }
120    /// The body of your post
121    pub fn body(mut self, body: String) -> Self {
122        self.body = Some(body);
123        self
124    }
125    /// Any and all images for the post
126    pub fn image_urls(mut self, image_urls: Vec<String>) -> Self {
127        self.image_urls = Some(image_urls);
128        self
129    }
130    /// Any and all videos for the post
131    pub fn video_urls(mut self, video_urls: Vec<String>) -> Self {
132        self.video_urls = Some(video_urls);
133        self
134    }
135    /// Min fee rate nanos per kb, defaults to 1250
136    pub fn fee_rate(mut self, fee_rate: u64) -> Self {
137        self.fee_rate = Some(fee_rate);
138        self
139    }
140    /// Used to "delete" a post, defaults to false
141    pub fn is_hidden(mut self, is_hidden: bool) -> Self {
142        self.is_hidden = Some(is_hidden);
143        self
144    }
145    /// An optional map of any meta data for the post
146    pub fn extra_data(mut self, extra_data: HashMap<String, String>) -> Self {
147        self.extra_data = Some(extra_data);
148        self
149    }
150    /// Builds the SubmitPostData
151    pub fn build(self) -> Result<SubmitPostData, errors::DesoError> {
152        if self.body.is_none() {
153            return Err(errors::DesoError::SubmitPostDataBuilderError(String::from(
154                "Body",
155            )));
156        }
157        if self.public_key.is_none() {
158            return Err(errors::DesoError::SubmitPostDataBuilderError(String::from(
159                "Poster Public Key",
160            )));
161        }
162        let body_object = SubmitPostBodyObject {
163            body: self.body.unwrap(),
164            image_urls: self.image_urls,
165            video_urls: self.video_urls,
166        };
167        Ok(SubmitPostData {
168            public_key: self.public_key.unwrap(),
169            parent_post_hash_hex: self.parent_post_hash_hex,
170            body_obj: body_object,
171            fee_rate: self.fee_rate.unwrap(),
172            is_hidden: self.is_hidden.unwrap(),
173            extra_data: self.extra_data,
174        })
175    }
176}
177
178#[derive(Serialize, Deserialize, Debug)]
179pub struct GetSinglePost {
180    #[serde(rename = "PostHashHex")]
181    pub post_hash_hex: String,
182
183    #[serde(rename = "CommentLimit")]
184    pub comment_limit: u32,
185}
186
187///Body of a Deso post, includes the string content and any images(optional) or videos(optional)
188#[derive(Serialize, Deserialize, Debug)]
189pub struct SubmitPostBodyObject {
190    #[serde(rename = "Body")]
191    pub body: String,
192
193    #[serde(rename = "ImageURLs")]
194    pub image_urls: Option<Vec<String>>,
195
196    #[serde(rename = "VideoURLs")]
197    pub video_urls: Option<Vec<String>>,
198}
199
200// let body = post_lib::SubmitPostBodyObject {
201//     body: String::from("Testing the new deso rust library by @Spatium!"),
202//     image_urls: None,
203//     video_urls: None,
204// };
205
206// let mut extra_data_map: HashMap<String, String> = HashMap::new();
207// extra_data_map.insert(String::from("nft_type"), String::from("AUTHOR"));
208
209// let post_data = post_lib::SubmitPostData {
210//     public_key: deso_account.public_key.clone(),
211//     parent_post_hash_hex: None,
212//     body_obj: body,
213//     fee_rate: 1250,
214//     is_hidden: false,
215//     extra_data: Some(extra_data_map),
216// };