Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move hide space/post functionality to the separate extrinsics #214

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions integration-tests/src/tests/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ fn create_comment_should_fail_when_ipfs_cid_is_invalid() {
#[test]
fn create_comment_should_fail_when_trying_to_create_in_hidden_space_scope() {
ExtBuilder::build_with_post().execute_with(|| {
assert_ok!(_update_space(
None,
None,
Some(space_update(None, Some(true)))
assert_ok!(Spaces::hide_space(
RuntimeOrigin::signed(ACCOUNT1),
SPACE1,
true,
));

assert_noop!(
Expand All @@ -115,7 +115,13 @@ fn create_comment_should_fail_when_trying_create_in_hidden_post_scope() {
assert_ok!(_update_post(
None,
None,
Some(post_update(None, None, Some(true)))
Some(post_update(None, another_space_content_ipfs().into()))
));

assert_ok!(Posts::hide_post(
RuntimeOrigin::signed(ACCOUNT1),
POST1,
true,
));

assert_noop!(
Expand Down Expand Up @@ -175,11 +181,16 @@ fn update_comment_hidden_should_work_when_comment_has_parents() {
Some(should_hide_id),
Some(post_update(
None,
None,
Some(true) // make comment hidden
another_space_content_ipfs().into(),
))
));

assert_ok!(Posts::hide_post(
RuntimeOrigin::signed(ACCOUNT1),
POST1,
true,
));

// We should check that counters weren't increased for all ancestor comments
// except the last before hidden.
for comment_id in first_comment_id..should_hide_by_one_id {
Expand Down Expand Up @@ -223,7 +234,7 @@ fn update_comment_should_fail_when_ipfs_cid_is_invalid() {
_update_comment(
None,
None,
Some(post_update(None, Some(invalid_content_ipfs()), None))
Some(post_update(None, Some(invalid_content_ipfs())))
),
ContentError::InvalidIpfsCid,
);
Expand Down
30 changes: 13 additions & 17 deletions integration-tests/src/tests/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn update_post_should_fail_when_content_is_blocked() {
_update_post(
None, // From ACCOUNT1 (has default permission to UpdateOwnPosts)
None,
Some(post_update(None, Some(valid_content_ipfs()), Some(true)))
Some(post_update(None, Some(valid_content_ipfs())))
),
ModerationError::ContentIsBlocked,
);
Expand All @@ -59,7 +59,7 @@ fn update_post_should_fail_when_account_is_blocked() {
_update_post(
None, // From ACCOUNT1 (has default permission to UpdateOwnPosts)
None,
Some(post_update(None, Some(valid_content_ipfs()), Some(true)))
Some(post_update(None, Some(valid_content_ipfs())))
),
ModerationError::AccountIsBlocked,
);
Expand Down Expand Up @@ -208,15 +208,13 @@ fn update_post_should_work() {
Some(post_update(
None,
Some(expected_content_ipfs.clone()),
Some(true)
))
));

// Check whether post updates correctly
let post = Posts::post_by_id(POST1).unwrap();
assert_eq!(post.space_id, Some(SPACE1));
assert_eq!(post.content, expected_content_ipfs);
assert!(post.hidden);
});
}

Expand Down Expand Up @@ -283,10 +281,10 @@ fn move_hidden_post_should_work() {
let expected_new_space_id = SPACE2;

// Hide the post before moving it
assert_ok!(_update_post(
None,
Some(moved_post_id),
Some(post_update(None, None, Some(true)))
assert_ok!(Posts::hide_post(
RuntimeOrigin::signed(ACCOUNT1),
moved_post_id,
true,
));

assert_ok!(_move_post_1_to_space_2());
Expand Down Expand Up @@ -382,7 +380,7 @@ fn should_fail_when_trying_to_move_comment() {
#[test]
fn update_post_should_work_after_transfer_space_ownership() {
ExtBuilder::build_with_post().execute_with(|| {
let post_update = post_update(None, Some(updated_post_content()), Some(true));
let post_update = post_update(None, Some(updated_post_content()));

assert_ok!(_transfer_default_space_ownership());

Expand All @@ -395,7 +393,7 @@ fn update_post_should_work_after_transfer_space_ownership() {
fn update_any_post_should_work_when_account_has_default_permission() {
ExtBuilder::build_with_a_few_roles_granted_to_account2(vec![SP::CreatePosts]).execute_with(
|| {
let post_update = post_update(None, Some(updated_post_content()), Some(true));
let post_update = post_update(None, Some(updated_post_content()));
assert_ok!(_create_post(
Some(RuntimeOrigin::signed(ACCOUNT2)),
None, // SpaceId 1
Expand All @@ -417,7 +415,7 @@ fn update_any_post_should_work_when_account_has_default_permission() {
fn update_any_post_should_work_when_one_of_roles_is_permitted() {
ExtBuilder::build_with_a_few_roles_granted_to_account2(vec![SP::UpdateAnyPost]).execute_with(
|| {
let post_update = post_update(None, Some(updated_post_content()), Some(true));
let post_update = post_update(None, Some(updated_post_content()));
assert_ok!(_create_default_post()); // PostId 1

// Post update with ID 1 should be fine
Expand Down Expand Up @@ -458,8 +456,7 @@ fn update_post_should_fail_when_post_not_found() {
Some(post_update(
// FIXME: when Post's `space_id` update is fully implemented
None, /*Some(SPACE2)*/
None,
Some(true) /*None*/
valid_content_ipfs().into(),
))
),
PostsError::<TestRuntime>::PostNotFound
Expand All @@ -484,8 +481,7 @@ fn update_post_should_fail_when_account_has_no_permission_to_update_any_post() {
Some(post_update(
// FIXME: when Post's `space_id` update is fully implemented
None, /*Some(SPACE2)*/
None,
Some(true) /*None*/
valid_content_ipfs().into(),
))
),
PostsError::<TestRuntime>::NoPermissionToUpdateAnyPost
Expand All @@ -501,7 +497,7 @@ fn update_post_should_fail_when_ipfs_cid_is_invalid() {
_update_post(
None,
None,
Some(post_update(None, Some(invalid_content_ipfs()), None))
Some(post_update(None, Some(invalid_content_ipfs())))
),
ContentError::InvalidIpfsCid,
);
Expand All @@ -512,7 +508,7 @@ fn update_post_should_fail_when_ipfs_cid_is_invalid() {
fn update_post_should_fail_when_no_right_permission_in_account_roles() {
ExtBuilder::build_with_a_few_roles_granted_to_account2(vec![SP::UpdateAnyPost]).execute_with(
|| {
let post_update = post_update(None, Some(updated_post_content()), Some(true));
let post_update = post_update(None, Some(updated_post_content()));
assert_ok!(_create_default_post());
// PostId 1
assert_ok!(_delete_default_role());
Expand Down
16 changes: 8 additions & 8 deletions integration-tests/src/tests/reactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ fn create_post_reaction_should_fail_when_post_not_found() {
fn create_post_reaction_should_fail_when_trying_to_react_in_hidden_space() {
ExtBuilder::build_with_post().execute_with(|| {
// Hide the space
assert_ok!(_update_space(
None,
None,
Some(space_update(None, Some(true)))
assert_ok!(Spaces::hide_space(
RuntimeOrigin::signed(ACCOUNT1),
SPACE1,
true,
));

assert_noop!(
Expand All @@ -103,10 +103,10 @@ fn create_post_reaction_should_fail_when_trying_to_react_in_hidden_space() {
fn create_post_reaction_should_fail_when_trying_to_react_on_hidden_post() {
ExtBuilder::build_with_post().execute_with(|| {
// Hide the post
assert_ok!(_update_post(
None,
None,
Some(post_update(None, None, Some(true)))
assert_ok!(Posts::hide_post(
RuntimeOrigin::signed(ACCOUNT1),
POST1,
true,
));

assert_noop!(
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/src/tests/space_follows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fn follow_space_should_fail_when_account_is_already_space_follower() {
#[test]
fn follow_space_should_fail_when_trying_to_follow_hidden_space() {
ExtBuilder::build_with_space().execute_with(|| {
assert_ok!(_update_space(
None,
None,
Some(space_update(None, Some(true)))
assert_ok!(Spaces::hide_space(
RuntimeOrigin::signed(ACCOUNT1),
SPACE1,
true,
));

assert_noop!(
Expand Down
8 changes: 2 additions & 6 deletions integration-tests/src/tests/spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn update_space_should_fail_when_content_is_blocked() {
_update_space(
None,
None,
Some(space_update(Some(valid_content_ipfs()), None))
Some(space_update(Some(valid_content_ipfs())))
),
ModerationError::ContentIsBlocked,
);
Expand Down Expand Up @@ -225,15 +225,13 @@ fn update_space_should_work() {
None,
Some(space_update(
Some(expected_content_ipfs.clone()),
Some(true),
))
));

// Check whether space updates correctly
let space = Spaces::space_by_id(SPACE1).unwrap();
// assert_eq!(space.handle, Some(new_handle.clone()));
assert_eq!(space.content, expected_content_ipfs);
assert!(space.hidden);

// assert_eq!(find_space_id_by_handle(space_handle()), None);
// assert_eq!(find_space_id_by_handle(new_handle), Some(SPACE1));
Expand All @@ -250,7 +248,6 @@ fn update_space_should_work_when_one_of_roles_is_permitted() {
|| {
let space_update = space_update(
Some(updated_space_content()),
Some(true),
);

assert_ok!(_update_space(
Expand Down Expand Up @@ -414,7 +411,7 @@ fn update_space_should_fail_when_ipfs_cid_is_invalid() {
_update_space(
None,
None,
Some(space_update(Some(invalid_content_ipfs()), None,))
Some(space_update(Some(invalid_content_ipfs()),))
),
ContentError::InvalidIpfsCid,
);
Expand All @@ -427,7 +424,6 @@ fn update_space_should_fail_when_no_right_permission_in_account_roles() {
|| {
let space_update = space_update(
Some(updated_space_content()),
Some(true),
);

assert_ok!(_delete_default_role());
Expand Down
6 changes: 2 additions & 4 deletions integration-tests/src/utils/posts_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ pub(crate) fn updated_post_content() -> Content {
pub(crate) fn post_update(
space_id: Option<SpaceId>,
content: Option<Content>,
hidden: Option<bool>,
) -> PostUpdate {
PostUpdate {
space_id,
content,
hidden,
}
}

Expand Down Expand Up @@ -72,7 +70,7 @@ pub(crate) fn _update_post(
Posts::update_post(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT1)),
post_id.unwrap_or(POST1),
update.unwrap_or_else(|| post_update(None, None, None)),
update.unwrap_or_else(|| post_update(None, None)),
)
}

Expand Down Expand Up @@ -127,7 +125,7 @@ pub(crate) fn _update_comment(
origin,
Some(post_id.unwrap_or(POST2)),
Some(update.unwrap_or_else(||
post_update(None, Some(reply_content_ipfs()), None))
post_update(None, Some(reply_content_ipfs())))
),
)
}
6 changes: 2 additions & 4 deletions integration-tests/src/utils/spaces_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ pub(crate) fn updated_space_content() -> Content {
pub(crate) fn update_for_space_content(
new_content: Content,
) -> SpaceUpdate {
space_update(Some(new_content), None)
space_update(Some(new_content))
}

pub(crate) fn space_update(
content: Option<Content>,
hidden: Option<bool>,
) -> SpaceUpdate {
SpaceUpdate {
content,
hidden,
permissions: None,
}
}
Expand Down Expand Up @@ -64,6 +62,6 @@ pub(crate) fn _update_space(
Spaces::update_space(
origin.unwrap_or_else(|| RuntimeOrigin::signed(ACCOUNT1)),
space_id.unwrap_or(SPACE1),
update.unwrap_or_else(|| space_update(None, None)),
update.unwrap_or_else(|| space_update(None)),
)
}
1 change: 0 additions & 1 deletion pallets/posts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ benchmarks! {
let new_content = Content::IPFS(b"Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu".to_vec());

let update = PostUpdate {
hidden: Some(true),
content: Some(new_content.clone()),
space_id: None,
};
Expand Down
33 changes: 33 additions & 0 deletions pallets/posts/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use frame_support::dispatch::DispatchResult;
use sp_runtime::traits::Saturating;

use subsocial_support::{remove_from_vec, SpaceId};
use subsocial_support::traits::HidePost;

use super::*;

Expand Down Expand Up @@ -334,4 +335,36 @@ impl<T: Config> Pallet<T> {

Ok(())
}

pub fn do_hide_post(
caller: Option<T::AccountId>,
post_id: PostId,
hidden: bool,
) -> DispatchResult {
let mut post = Self::require_post(post_id)?;

if let (Some(caller), Some(space)) = (caller, &post.try_get_space()) {
ensure!(
T::IsAccountBlocked::is_allowed_account(caller.clone(), space.id),
ModerationError::AccountIsBlocked
);
Self::ensure_account_can_update_post(&caller, &post, space)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use ensure_account_can_hide_post

}

if hidden != post.hidden {
post.hidden = hidden;

<PostById<T>>::insert(post.id, post);
Self::deposit_event(Event::PostHiddenChanged { hidden, post_id });
}

Ok(())
}
}

impl<T: Config> HidePost<T::AccountId, SpaceId> for Pallet<T> {
fn hide_post(caller: Option<T::AccountId>, post_id: PostId, hidden: bool) -> DispatchResult {
Self::do_hide_post(caller, post_id, hidden)
}

}
Loading