desmos_bindings/posts/
querier.rs

1//! Contains the querier that can be used to query data related to the x/posts module.
2
3use crate::cosmos_types::PageRequest;
4use crate::posts::types::*;
5use cosmwasm_std::{Addr, Empty, QuerierWrapper, StdResult};
6#[cfg(feature = "iterators")]
7use {
8    crate::iter::page_iterator::{Page, PageIterator},
9    crate::posts::types::{Attachment, Post, UserAnswer},
10    cosmwasm_std::Binary,
11};
12
13/// Querier allows to query data from the Desmos x/posts module.
14pub struct PostsQuerier<'a> {
15    querier: crate::posts::types::PostsQuerier<'a, Empty>,
16}
17
18impl<'a> PostsQuerier<'a> {
19    /// Creates a new instance of [`PostsQuerier`].
20    ///
21    /// # Example
22    /// ```
23    /// use cosmwasm_std::{DepsMut, MessageInfo};
24    /// use desmos_bindings::posts::querier::PostsQuerier;
25    ///
26    /// pub fn contract_action(deps: DepsMut, _: MessageInfo) {
27    ///     let querier = PostsQuerier::new(&deps.querier);
28    /// }
29    /// ```
30    pub fn new(querier: &'a QuerierWrapper<'a, Empty>) -> Self {
31        Self {
32            querier: crate::posts::types::PostsQuerier::new(querier),
33        }
34    }
35
36    /// Queries posts created inside a subspace.
37    ///
38    /// * `subspace_id` - Subspace to query the posts for.
39    /// * `pagination` - Optional pagination configs.
40    pub fn query_subspace_posts(
41        &self,
42        subspace_id: u64,
43        pagination: Option<PageRequest>,
44    ) -> StdResult<QuerySubspacePostsResponse> {
45        self.querier
46            .subspace_posts(subspace_id, pagination.map(Into::into))
47    }
48
49    /// Gives an iterator to scan over the posts created inside a subspace.
50    ///
51    /// * `subspace_id` - Subspace to query the posts for.
52    /// * `page_size` - Size of the page requested to the chain.
53    #[cfg(feature = "iterators")]
54    pub fn iterate_subspace_posts(
55        &self,
56        subspace_id: u64,
57        page_size: u64,
58    ) -> PageIterator<Post, Binary> {
59        PageIterator::new(
60            Box::new(move |key, limit| {
61                self.query_subspace_posts(
62                    subspace_id,
63                    Some(PageRequest {
64                        key: key.unwrap_or_default().to_vec(),
65                        limit: limit.into(),
66                        reverse: false,
67                        count_total: false,
68                        offset: 0,
69                    }),
70                )
71                .map(|response| Page {
72                    items: response.posts,
73                    next_page_key: response.pagination.and_then(|response| {
74                        (!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
75                    }),
76                })
77            }),
78            page_size,
79        )
80    }
81
82    /// Queries all the posts inside a give section.
83    ///
84    /// * `subspace_id` - Subspace to query the posts for.
85    /// * `section_id` - Section to query the post for.
86    /// * `pagination` - Optional pagination configs.
87    pub fn query_section_posts(
88        &self,
89        subspace_id: u64,
90        section_id: u32,
91        pagination: Option<PageRequest>,
92    ) -> StdResult<QuerySectionPostsResponse> {
93        self.querier
94            .section_posts(subspace_id, section_id, pagination.map(Into::into))
95    }
96
97    /// Gives an iterator to scan over the posts created inside a give section.
98    ///
99    /// * `subspace_id` - Subspace to query the posts for.
100    /// * `section_id` - Section to query the post for.
101    /// * `page_size` - Size of the page requested to the chain.
102    #[cfg(feature = "iterators")]
103    pub fn iterate_section_posts(
104        &self,
105        subspace_id: u64,
106        section_id: u32,
107        page_size: u64,
108    ) -> PageIterator<Post, Binary> {
109        PageIterator::new(
110            Box::new(move |key, limit| {
111                self.query_section_posts(
112                    subspace_id,
113                    section_id,
114                    Some(PageRequest {
115                        key: key.unwrap_or_default().to_vec(),
116                        limit: limit.into(),
117                        reverse: false,
118                        count_total: false,
119                        offset: 0,
120                    }),
121                )
122                .map(|response| Page {
123                    items: response.posts,
124                    next_page_key: response.pagination.and_then(|response| {
125                        (!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
126                    }),
127                })
128            }),
129            page_size,
130        )
131    }
132
133    /// Queries a single post inside a given subspace.
134    ///
135    /// * `subspace_id` - Id of the subspace where the post is stored.
136    /// * `post_id` - Id of the post to query for.
137    pub fn query_post(&self, subspace_id: u64, post_id: u64) -> StdResult<QueryPostResponse> {
138        self.querier.post(subspace_id, post_id)
139    }
140
141    /// Queries the attachments of the post having the given `post_id`.
142    ///
143    /// * `subspace_id` - Id of the subspace where the post is stored.
144    /// * `post_id` - Id of the post to query the attachments for.
145    /// * `pagination` - Optional pagination configs.
146    pub fn query_post_attachments(
147        &self,
148        subspace_id: u64,
149        post_id: u64,
150        pagination: Option<PageRequest>,
151    ) -> StdResult<QueryPostAttachmentsResponse> {
152        self.querier
153            .post_attachments(subspace_id, post_id, pagination.map(Into::into))
154    }
155
156    /// Gives an iterator to scan over the attachments of the post having the given `post_id`.
157    ///
158    /// * `subspace_id` - Id of the subspace where the post is stored.
159    /// * `post_id` - Id of the post to query the attachments for.
160    /// * `page_size` - Size of the page requested to the chain.
161    #[cfg(feature = "iterators")]
162    pub fn iterate_post_attachments(
163        &self,
164        subspace_id: u64,
165        post_id: u64,
166        page_size: u64,
167    ) -> PageIterator<Attachment, Binary> {
168        PageIterator::new(
169            Box::new(move |key, limit| {
170                self.query_post_attachments(
171                    subspace_id,
172                    post_id,
173                    Some(PageRequest {
174                        key: key.unwrap_or_default().to_vec(),
175                        limit: limit.into(),
176                        reverse: false,
177                        count_total: false,
178                        offset: 0,
179                    }),
180                )
181                .map(|response| Page {
182                    items: response.attachments,
183                    next_page_key: response.pagination.and_then(|response| {
184                        (!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
185                    }),
186                })
187            }),
188            page_size,
189        )
190    }
191
192    /// Queries the answers for the poll having the given `post_id`.
193    ///
194    /// * `subspace_id` - Id of the subspace where the post is stored.
195    /// * `poll_id` - Id of the post that holds the poll.
196    /// * `user` - Optional address of the user to query the responses for.
197    /// * `pagination` - Optional pagination configs.
198    pub fn query_poll_answers(
199        &self,
200        subspace_id: u64,
201        post_id: u64,
202        poll_id: u32,
203        user: Option<Addr>,
204        pagination: Option<PageRequest>,
205    ) -> StdResult<QueryPollAnswersResponse> {
206        self.querier.poll_answers(
207            subspace_id,
208            post_id,
209            poll_id,
210            user.unwrap_or_else(|| Addr::unchecked("")).into(),
211            pagination.map(Into::into),
212        )
213    }
214
215    /// Gives an iterator to scan over the answers for the poll having the given `post_id`.
216    ///
217    /// * `subspace_id` - Id of the subspace where the post is stored.
218    /// * `poll_id` - Id of the post that holds the poll.
219    /// * `user` - Optional address of the user to query the responses for.
220    /// * `page_size` - Size of the page requested to the chain.
221    #[cfg(feature = "iterators")]
222    pub fn iterate_poll_answers(
223        &self,
224        subspace_id: u64,
225        post_id: u64,
226        poll_id: u32,
227        user: Option<Addr>,
228        page_size: u64,
229    ) -> PageIterator<UserAnswer, Binary> {
230        PageIterator::new(
231            Box::new(move |key, limit| {
232                self.query_poll_answers(
233                    subspace_id,
234                    post_id,
235                    poll_id,
236                    user.clone(),
237                    Some(PageRequest {
238                        key: key.unwrap_or_default().to_vec(),
239                        limit: limit.into(),
240                        reverse: false,
241                        count_total: false,
242                        offset: 0,
243                    }),
244                )
245                .map(|response| Page {
246                    items: response.answers,
247                    next_page_key: response.pagination.and_then(|response| {
248                        (!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
249                    }),
250                })
251            }),
252            page_size,
253        )
254    }
255
256    /// Queries the incoming post transfer requests having the given `subspace_id`.
257    ///
258    /// * `subspace_id` - Id of the subspace where the requests are stored.
259    /// * `receiver` - Optional the address of the user to which query the incoming requests for.
260    /// * `pagination` - Optional pagination for the request.
261    pub fn query_incoming_post_transfer_requests(
262        &self,
263        subspace_id: u64,
264        receiver: Option<Addr>,
265        pagination: Option<PageRequest>,
266    ) -> StdResult<QueryIncomingPostOwnerTransferRequestsResponse> {
267        self.querier.incoming_post_owner_transfer_requests(
268            subspace_id,
269            receiver.unwrap_or_else(|| Addr::unchecked("")).into(),
270            pagination.map(Into::into),
271        )
272    }
273
274    /// Gives an iterator to scan over the incoming post transfer requests having the given `subspace_id`.
275    ///
276    /// * `subspace_id` - Id of the subspace where the requests are stored.
277    /// * `receiver` - Optional the address of the user to which query the incoming requests for.
278    /// * `page_size` - Size of the page requested to the chain.
279    #[cfg(feature = "iterators")]
280    pub fn iterate_incoming_post_transfer_requests(
281        &self,
282        subspace_id: u64,
283        receiver: Option<Addr>,
284        page_size: u64,
285    ) -> PageIterator<PostOwnerTransferRequest, Binary> {
286        PageIterator::new(
287            Box::new(move |key, limit| {
288                self.query_incoming_post_transfer_requests(
289                    subspace_id,
290                    receiver.clone(),
291                    Some(PageRequest {
292                        key: key.unwrap_or_default().to_vec(),
293                        limit: limit.into(),
294                        reverse: false,
295                        count_total: false,
296                        offset: 0,
297                    }),
298                )
299                .map(|response| Page {
300                    items: response.requests,
301                    next_page_key: response.pagination.and_then(|response| {
302                        (!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
303                    }),
304                })
305            }),
306            page_size,
307        )
308    }
309}
310
311#[cfg(test)]
312mod tests {
313    use super::*;
314    use crate::mocks::mock_queriers::mock_desmos_dependencies;
315    use crate::posts::mocks::MockPostsQueries;
316    #[test]
317    fn test_query_subspace_posts() {
318        let owned_deps = mock_desmos_dependencies();
319        let deps = owned_deps.as_ref();
320        let querier = PostsQuerier::new(&deps.querier);
321
322        let result = querier.query_subspace_posts(1, None);
323        let response = result.unwrap();
324        let expected = MockPostsQueries::get_mocked_subspace_posts_response();
325
326        assert_eq!(expected, response)
327    }
328
329    #[test]
330    fn test_iterate_subspace_posts() {
331        let owned_deps = mock_desmos_dependencies();
332        let deps = owned_deps.as_ref();
333        let querier = PostsQuerier::new(&deps.querier);
334
335        let mut iterator = querier.iterate_subspace_posts(1, 32);
336        let expected = MockPostsQueries::get_mocked_subspace_posts_response();
337
338        assert_eq!(expected.posts[0], iterator.next().unwrap().unwrap());
339        assert_eq!(expected.posts[1], iterator.next().unwrap().unwrap());
340        assert!(iterator.next().is_none())
341    }
342
343    #[test]
344    fn test_query_section_posts() {
345        let owned_deps = mock_desmos_dependencies();
346        let deps = owned_deps.as_ref();
347        let querier = PostsQuerier::new(&deps.querier);
348
349        let result = querier.query_section_posts(1, 1, None);
350        let response = result.unwrap();
351        let expected = MockPostsQueries::get_mocked_section_posts_response();
352
353        assert_eq!(expected, response)
354    }
355
356    #[test]
357    fn test_iterate_section_posts() {
358        let owned_deps = mock_desmos_dependencies();
359        let deps = owned_deps.as_ref();
360        let querier = PostsQuerier::new(&deps.querier);
361
362        let mut iterator = querier.iterate_section_posts(1, 1, 32);
363        let expected = MockPostsQueries::get_mocked_section_posts_response();
364
365        assert_eq!(expected.posts[0], iterator.next().unwrap().unwrap());
366        assert_eq!(expected.posts[1], iterator.next().unwrap().unwrap());
367        assert!(iterator.next().is_none())
368    }
369
370    #[test]
371    fn test_query_post() {
372        let owned_deps = mock_desmos_dependencies();
373        let deps = owned_deps.as_ref();
374        let querier = PostsQuerier::new(&deps.querier);
375
376        let result = querier.query_post(1, 1);
377        let response = result.unwrap();
378        let expected = MockPostsQueries::get_mocked_post_response();
379
380        assert_eq!(expected, response)
381    }
382
383    #[test]
384    fn test_query_post_attachments() {
385        let owned_deps = mock_desmos_dependencies();
386        let deps = owned_deps.as_ref();
387        let querier = PostsQuerier::new(&deps.querier);
388
389        let result = querier.query_post_attachments(1, 1, None);
390        let response = result.unwrap();
391        let expected = MockPostsQueries::get_mocked_post_attachments_response();
392
393        assert_eq!(expected, response)
394    }
395
396    #[test]
397    fn test_iterate_post_attachments() {
398        let owned_deps = mock_desmos_dependencies();
399        let deps = owned_deps.as_ref();
400        let querier = PostsQuerier::new(&deps.querier);
401
402        let mut iterator = querier.iterate_post_attachments(1, 1, 32);
403        let expected = MockPostsQueries::get_mocked_post_attachments_response();
404
405        assert_eq!(expected.attachments[0], iterator.next().unwrap().unwrap());
406        assert_eq!(expected.attachments[1], iterator.next().unwrap().unwrap());
407        assert!(iterator.next().is_none())
408    }
409
410    #[test]
411    fn test_query_poll_answers() {
412        let owned_deps = mock_desmos_dependencies();
413        let deps = owned_deps.as_ref();
414        let querier = PostsQuerier::new(&deps.querier);
415
416        let result = querier.query_poll_answers(1, 1, 1, None, None);
417        let response = result.unwrap();
418        let expected = MockPostsQueries::get_mocked_poll_answers_response();
419
420        assert_eq!(expected, response)
421    }
422
423    #[test]
424    fn test_iterate_poll_answers() {
425        let owned_deps = mock_desmos_dependencies();
426        let deps = owned_deps.as_ref();
427        let querier = PostsQuerier::new(&deps.querier);
428
429        let mut iterator = querier.iterate_poll_answers(1, 1, 1, None, 32);
430        let expected = MockPostsQueries::get_mocked_poll_answers_response();
431
432        assert_eq!(expected.answers[0], iterator.next().unwrap().unwrap());
433        assert!(iterator.next().is_none())
434    }
435
436    #[test]
437    fn test_query_incoming_post_transfer_requests() {
438        let owned_deps = mock_desmos_dependencies();
439        let deps = owned_deps.as_ref();
440        let querier = PostsQuerier::new(&deps.querier);
441
442        let result = querier.query_incoming_post_transfer_requests(1, None, None);
443        let response = result.unwrap();
444        let expected = MockPostsQueries::get_mocked_incoming_post_transfer_requests_response();
445
446        assert_eq!(expected, response)
447    }
448
449    #[test]
450    fn test_iterate_incoming_post_transfer_requests() {
451        let owned_deps = mock_desmos_dependencies();
452        let deps = owned_deps.as_ref();
453        let querier = PostsQuerier::new(&deps.querier);
454
455        let mut iterator = querier.iterate_incoming_post_transfer_requests(1, None, 32);
456        let expected = MockPostsQueries::get_mocked_incoming_post_transfer_requests_response();
457
458        assert_eq!(expected.requests[0], iterator.next().unwrap().unwrap());
459        assert!(iterator.next().is_none())
460    }
461}