Skip to content

Commit

Permalink
posts
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Dec 15, 2022
1 parent c7de8b7 commit addef3f
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 38 deletions.
43 changes: 23 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ members = [
"traits/sortition-sum-game-link",
"traits/schelling-game-shared-link",
"pallets/election",
"pallets/project-submission",
"pallets/schelling-game-shared",
"pallets/profile-validation",
"pallets/tags",
"pallets/spaces",
"pallets/posts",
"pallets/support",
"runtime",
]
Expand Down
20 changes: 12 additions & 8 deletions pallets/project-submission/Cargo.toml → pallets/posts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
[package]
name = "project-submission"
version = "4.0.0-dev"
description = "FRAME pallet template for defining custom runtime logic."
authors = ["Substrate DevHub <https://github.com/substrate-developer-hub>"]
homepage = "https://substrate.io/"
name = "posts"
version = "0.1.0"
edition = "2021"
license = "Unlicense"
publish = false
repository = "https://github.com/substrate-developer-hub/substrate-node-template/"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand All @@ -26,6 +22,12 @@ log = { default-features= false, version="0.4"}
sp-runtime = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
sp-std = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
sp-arithmetic = { default-features = false, version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
serde = { features = ['derive'], optional = true, version = '1.0.137' }

# Substrate dependencies
support = { default-features = false, path = '../support' }
pallet-timestamp = {git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18", default-features = false }


[dev-dependencies]
sp-core = { default-features = false, version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.18" }
Expand All @@ -36,6 +38,7 @@ frame-support-test = { default-features = false, version = "3.0.0-dev", git = "h
[features]
default = ["std"]
std = [
"serde",
"codec/std",
"scale-info/std",
"frame-support/std",
Expand All @@ -46,6 +49,7 @@ std = [
"sp-runtime/std",
"sp-std/std",
"sp-arithmetic/std",
"pallet-timestamp/std",
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
Expand Down
File renamed without changes.
File renamed without changes.
32 changes: 29 additions & 3 deletions pallets/project-submission/src/lib.rs → pallets/posts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,30 @@ mod tests;
mod benchmarking;

mod extras;
pub mod types;

pub use types::{FIRST_POST_ID, Post};

// use frame_support::sp_std::{prelude::*};
// use scale_info::prelude::format;
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use support::{
Content, PostId, SpaceId, WhoAndWhen, WhoAndWhenOf,
};

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;


/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + pallet_timestamp::Config{
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}
Expand All @@ -37,6 +48,21 @@ pub mod pallet {
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

#[pallet::type_value]
pub fn DefaultForNextPostId() -> PostId {
FIRST_POST_ID
}

/// The next post id.
#[pallet::storage]
#[pallet::getter(fn next_post_id)]
pub type NextPostId<T: Config> = StorageValue<_, PostId, ValueQuery, DefaultForNextPostId>;

/// Get the details of a post by its' id.
#[pallet::storage]
#[pallet::getter(fn post_by_id)]
pub type PostById<T: Config> = StorageMap<_, Twox64Concat, PostId, Post<T>>;

// The pallet's runtime storage items.
// https://docs.substrate.io/v3/runtime/storage
#[pallet::storage]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate as pallet_template;
use frame_support::traits::{ConstU16, ConstU64};
use frame_support::traits::{ConstU16, ConstU64, OnTimestampSet};
use sp_std::cell::RefCell;
use frame_system as system;
use sp_core::H256;
use sp_runtime::{
Expand Down Expand Up @@ -53,6 +54,25 @@ impl pallet_template::Config for Test {
type Event = Event;
}

type Moment = u64;

thread_local! {
pub static CAPTURED_MOMENT: RefCell<Option<Moment>> = RefCell::new(None);
}
pub struct MockOnTimestampSet;
impl OnTimestampSet<Moment> for MockOnTimestampSet {
fn on_timestamp_set(moment: Moment) {
CAPTURED_MOMENT.with(|x| *x.borrow_mut() = Some(moment));
}
}
impl pallet_timestamp::Config for Test {
type Moment = Moment;

type OnTimestampSet = MockOnTimestampSet;
type MinimumPeriod = ConstU64<5>;
type WeightInfo = ();
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
Expand Down
File renamed without changes.
71 changes: 71 additions & 0 deletions pallets/posts/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use super::*;

pub const FIRST_POST_ID: u64 = 1;

/// Information about a post's owner, its' related space, content, and visibility.
#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct Post<T: Config> {
/// Unique sequential identifier of a post. Examples of post ids: `1`, `2`, `3`, and so on.
pub id: PostId,

pub created: WhoAndWhenOf<T>,
/// True, if the content of this post was edited.
pub edited: bool,

/// The current owner of a given post.
pub owner: T::AccountId,

/// Through post extension you can provide specific information necessary for different kinds
/// of posts such as regular posts, comments, and shared posts.
pub extension: PostExtension,

/// An id of a space which contains a given post.
pub space_id: Option<SpaceId>,

pub content: Content,

/// Hidden field is used to recommend to end clients (web and mobile apps) that a particular
/// posts and its' comments should not be shown.
pub hidden: bool,

/// The number of times a given post has been upvoted.
pub upvotes_count: u32,

/// The number of times a given post has been downvoted.
pub downvotes_count: u32,
}

#[derive(Encode, Decode, Default, Clone, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct PostUpdate {
/// Deprecated: This field has no effect in `fn update_post()` extrinsic.
/// See `fn move_post()` extrinsic if you want to move a post to another space.
pub space_id: Option<SpaceId>,

pub content: Option<Content>,
pub hidden: Option<bool>,
}

/// Post extension provides specific information necessary for different kinds
/// of posts such as regular posts, comments, and shared posts.
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(untagged))]
pub enum PostExtension {
RegularPost,
Comment(Comment),
SharedPost(PostId),
}

#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct Comment {
pub root_post_id: PostId,
pub parent_id: Option<PostId>,
}

impl Default for PostExtension {
fn default() -> Self {
PostExtension::RegularPost
}
}
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ shivarthu-runtime-api={version = "4.0.0-dev", default-features = false, path = "
sortition-sum-game = {default-features = false, path ="../pallets/sortition-sum-game"}
election={ default-features = false, path="../pallets/election"}
election-runtime-api={default-features=false, path="../pallets/election/election-runtime-api"}
project-submission = {default-features=false, path="../pallets/project-submission"}
posts = {default-features=false, path="../pallets/posts"}
schelling-game-shared = {default-features=false, path="../pallets/schelling-game-shared"}
profile-validation = {default-features=false, path="../pallets/profile-validation"}
profile-validation-runtime-api = {default-features=false, path="../pallets/profile-validation/profile-validation-runtime-api"}
Expand Down Expand Up @@ -94,7 +94,7 @@ std = [
"pallet-template/std",
"shivarthu-runtime-api/std",
"sortition-sum-game/std",
"project-submission/std",
"posts/std",
"schelling-game-shared/std",
"profile-validation/std",
"profile-validation-runtime-api/std"
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use election;
pub use pallet_template;
pub use profile_validation;
pub use schelling_game_shared;
pub use project_submission;
pub use posts;
pub use sortition_sum_game;

/// An index to a block.
Expand Down Expand Up @@ -301,7 +301,7 @@ impl election::Config for Runtime {
type CandidacyBond = CandidacyBond;
}

impl project_submission::Config for Runtime {
impl posts::Config for Runtime {
type Event = Event;
}

Expand Down Expand Up @@ -339,7 +339,7 @@ construct_runtime!(
TemplateModule: pallet_template,
SortitionSumGame: sortition_sum_game,
Election: election,
ProjectSubmission: project_submission,
Posts: posts,
SchellingGameShared: schelling_game_shared,
ProfileValidation: profile_validation,

Expand Down

0 comments on commit addef3f

Please sign in to comment.