Skip to content

Commit

Permalink
feat: support new posts features
Browse files Browse the repository at this point in the history
  • Loading branch information
dadamu committed Aug 23, 2023
1 parent e9210f7 commit 4d2b7e3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/bindings/src/mocks/mock_queriers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn register_default_mock_queries(querier: &mut MockDesmosQuerier) {
use crate::posts::mocks::MockPostsQueries;
use crate::posts::types::{
QueryPollAnswersRequest, QueryPostAttachmentsRequest, QueryPostRequest,
QuerySectionPostsRequest, QuerySubspacePostsRequest,
QuerySectionPostsRequest, QuerySubspacePostsRequest, QueryIncomingPostOwnerTransferRequestsRequest,
};

QuerySubspacePostsRequest::mock_response(
Expand All @@ -125,6 +125,11 @@ fn register_default_mock_queries(querier: &mut MockDesmosQuerier) {
querier,
MockPostsQueries::get_mocked_poll_answers_response(),
);

QueryIncomingPostOwnerTransferRequestsRequest::mock_response(
querier,
MockPostsQueries::get_mocked_incoming_post_transfer_requests_response(),
);
}
#[cfg(feature = "profiles")]
{
Expand Down
15 changes: 14 additions & 1 deletion packages/bindings/src/posts/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::posts::types::AttachmentContent;
use crate::posts::types::{
Attachment, Media, Post, QueryPollAnswersResponse, QueryPostAttachmentsResponse,
QueryPostResponse, QuerySectionPostsResponse, QuerySubspacePostsResponse, ReplySetting,
UserAnswer,
UserAnswer, QueryIncomingPostOwnerTransferRequestsResponse, PostOwnerTransferRequest,
};

use chrono::DateTime;
Expand Down Expand Up @@ -140,4 +140,17 @@ impl MockPostsQueries {
pagination: None,
}
}

/// Function that mocks a [`QueryIncomingPostOwnerTransferRequestsResponse`].
pub fn get_mocked_incoming_post_transfer_requests_response() -> QueryIncomingPostOwnerTransferRequestsResponse {
QueryIncomingPostOwnerTransferRequestsResponse {
requests: vec![PostOwnerTransferRequest{
subspace_id: 1,
post_id: 1,
sender: "sender".into(),
receiver: "receiver".into()
}],
pagination: None,
}
}
}
80 changes: 80 additions & 0 deletions packages/bindings/src/posts/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,60 @@ impl<'a> PostsQuerier<'a> {
page_size,
)
}

/// Queries the incoming post transfer requests having the given `subspace_id`.
///
/// * `subspace_id` - Id of the subspace where the requests are stored.
/// * `receiver` - Optional the address of the user to which query the incoming requests for.
/// * `pagination` - Optional pagination for the request.
pub fn query_incoming_post_transfer_requests(
&self,
subspace_id: u64,
receiver: Option<Addr>,
pagination: Option<PageRequest>,
) -> StdResult<QueryIncomingPostOwnerTransferRequestsResponse> {
self.querier.incoming_post_owner_transfer_requests(
subspace_id,
receiver.unwrap_or_else(|| Addr::unchecked("")).into(),
pagination.map(Into::into),
)
}

/// Gives an iterator to scan over the incoming post transfer requests having the given `subspace_id`.
///
/// * `subspace_id` - Id of the subspace where the requests are stored.
/// * `receiver` - Optional the address of the user to which query the incoming requests for.
/// * `page_size` - Size of the page requested to the chain.
#[cfg(feature = "iterators")]
pub fn iterate_incoming_post_transfer_requests(
&self,
subspace_id: u64,
receiver: Option<Addr>,
page_size: u64,
) -> PageIterator<PostOwnerTransferRequest, Binary> {
PageIterator::new(
Box::new(move |key, limit| {
self.query_incoming_post_transfer_requests(
subspace_id,
receiver.clone(),
Some(PageRequest {
key: key.unwrap_or_default().to_vec(),
limit: limit.into(),
reverse: false,
count_total: false,
offset: 0,
}),
)
.map(|response| Page {
items: response.requests,
next_page_key: response.pagination.and_then(|response| {
(!response.next_key.is_empty()).then_some(Binary::from(response.next_key))
}),
})
}),
page_size,
)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -378,4 +432,30 @@ mod tests {
assert_eq!(expected.answers[0], iterator.next().unwrap().unwrap());
assert!(iterator.next().is_none())
}

#[test]
fn test_query_incoming_post_transfer_requests() {
let owned_deps = mock_desmos_dependencies();
let deps = owned_deps.as_ref();
let querier = PostsQuerier::new(&deps.querier);

let result = querier.query_incoming_post_transfer_requests(1, None, None);
let response = result.unwrap();
let expected = MockPostsQueries::get_mocked_incoming_post_transfer_requests_response();

assert_eq!(expected, response)
}

#[test]
fn test_iterate_incoming_post_transfer_requests() {
let owned_deps = mock_desmos_dependencies();
let deps = owned_deps.as_ref();
let querier = PostsQuerier::new(&deps.querier);

let mut iterator = querier.iterate_incoming_post_transfer_requests(1, None, 32);
let expected = MockPostsQueries::get_mocked_incoming_post_transfer_requests_response();

assert_eq!(expected.requests[0], iterator.next().unwrap().unwrap());
assert!(iterator.next().is_none())
}
}

0 comments on commit 4d2b7e3

Please sign in to comment.