Skip to content
Draft
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
24 changes: 24 additions & 0 deletions Cargo.lock

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

59 changes: 59 additions & 0 deletions pallets/moderation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = 'pallet-moderation'
version = "0.1.8"
authors = ["DappForce <[email protected]>"]
edition = "2018"
license = "GPL-3.0-only"
homepage = "https://subsocial.network"
repository = "https://github.com/dappforce/subsocial-parachain"
description = 'Subsocial pallet for content moderation'
keywords = ["blockchain", "cryptocurrency", "social-network", "news-feed", "marketplace"]
categories = ["cryptography::cryptocurrencies"]

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }

subsocial-support = { path = "../support", default-features = false }
pallet-permissions = { default-features = false, path = '../permissions' }
pallet-posts = { default-features = false, path = '../posts' }
pallet-roles = { default-features = false, path = '../roles' }
pallet-space-follows = { default-features = false, path = '../space-follows' }
pallet-spaces = { default-features = false, path = '../spaces' }

frame-benchmarking = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }

[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37", default-features = false }

[features]
default = ["std"]
runtime-benchmarks = ["frame-benchmarking"]
std = [
"codec/std",
"scale-info/std",
"pallet-balances/std",
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"sp-runtime/std",
"sp-std/std",
"subsocial-support/std",
"pallet-permissions/std",
"pallet-posts/std",
"pallet-roles/std",
"pallet-space-follows/std",
"pallet-spaces/std",

]
try-runtime = ["frame-support/try-runtime"]
173 changes: 173 additions & 0 deletions pallets/moderation/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use crate::*;

use frame_support::dispatch::DispatchError;
use pallet_posts::Pallet as Posts;
use pallet_spaces::Pallet as Spaces;
use pallet_spaces::types::Space;
use pallet_space_follows::Pallet as SpaceFollows;
use subsocial_support::{Content, ensure_content_is_some, new_who_and_when, PostId, SpaceId};
use subsocial_support::traits::{IsAccountBlocked, IsContentBlocked, IsPostBlocked, IsSpaceBlocked};
use crate::pallet::{Config, EntityId, EntityStatus, Error, Pallet, Report, ReportId, SpaceModerationSettings, StatusByEntityInSpace, SuggestedStatus};

impl<T: Config> Pallet<T> {
pub fn require_report(report_id: ReportId) -> Result<Report<T>, DispatchError> {
Ok(Self::report_by_id(report_id).ok_or(Error::<T>::ReportNotFound)?)
}

/// Get entity space_id if it exists.
/// Content and Account has no scope, consider check with `if let Some`
fn get_entity_scope(entity: &EntityId<T::AccountId>) -> Result<Option<SpaceId>, DispatchError> {
match entity {
EntityId::Content(content) => {
ensure_content_is_some(content).map(|_| None)
},
EntityId::Account(_) => Ok(None),
EntityId::Space(space_id) => {
// TODO: refactor
// let space = Spaces::<T>::require_space(*space_id)?;
// let root_space_id = space.try_get_parent()?;
//
// Ok(Some(root_space_id))
Ok(None)
},
EntityId::Post(post_id) => {
let post = Posts::<T>::require_post(*post_id)?;
let space_id = post.get_space()?.id;

Ok(Some(space_id))
},
}
}

#[allow(dead_code)]
// fixme: do we need this?
fn ensure_entity_exists(entity: &EntityId<T::AccountId>) -> DispatchResult {
match entity {
EntityId::Content(content) => ensure_content_is_some(content),
EntityId::Account(_) => Ok(()),
EntityId::Space(space_id) => Spaces::<T>::ensure_space_exists(*space_id),
EntityId::Post(post_id) => Posts::<T>::ensure_post_exists(*post_id),
}.map_err(|_| Error::<T>::EntityNotFound.into())
}

pub(crate) fn block_entity_in_scope(entity: &EntityId<T::AccountId>, scope: SpaceId) -> DispatchResult {
// TODO: update counters, when entity is moved
// TODO: think, what and where we should change something if entity is moved
match entity {
EntityId::Content(_) => (),
EntityId::Account(account_id)
=> SpaceFollows::<T>::remove_space_follower(account_id.clone(), scope)?,
EntityId::Space(space_id) => /*TODO:refactor*//*Spaces::<T>::try_move_space_to_root(*space_id)?*/{},
EntityId::Post(post_id) => Posts::<T>::delete_post_from_space(*post_id)?,
}
StatusByEntityInSpace::<T>::insert(entity, scope, EntityStatus::Blocked);
Ok(())
}

pub(crate) fn ensure_account_status_manager(who: T::AccountId, space: &Space<T>) -> DispatchResult {
Spaces::<T>::ensure_account_has_space_permission(
who,
&space,
pallet_permissions::SpacePermission::UpdateEntityStatus,
Error::<T>::NoPermissionToUpdateEntityStatus.into(),
)
}

pub(crate) fn ensure_entity_in_scope(entity: &EntityId<T::AccountId>, scope: SpaceId) -> DispatchResult {
if let Some(entity_scope) = Self::get_entity_scope(entity)? {
ensure!(entity_scope == scope, Error::<T>::EntityNotInScope);
}
Ok(())
}

pub fn default_autoblock_threshold_as_settings() -> SpaceModerationSettings {
SpaceModerationSettings {
autoblock_threshold: Some(T::DefaultAutoblockThreshold::get())
}
}
}

impl<T: Config> Report<T> {
pub fn new(
id: ReportId,
created_by: T::AccountId,
reported_entity: EntityId<T::AccountId>,
scope: SpaceId,
reason: Content
) -> Self {
Self {
id,
created: new_who_and_when::<T>(created_by),
reported_entity,
reported_within: scope,
reason
}
}
}

impl<T: Config> SuggestedStatus<T> {
pub fn new(who: T::AccountId, status: Option<EntityStatus>, report_id: Option<ReportId>) -> Self {
Self {
suggested: new_who_and_when::<T>(who),
status,
report_id
}
}
}

// TODO: maybe simplify using one common trait?
impl<T: Config> IsAccountBlocked<T::AccountId> for Pallet<T> {
fn is_blocked_account(account: T::AccountId, scope: SpaceId) -> bool {
let entity = EntityId::Account(account);

Self::status_by_entity_in_space(entity, scope) == Some(EntityStatus::Blocked)
}

fn is_allowed_account(account: T::AccountId, scope: SpaceId) -> bool {
let entity = EntityId::Account(account);

Self::status_by_entity_in_space(entity, scope) != Some(EntityStatus::Blocked)
}
}

impl<T: Config> IsSpaceBlocked for Pallet<T> {
fn is_blocked_space(space_id: SpaceId, scope: SpaceId) -> bool {
let entity = EntityId::Space(space_id);

Self::status_by_entity_in_space(entity, scope) == Some(EntityStatus::Blocked)
}

fn is_allowed_space(space_id: SpaceId, scope: SpaceId) -> bool {
let entity = EntityId::Space(space_id);

Self::status_by_entity_in_space(entity, scope) != Some(EntityStatus::Blocked)
}
}

impl<T: Config> IsPostBlocked<PostId> for Pallet<T> {
fn is_blocked_post(post_id: PostId, scope: SpaceId) -> bool {
let entity = EntityId::Post(post_id);

Self::status_by_entity_in_space(entity, scope) == Some(EntityStatus::Blocked)
}

fn is_allowed_post(post_id: PostId, scope: SpaceId) -> bool {
let entity = EntityId::Post(post_id);

Self::status_by_entity_in_space(entity, scope) != Some(EntityStatus::Blocked)
}
}

impl<T: Config> IsContentBlocked for Pallet<T> {
fn is_blocked_content(content: Content, scope: SpaceId) -> bool {
let entity = EntityId::Content(content);

Self::status_by_entity_in_space(entity, scope) == Some(EntityStatus::Blocked)
}

fn is_allowed_content(content: Content, scope: SpaceId) -> bool {
let entity = EntityId::Content(content);

Self::status_by_entity_in_space(entity, scope) != Some(EntityStatus::Blocked)
}
}
Loading