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

Add sudo calls to migrate data in spaces and space-follows pallets #119

Merged
Merged
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
31 changes: 26 additions & 5 deletions pallets/space-follows/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub mod pallet {
ModerationError::AccountIsBlocked
);

Self::add_space_follower(follower, space_id)?;
Self::add_space_follower(follower, space_id);
SpaceById::<T>::insert(space_id, space);

Ok(())
Expand All @@ -134,19 +134,39 @@ pub mod pallet {

Self::unfollow_space_by_account(follower, space_id)
}

#[pallet::weight((
100_000 + T::DbWeight::get().reads_writes(3, 4),
DispatchClass::Operational,
Pays::Yes,
))]
pub fn force_follow_space(
origin: OriginFor<T>,
follower: T::AccountId,
space_id: SpaceId,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

ensure!(
!Self::space_followed_by_account((follower.clone(), space_id)),
Error::<T>::AlreadySpaceFollower
);

Self::add_space_follower(follower, space_id);

Ok(Pays::No.into())
}
}

impl<T: Config> Pallet<T> {
fn add_space_follower(follower: T::AccountId, space_id: SpaceId) -> DispatchResult {
fn add_space_follower(follower: T::AccountId, space_id: SpaceId) {
SpaceFollowers::<T>::mutate(space_id, |followers| followers.push(follower.clone()));
SpaceFollowedByAccount::<T>::insert((follower.clone(), space_id), true);
SpacesFollowedByAccount::<T>::mutate(follower.clone(), |space_ids| {
space_ids.push(space_id)
});

Self::deposit_event(Event::SpaceFollowed(follower, space_id));

Ok(())
}

pub fn unfollow_space_by_account(follower: T::AccountId, space_id: SpaceId) -> DispatchResult {
Expand Down Expand Up @@ -174,7 +194,8 @@ pub mod pallet {
impl<T: Config> BeforeSpaceCreated<T> for Pallet<T> {
fn before_space_created(creator: T::AccountId, space: &mut Space<T>) -> DispatchResult {
// Make a space creator the first follower of this space:
Pallet::<T>::add_space_follower(creator, space.id)
Pallet::<T>::add_space_follower(creator, space.id);
Ok(())
}
}
}
Expand Down
75 changes: 73 additions & 2 deletions pallets/spaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pub mod pallet {
Pallet as Permissions, PermissionChecker, SpacePermissionsContext, SpacePermissionsInfoOf,
};
use subsocial_support::{
ensure_content_is_valid,
ensure_content_is_valid, remove_from_bounded_vec,
traits::{IsAccountBlocked, IsContentBlocked, SpacePermissionsProvider},
ModerationError, SpacePermissionsInfo,
ModerationError, SpacePermissionsInfo, WhoAndWhen, WhoAndWhenOf,
};

#[pallet::config]
Expand Down Expand Up @@ -308,6 +308,77 @@ pub mod pallet {
}
Ok(())
}

#[pallet::weight((
1_000_000 + T::DbWeight::get().reads_writes(1, 3),
DispatchClass::Operational,
Pays::Yes,
))]
pub fn force_create_space(
origin: OriginFor<T>,
space_id: SpaceId,
created: WhoAndWhenOf<T>,
owner: T::AccountId,
content: Content,
hidden: bool,
permissions_opt: Option<SpacePermissions>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;

let permissions =
permissions_opt.map(|perms| Permissions::<T>::override_permissions(perms));

let WhoAndWhen { account, time, .. } = created;
let new_who_and_when =
WhoAndWhen { account, block: frame_system::Pallet::<T>::block_number(), time };

let new_space = &mut Space {
id: space_id,
created: new_who_and_when,
updated: false,
owner: owner.clone(),
parent_id: None,
content,
hidden,
permissions,
};

let add_new_space_id_by_owner = |owner: &T::AccountId, space_id: SpaceId| {
SpaceIdsByOwner::<T>::mutate(&owner, |ids| {
ids.try_push(space_id).expect("qed; too many spaces per account")
});
};

// To prevent incorrect [SpaceIdsByOwner] insertion,
// we check if the space already exists.
match Self::require_space(space_id) {
Ok(space) if !space.is_owner(&owner) => {
SpaceIdsByOwner::<T>::mutate(&space.owner, |ids| {
remove_from_bounded_vec(ids, space_id)
});
add_new_space_id_by_owner(&owner, space_id);
},
Err(_) => add_new_space_id_by_owner(&owner, space_id),
_ => (),
}

SpaceById::<T>::insert(space_id, new_space);

Self::deposit_event(Event::SpaceCreated(owner, space_id));

Ok(Pays::No.into())
}

#[pallet::weight((
10_000 + T::DbWeight::get().writes(1),
DispatchClass::Operational,
Pays::Yes,
))]
pub fn force_set_next_space_id(origin: OriginFor<T>, space_id: SpaceId) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
NextSpaceId::<T>::put(space_id);
Ok(Pays::No.into())
}
}

impl<T: Config> Pallet<T> {
Expand Down