فهرست منبع

Merge pull request #2537 from iorveth/forum_thread_tags_runtime

Forum thread tags (runtime part)
Bedeho Mender 3 سال پیش
والد
کامیت
1c37a71351

+ 1 - 3
runtime-modules/forum/src/benchmarking.rs

@@ -961,7 +961,6 @@ benchmarks! {
         // Ensure new thread created successfully
         let new_thread = Thread {
             category_id,
-            metadata_hash: T::calculate_hash(&metadata),
             author_id: forum_user_id.saturated_into(),
             poll: poll_input.clone().map(<Module<T>>::from_poll_input),
             cleanup_pay_off: T::ThreadDeposit::get(),
@@ -1010,13 +1009,12 @@ benchmarks! {
             caller_id.clone(), forum_user_id.saturated_into(), category_id,
             vec![1u8].repeat(MAX_BYTES as usize), vec![1u8].repeat(MAX_BYTES as usize), None
         );
-        let mut thread = Module::<T>::thread_by_id(category_id, thread_id);
+        let thread = Module::<T>::thread_by_id(category_id, thread_id);
 
         let new_metadata = vec![0u8].repeat(j as usize);
 
     }: _ (RawOrigin::Signed(caller_id), forum_user_id.saturated_into(), category_id, thread_id, new_metadata.clone())
     verify {
-        thread.metadata_hash = T::calculate_hash(&new_metadata);
         assert_eq!(Module::<T>::thread_by_id(category_id, thread_id), thread);
 
         assert_last_event::<T>(

+ 4 - 12
runtime-modules/forum/src/lib.rs

@@ -266,9 +266,6 @@ pub struct Post<ForumUserId, ThreadId, Hash, Balance, BlockNumber> {
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Default, Clone, PartialEq, Debug, Eq)]
 pub struct Thread<ForumUserId, CategoryId, Moment, Hash, Balance> {
-    /// Metadata hash
-    pub metadata_hash: Hash,
-
     /// Category in which this thread lives
     pub category_id: CategoryId,
 
@@ -951,7 +948,6 @@ decl_module! {
             // Build a new thread
             let new_thread = Thread {
                 category_id,
-                metadata_hash: T::calculate_hash(&metadata),
                 author_id: forum_user_id,
                 poll,
                 cleanup_pay_off: T::ThreadDeposit::get(),
@@ -994,14 +990,14 @@ decl_module! {
             Ok(())
         }
 
-        /// Edit thread title
+        /// Edit thread metadata
         ///
         /// <weight>
         ///
         /// ## Weight
         /// `O (W + V)` where:
         /// - `W` is the category depth
-        /// - `V` is the length of the thread title.
+        /// - `V` is the length of the thread metadata.
         /// - DB:
         ///    - O(W)
         /// # </weight>
@@ -1021,16 +1017,12 @@ decl_module! {
 
             let account_id = ensure_signed(origin)?;
 
-            let thread = Self::ensure_can_edit_thread_title(account_id, &category_id, &thread_id, &forum_user_id)?;
+            Self::ensure_can_edit_thread_metadata(account_id, &category_id, &thread_id, &forum_user_id)?;
 
             //
             // == MUTATION SAFE ==
             //
 
-            // Update thread metadata
-            let metadata_hash = T::calculate_hash(&new_metadata);
-            <ThreadById<T>>::mutate(thread.category_id, thread_id, |thread| thread.metadata_hash = metadata_hash);
-
             // Store the event
             Self::deposit_event(
                 RawEvent::ThreadMetadataUpdated(
@@ -1830,7 +1822,7 @@ impl<T: Trait> Module<T> {
         Ok(<ThreadById<T>>::get(category_id, thread_id))
     }
 
-    fn ensure_can_edit_thread_title(
+    fn ensure_can_edit_thread_metadata(
         account_id: T::AccountId,
         category_id: &T::CategoryId,
         thread_id: &T::ThreadId,

+ 4 - 6
runtime-modules/forum/src/mock.rs

@@ -575,8 +575,8 @@ pub fn good_category_description_new() -> Vec<u8> {
     b"This is a great new category description for the forum".to_vec()
 }
 
-/// Get a new good thread title
-pub fn good_thread_title() -> Vec<u8> {
+/// Get a new good thread metadata
+pub fn good_thread_metadata() -> Vec<u8> {
     b"Great new thread".to_vec()
 }
 
@@ -585,6 +585,7 @@ pub fn good_thread_text() -> Vec<u8> {
     b"The first post in this thread".to_vec()
 }
 
+/// Get a new metadata for the good thread
 pub fn good_thread_new_metadata() -> Vec<u8> {
     b"Brand new thread metadata".to_vec()
 }
@@ -728,6 +729,7 @@ pub fn create_thread_mock(
     thread_id
 }
 
+/// Create edit thread metadata mock
 pub fn edit_thread_metadata_mock(
     origin: OriginType,
     forum_user_id: ForumUserId<Runtime>,
@@ -747,10 +749,6 @@ pub fn edit_thread_metadata_mock(
         result
     );
     if result.is_ok() {
-        assert_eq!(
-            TestForumModule::thread_by_id(category_id, thread_id).metadata_hash,
-            Runtime::calculate_hash(new_metadata.as_slice()),
-        );
         assert_eq!(
             System::events().last().unwrap().event,
             TestEvent::forum_mod(RawEvent::ThreadMetadataUpdated(

+ 30 - 30
runtime-modules/forum/src/tests.rs

@@ -343,7 +343,7 @@ fn update_category_archival_status_lock_works() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -374,7 +374,7 @@ fn update_category_archival_status_lock_works() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Err(Error::<Runtime>::AncestorCategoryImmutable.into()),
@@ -738,7 +738,7 @@ fn delete_category_non_empty_threads() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -886,7 +886,7 @@ fn create_thread_origin() {
                 forum_user_ids[index],
                 forum_user_ids[index],
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 None,
                 results[index],
@@ -919,7 +919,7 @@ fn create_thread_balance() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -930,7 +930,7 @@ fn create_thread_balance() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -979,7 +979,7 @@ fn create_thread_poll_timestamp() {
                 forum_lead,
                 forum_lead,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 Some(poll),
                 results[index],
@@ -1021,7 +1021,7 @@ fn edit_thread_metadata() {
             forum_users[0],
             forum_users[0],
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1086,7 +1086,7 @@ fn delete_thread() {
             NOT_FORUM_LEAD_ORIGIN_ID,
             NOT_FORUM_LEAD_ORIGIN_ID,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             Some(generate_poll_input(10)),
             Ok(()),
@@ -1256,7 +1256,7 @@ fn move_thread_moderator_permissions() {
             forum_lead,
             forum_lead,
             category_id_1,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1375,7 +1375,7 @@ fn move_thread_invalid_move() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1432,7 +1432,7 @@ fn vote_on_poll_origin() {
                 forum_lead,
                 forum_lead,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 Some(generate_poll_input(expiration_diff)),
                 Ok(()),
@@ -1471,7 +1471,7 @@ fn vote_on_poll_fails_on_double_voting() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             Some(generate_poll_input(expiration_diff)),
             Ok(()),
@@ -1518,7 +1518,7 @@ fn vote_on_poll_exists() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1556,7 +1556,7 @@ fn vote_on_poll_expired() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             Some(generate_poll_input(expiration_diff)),
             Ok(()),
@@ -1606,7 +1606,7 @@ fn moderate_thread_origin_ok() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1658,7 +1658,7 @@ fn add_post_origin() {
                 forum_lead,
                 forum_lead,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 None,
                 Ok(()),
@@ -1706,7 +1706,7 @@ fn add_post_balance() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1782,7 +1782,7 @@ fn edit_post_text() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1851,7 +1851,7 @@ fn edit_non_editable_post_text() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -1910,7 +1910,7 @@ fn react_post() {
                 forum_lead,
                 forum_lead,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 None,
                 Ok(()),
@@ -1978,7 +1978,7 @@ fn moderate_post_origin() {
                 forum_lead,
                 forum_lead,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 None,
                 Ok(()),
@@ -2043,7 +2043,7 @@ fn delete_post_creator() {
             NOT_FORUM_LEAD_ORIGIN_ID,
             NOT_FORUM_LEAD_ORIGIN_ID,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2197,7 +2197,7 @@ fn delete_post_not_creator() {
             NOT_FORUM_LEAD_ORIGIN_ID,
             NOT_FORUM_LEAD_ORIGIN_ID,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2335,7 +2335,7 @@ fn set_stickied_threads_ok() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2353,7 +2353,7 @@ fn set_stickied_threads_ok() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2406,7 +2406,7 @@ fn set_stickied_threads_fails_with_duplicated_ids() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2443,7 +2443,7 @@ fn set_stickied_threads_wrong_moderator() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2486,7 +2486,7 @@ fn set_stickied_threads_thread_not_exists() {
             forum_lead,
             forum_lead,
             category_id,
-            good_thread_title(),
+            good_thread_metadata(),
             good_thread_text(),
             None,
             Ok(()),
@@ -2529,7 +2529,7 @@ fn test_migration_not_done() {
                 mock_origin(origin.clone()),
                 forum_user_id,
                 category_id,
-                good_thread_title(),
+                good_thread_metadata(),
                 good_thread_text(),
                 None,
             ),