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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! Contains a querier to query data from the Desmos x/relationships module.

#[cfg(feature = "iterators")]
use crate::{
    iter::page_iterator::{Page, PageIterator},
    relationships::models::{Relationship, UserBlock},
};
#[cfg(feature = "iterators")]
use cosmwasm_std::Binary;

use crate::{
    query::DesmosQuery,
    relationships::{
        models_query::{QueryBlocksResponse, QueryRelationshipsResponse},
        query::RelationshipsQuery,
    },
    types::PageRequest,
};
use cosmwasm_std::{Addr, Querier, QuerierWrapper, StdResult};

/// Querier able to query data from the Desmos x/relationships module.
pub struct RelationshipsQuerier<'a> {
    querier: QuerierWrapper<'a, DesmosQuery>,
}

impl<'a> RelationshipsQuerier<'a> {
    /// Creates a new instance of [`RelationshipsQuerier`].
    ///
    /// # Example
    /// ```
    /// use std::ops::Deref;
    /// use cosmwasm_std::{DepsMut, MessageInfo};
    /// use desmos_bindings::relationships::querier::RelationshipsQuerier;
    ///
    /// pub fn contract_action(deps: DepsMut, _: MessageInfo) {
    ///     let querier = RelationshipsQuerier::new(deps.querier.deref());
    /// }
    /// ```
    pub fn new(querier: &'a dyn Querier) -> Self {
        Self {
            querier: QuerierWrapper::<'a, DesmosQuery>::new(querier),
        }
    }

    /// Queries the relationships inside a subspaces.
    ///
    /// * `subspace_id` - Subspace to query the relationships for.
    /// * `user` - Optional address of the user for which to query the relationships.
    /// * `counterparty` - Optional address of the counterparty of the relationships (used only if the
    /// `user` is provided).
    /// * `pagination` - Optional pagination configs.
    pub fn query_relationships(
        &self,
        subspace_id: u64,
        user: Option<Addr>,
        counterparty: Option<Addr>,
        pagination: Option<PageRequest>,
    ) -> StdResult<QueryRelationshipsResponse> {
        let request = DesmosQuery::Relationships(RelationshipsQuery::Relationships {
            subspace_id: subspace_id.into(),
            user,
            counterparty,
            pagination,
        });

        let res: QueryRelationshipsResponse = self.querier.query(&request.into())?;
        Ok(res)
    }

    /// Gives an iterator to scan over a user's relationships created in a subspace or
    /// all the relationships created in a subspace.
    ///
    /// * `subspace_id` - Subspace to query the relationships for.
    /// * `user` - Optional address of the user for which to query the relationships.
    /// * `page_size` - Size of the page requested to the chain.
    #[cfg(feature = "iterators")]
    pub fn iterate_relationships(
        &self,
        subspace_id: u64,
        user: Option<Addr>,
        page_size: u64,
    ) -> PageIterator<Relationship, Binary> {
        PageIterator::new(
            Box::new(move |key, limit| {
                self.query_relationships(
                    subspace_id,
                    user.clone(),
                    None,
                    Some(PageRequest {
                        key,
                        limit: limit.into(),
                        reverse: false,
                        count_total: false,
                        offset: None,
                    }),
                )
                .map(|response| Page {
                    items: response.relationships,
                    next_page_key: response
                        .pagination
                        .and_then(|pagination| pagination.next_key),
                })
            }),
            page_size,
        )
    }

    /// Queries the blocks created inside a subspace.
    ///
    /// * `subspace_id` - Subspace to query the blocks for.
    /// * `blocker` - Optional address of the blocker to query the blocks for.
    /// * `blocked` - Optional address of the blocked user to query the block for (used only if
    /// the `blocker` is provided).
    /// * `pagination` - Optional pagination configs.
    pub fn query_blocks(
        &self,
        subspace_id: u64,
        blocker: Option<Addr>,
        blocked: Option<Addr>,
        pagination: Option<PageRequest>,
    ) -> StdResult<QueryBlocksResponse> {
        let request = DesmosQuery::Relationships(RelationshipsQuery::Blocks {
            subspace_id: subspace_id.into(),
            blocker,
            blocked,
            pagination,
        });

        let res: QueryBlocksResponse = self.querier.query(&request.into())?;
        Ok(res)
    }

    /// Gives an iterator to scan over the users blocked from a specific user in a subspace or
    /// all the blocks performed from the users in a subspace.
    ///
    /// * `subspace_id` - Subspace to query the blocks for.
    /// * `blocker` - Optional address of the blocker to query the blocks for.
    /// * `page_size` - Size of the page requested to the chain.
    #[cfg(feature = "iterators")]
    pub fn iterate_blocks(
        &self,
        subspace_id: u64,
        blocker: Option<Addr>,
        page_size: u64,
    ) -> PageIterator<UserBlock, Binary> {
        PageIterator::new(
            Box::new(move |key, limit| {
                self.query_blocks(
                    subspace_id,
                    blocker.clone(),
                    None,
                    Some(PageRequest {
                        key,
                        limit: limit.into(),
                        reverse: false,
                        count_total: false,
                        offset: None,
                    }),
                )
                .map(|response| Page {
                    items: response.blocks,
                    next_page_key: response
                        .pagination
                        .and_then(|pagination| pagination.next_key),
                })
            }),
            page_size,
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        mocks::mock_dependencies_with_custom_querier,
        relationships::{
            mocks::MockRelationshipsQueries,
            models_query::{QueryBlocksResponse, QueryRelationshipsResponse},
            querier::RelationshipsQuerier,
        },
    };
    use cosmwasm_std::Addr;
    use std::ops::Deref;

    #[test]
    fn test_query_relationships() {
        let owned_deps = mock_dependencies_with_custom_querier(&[]);
        let deps = owned_deps.as_ref();
        let relationships_querier = RelationshipsQuerier::new(deps.querier.deref());

        let response = relationships_querier
            .query_relationships(
                0,
                Some(Addr::unchecked("")),
                Some(Addr::unchecked("")),
                None,
            )
            .unwrap();
        let expected = QueryRelationshipsResponse {
            relationships: vec![MockRelationshipsQueries::get_mock_relationship()],
            pagination: Default::default(),
        };

        assert_eq!(response, expected)
    }

    #[test]
    fn test_iterate_relationships() {
        let owned_deps = mock_dependencies_with_custom_querier(&[]);
        let deps = owned_deps.as_ref();
        let relationships_querier = RelationshipsQuerier::new(deps.querier.deref());

        let mut it = relationships_querier.iterate_relationships(0, Some(Addr::unchecked("")), 10);

        assert_eq!(
            it.next().unwrap().unwrap(),
            MockRelationshipsQueries::get_mock_relationship()
        );
        assert!(it.next().is_none());
    }

    #[test]
    fn test_query_blocks() {
        let owned_deps = mock_dependencies_with_custom_querier(&[]);
        let deps = owned_deps.as_ref();
        let relationships_querier = RelationshipsQuerier::new(deps.querier.deref());

        let response = relationships_querier
            .query_blocks(
                0,
                Some(Addr::unchecked("")),
                Some(Addr::unchecked("")),
                None,
            )
            .unwrap();
        let expected = QueryBlocksResponse {
            blocks: vec![MockRelationshipsQueries::get_mock_user_block()],
            pagination: Default::default(),
        };

        assert_eq!(response, expected)
    }

    #[test]
    fn test_iterate_blocks() {
        let owned_deps = mock_dependencies_with_custom_querier(&[]);
        let deps = owned_deps.as_ref();
        let relationships_querier = RelationshipsQuerier::new(deps.querier.deref());

        let mut it = relationships_querier.iterate_blocks(0, Some(Addr::unchecked("")), 10);

        assert_eq!(
            it.next().unwrap().unwrap(),
            MockRelationshipsQueries::get_mock_user_block()
        );
        assert!(it.next().is_none());
    }
}