Browse Source

fix import of forum

Mokhtar Naamani 4 years ago
parent
commit
dafee37a8e

File diff suppressed because it is too large
+ 0 - 0
node/res/forum_data_acropolis_encoded.json


File diff suppressed because it is too large
+ 0 - 0
node/res/forum_data_acropolis_serialized.json


File diff suppressed because it is too large
+ 0 - 0
node/res/forum_nicaea_encoded.json


+ 1 - 1
node/src/chain_spec.rs

@@ -282,7 +282,7 @@ pub fn testnet_genesis(
             default_paid_membership_fee: 100u128,
             members,
         }),
-        forum: Some(crate::forum_config::from_encoded::create(root_key)),
+        forum: Some(crate::forum_config::create(root_key)),
         data_object_type_registry: Some(DataObjectTypeRegistryConfig {
             first_data_object_type_id: 1,
         }),

+ 45 - 22
node/src/forum_config/from_encoded.rs → node/src/forum_config.rs

@@ -1,74 +1,97 @@
-// This module is not used but included as sample code
-// and highlights some pitfalls.
-
-use super::new_validation;
+use codec::Decode;
 use node_runtime::{
+    common::constraints::InputValidationLengthConstraint,
     forum::{Category, CategoryId, Post, Thread},
     AccountId, BlockNumber, ForumConfig, Moment, PostId, ThreadId,
 };
 use serde::Deserialize;
-use serde_json::Result;
 
-use codec::Decode;
+fn new_validation(min: u16, max_min_diff: u16) -> InputValidationLengthConstraint {
+    InputValidationLengthConstraint { min, max_min_diff }
+}
 
 #[derive(Deserialize)]
 struct ForumData {
     /// hex encoded categories
-    categories: Vec<(CategoryId, String)>,
+    categories: Vec<String>,
     /// hex encoded posts
-    posts: Vec<(PostId, String)>,
+    posts: Vec<String>,
     /// hex encoded threads
-    threads: Vec<(ThreadId, String)>,
+    threads: Vec<String>,
 }
 
 fn decode_post(encoded: String) -> Post<BlockNumber, Moment, AccountId, ThreadId, PostId> {
     // hex string must not include '0x' prefix!
-    let encoded = hex::decode(encoded.as_bytes()).expect("failed to parse hex string");
+    let encoded = hex::decode(&encoded[2..].as_bytes()).expect("failed to parse post hex string");
     Decode::decode(&mut encoded.as_slice()).unwrap()
 }
 
 fn decode_category(encoded: String) -> Category<BlockNumber, Moment, AccountId> {
     // hex string must not include '0x' prefix!
-    let encoded = hex::decode(encoded.as_bytes()).expect("failed to parse hex string");
+    let encoded =
+        hex::decode(&encoded[2..].as_bytes()).expect("failed to parse category hex string");
     Decode::decode(&mut encoded.as_slice()).unwrap()
 }
 
 fn decode_thread(encoded: String) -> Thread<BlockNumber, Moment, AccountId, ThreadId> {
     // hex string must not include '0x' prefix!
-    let encoded = hex::decode(encoded.as_bytes()).expect("failed to parse hex string");
+    let encoded = hex::decode(&encoded[2..].as_bytes()).expect("failed to parse thread hex string");
     Decode::decode(&mut encoded.as_slice()).unwrap()
 }
 
-fn parse_forum_json() -> Result<ForumData> {
-    let data = include_str!("../../res/forum_nicaea_encoded.json");
+fn parse_forum_json() -> serde_json::Result<ForumData> {
+    let data = include_str!("../res/forum_nicaea_encoded.json");
     serde_json::from_str(data)
 }
 
 pub fn create(forum_sudo: AccountId) -> ForumConfig {
     let forum_data = parse_forum_json().expect("failed loading forum data");
+    let first_id = 1;
+
+    let next_category_id: CategoryId = forum_data.categories.last().map_or(first_id, |category| {
+        decode_category(category.clone()).id + 1
+    });
+    assert_eq!(
+        next_category_id,
+        (forum_data.categories.len() + 1) as CategoryId
+    );
+
+    let next_thread_id: ThreadId = forum_data
+        .threads
+        .last()
+        .map_or(first_id, |thread| decode_thread(thread.clone()).id + 1);
+    assert_eq!(next_thread_id, (forum_data.threads.len() + 1) as ThreadId);
 
-    let next_category_id: CategoryId = forum_data
-        .categories
+    let next_post_id: PostId = forum_data
+        .posts
         .last()
-        .map_or(1, |category| category.0 + 1);
-    let next_thread_id: ThreadId = forum_data.threads.last().map_or(1, |thread| thread.0 + 1);
-    let next_post_id: PostId = forum_data.posts.last().map_or(1, |post| post.0 + 1);
+        .map_or(first_id, |post| decode_post(post.clone()).id + 1);
+    assert_eq!(next_post_id, (forum_data.posts.len() + 1) as PostId);
 
     ForumConfig {
         category_by_id: forum_data
             .categories
             .into_iter()
-            .map(|category| (category.0, decode_category(category.1)))
+            .map(|encoded_category| {
+                let category = decode_category(encoded_category);
+                (category.id, category)
+            })
             .collect(),
         thread_by_id: forum_data
             .threads
             .into_iter()
-            .map(|thread| (thread.0, decode_thread(thread.1)))
+            .map(|encoded_thread| {
+                let thread = decode_thread(encoded_thread);
+                (thread.id, thread)
+            })
             .collect(),
         post_by_id: forum_data
             .posts
             .into_iter()
-            .map(|post| (post.0, decode_post(post.1)))
+            .map(|encoded_post| {
+                let post = decode_post(encoded_post);
+                (post.id, post)
+            })
             .collect(),
         next_category_id,
         next_thread_id,

+ 0 - 51
node/src/forum_config/from_serialized.rs

@@ -1,51 +0,0 @@
-#![allow(clippy::type_complexity)]
-
-use super::new_validation;
-use node_runtime::{
-    forum::{Category, CategoryId, Post, Thread},
-    AccountId, BlockNumber, ForumConfig, Moment, PostId, ThreadId,
-};
-use serde::Deserialize;
-use serde_json::Result;
-
-#[derive(Deserialize)]
-struct ForumData {
-    categories: Vec<(CategoryId, Category<BlockNumber, Moment, AccountId>)>,
-    posts: Vec<(
-        PostId,
-        Post<BlockNumber, Moment, AccountId, ThreadId, PostId>,
-    )>,
-    threads: Vec<(ThreadId, Thread<BlockNumber, Moment, AccountId, ThreadId>)>,
-}
-
-fn parse_forum_json() -> Result<ForumData> {
-    let data = include_str!("../../res/forum_data_empty.json");
-    serde_json::from_str(data)
-}
-
-pub fn create(forum_sudo: AccountId) -> ForumConfig {
-    let forum_data = parse_forum_json().expect("failed loading forum data");
-
-    let next_category_id: CategoryId = forum_data
-        .categories
-        .last()
-        .map_or(1, |category| category.0 + 1);
-    let next_thread_id: ThreadId = forum_data.threads.last().map_or(1, |thread| thread.0 + 1);
-    let next_post_id: PostId = forum_data.posts.last().map_or(1, |post| post.0 + 1);
-
-    ForumConfig {
-        category_by_id: forum_data.categories,
-        thread_by_id: forum_data.threads,
-        post_by_id: forum_data.posts,
-        next_category_id,
-        next_thread_id,
-        next_post_id,
-        category_title_constraint: new_validation(10, 90),
-        category_description_constraint: new_validation(10, 490),
-        thread_title_constraint: new_validation(10, 90),
-        post_text_constraint: new_validation(10, 990),
-        thread_moderation_rationale_constraint: new_validation(10, 290),
-        post_moderation_rationale_constraint: new_validation(10, 290),
-        forum_sudo,
-    }
-}

+ 0 - 8
node/src/forum_config/mod.rs

@@ -1,8 +0,0 @@
-// pub mod from_serialized;
-pub mod from_encoded;
-
-use node_runtime::common::constraints::InputValidationLengthConstraint;
-
-pub fn new_validation(min: u16, max_min_diff: u16) -> InputValidationLengthConstraint {
-    InputValidationLengthConstraint { min, max_min_diff }
-}

+ 3 - 3
runtime-modules/forum/src/lib.rs

@@ -111,7 +111,7 @@ pub struct PostTextChange<BlockNumber, Moment> {
 #[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
 pub struct Post<BlockNumber, Moment, AccountId, ThreadId, PostId> {
     /// Post identifier
-    id: PostId,
+    pub id: PostId,
 
     /// Id of thread to which this post corresponds.
     thread_id: ThreadId,
@@ -144,7 +144,7 @@ pub struct Post<BlockNumber, Moment, AccountId, ThreadId, PostId> {
 #[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
 pub struct Thread<BlockNumber, Moment, AccountId, ThreadId> {
     /// Thread identifier
-    id: ThreadId,
+    pub id: ThreadId,
 
     /// Title
     title: Vec<u8>,
@@ -208,7 +208,7 @@ pub struct ChildPositionInParentCategory {
 #[derive(Encode, Decode, Default, Clone, PartialEq, Eq)]
 pub struct Category<BlockNumber, Moment, AccountId> {
     /// Category identifier
-    id: CategoryId,
+    pub id: CategoryId,
 
     /// Title
     title: Vec<u8>,

Some files were not shown because too many files changed in this diff