Browse Source

forum: improve deletion signature

iorveth 3 years ago
parent
commit
2e3b800780

+ 4 - 5
runtime-modules/forum/src/benchmarking.rs

@@ -1708,9 +1708,9 @@ benchmarks! {
             text.clone(), text.clone(), poll
         );
         let hide = false;
-        let mut posts = Vec::new();
+        let mut posts = BTreeMap::new();
         for _ in 0 .. k {
-            posts.push((
+            posts.insert((
                     category_id,
                     thread_id,
                     add_thread_post::<T>(
@@ -1719,9 +1719,8 @@ benchmarks! {
                         category_id,
                         thread_id,
                         vec![0u8],
-                    ),
+                    )),
                     hide
-                )
             );
         }
 
@@ -1748,7 +1747,7 @@ benchmarks! {
         thread.number_of_posts -= k as u64;
         assert_eq!(Module::<T>::thread_by_id(category_id, thread_id), thread);
 
-        for post in posts.clone() {
+        for (post, _) in &posts {
             assert!(!<PostById<T>>::contains_key(post.1, post.2));
         }
 

+ 7 - 9
runtime-modules/forum/src/lib.rs

@@ -17,6 +17,7 @@ use sp_arithmetic::traits::{BaseArithmetic, One};
 pub use sp_io::storage::clear_prefix;
 use sp_runtime::traits::{AccountIdConversion, MaybeSerialize, Member};
 use sp_runtime::{ModuleId, SaturatedConversion};
+use sp_std::collections::btree_map::BTreeMap;
 use sp_std::collections::btree_set::BTreeSet;
 use sp_std::fmt::Debug;
 use sp_std::prelude::*;
@@ -526,7 +527,7 @@ decl_event!(
         PostModerated(PostId, Vec<u8>, PrivilegedActor, CategoryId, ThreadId),
 
         /// Post with givne id was deleted.
-        PostDeleted(Vec<u8>, ForumUserId, Vec<(CategoryId, ThreadId, PostId, bool)>),
+        PostDeleted(Vec<u8>, ForumUserId, BTreeMap<(CategoryId, ThreadId, PostId), bool>),
 
         /// Post with given id had its text updated.
         /// The second argument reflects the number of total edits when the text update occurs.
@@ -1444,20 +1445,17 @@ decl_module! {
         fn delete_posts(
             origin,
             forum_user_id: ForumUserId<T>,
-            posts: Vec<(T::CategoryId, T::ThreadId, T::PostId, bool)>,
+            posts: BTreeMap<(T::CategoryId, T::ThreadId, T::PostId), bool>,
             rationale: Vec<u8>,
         ) -> DispatchResult {
 
-            // Check only unique post instances.
-            let unique_posts: BTreeSet<_> = posts.into_iter().collect();
-
             // Ensure data migration is done
             Self::ensure_data_migration_done()?;
 
             let account_id = ensure_signed(origin)?;
 
-            let mut deleting_posts = BTreeSet::new();
-            for (category_id, thread_id, post_id, hide) in &unique_posts {
+            let mut deleting_posts = Vec::new();
+            for ((category_id, thread_id, post_id), hide) in &posts {
                 // Ensure actor is allowed to moderate post and post is editable
                 let post = Self::ensure_can_delete_post(
                     &account_id,
@@ -1468,7 +1466,7 @@ decl_module! {
                     *hide,
                 )?;
 
-                deleting_posts.insert((category_id, thread_id, post_id, post));
+                deleting_posts.push((category_id, thread_id, post_id, post));
             }
 
             //
@@ -1484,7 +1482,7 @@ decl_module! {
 
             // Generate event
             Self::deposit_event(
-                RawEvent::PostDeleted(rationale, forum_user_id, unique_posts.into_iter().collect())
+                RawEvent::PostDeleted(rationale, forum_user_id, posts)
             );
 
             Ok(())

+ 2 - 1
runtime-modules/forum/src/mock.rs

@@ -821,7 +821,8 @@ pub fn delete_post_mock(
 ) {
     let initial_balance = balances::Module::<Runtime>::free_balance(&account_id);
     let number_of_posts = <ThreadById<Runtime>>::get(category_id, thread_id).number_of_posts;
-    let deleted_posts = vec![(category_id, thread_id, post_id, hide)];
+    let mut deleted_posts = BTreeMap::new();
+    deleted_posts.insert((category_id, thread_id, post_id), hide);
 
     assert_eq!(
         TestForumModule::delete_posts(