Просмотр исходного кода

Merge pull request #2270 from iorveth/content_censorship_rework

Content: rework censorship logic
Mokhtar Naamani 3 лет назад
Родитель
Сommit
23605e5678

+ 28 - 70
runtime-modules/content/src/lib.rs

@@ -770,15 +770,20 @@ decl_module! {
         }
 
         #[weight = 10_000_000] // TODO: adjust weight
-        pub fn censor_channel(
+        pub fn update_channel_censorship_status(
             origin,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
+            is_censored: bool,
             rationale: Vec<u8>,
         ) {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
 
+            if channel.is_censored == is_censored {
+                return Ok(())
+            }
+
             ensure_actor_authorized_to_censor::<T>(
                 origin,
                 &actor,
@@ -791,44 +796,14 @@ decl_module! {
 
             let mut channel = channel;
 
-            channel.is_censored = true;
+            channel.is_censored = is_censored;
 
             // TODO: unset the reward account ? so no revenue can be earned for censored channels?
 
             // Update the channel
             ChannelById::<T>::insert(channel_id, channel);
 
-            Self::deposit_event(RawEvent::ChannelCensored(actor, channel_id, rationale));
-        }
-
-        #[weight = 10_000_000] // TODO: adjust weight
-        pub fn uncensor_channel(
-            origin,
-            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
-            channel_id: T::ChannelId,
-            rationale: Vec<u8>,
-        ) {
-            // check that channel exists
-            let channel = Self::ensure_channel_exists(&channel_id)?;
-
-            ensure_actor_authorized_to_censor::<T>(
-                origin,
-                &actor,
-                &channel.owner,
-            )?;
-
-            //
-            // == MUTATION SAFE ==
-            //
-
-            let mut channel = channel;
-
-            channel.is_censored = false;
-
-            // Update the channel
-            ChannelById::<T>::insert(channel_id, channel);
-
-            Self::deposit_event(RawEvent::ChannelUncensored(actor, channel_id, rationale));
+            Self::deposit_event(RawEvent::ChannelCensorshipStatusUpdated(actor, channel_id, is_censored, rationale));
         }
 
         #[weight = 10_000_000] // TODO: adjust weight
@@ -1221,45 +1196,19 @@ decl_module! {
         }
 
         #[weight = 10_000_000] // TODO: adjust weight
-        pub fn censor_video(
+        pub fn update_video_censorship_status(
             origin,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
+            is_censored: bool,
             rationale: Vec<u8>,
         ) {
             // check that video exists
             let video = Self::ensure_video_exists(&video_id)?;
 
-            ensure_actor_authorized_to_censor::<T>(
-                origin,
-                &actor,
-                // The channel owner will be..
-                &Self::channel_by_id(video.in_channel).owner,
-            )?;
-
-            //
-            // == MUTATION SAFE ==
-            //
-
-            let mut video = video;
-
-            video.is_censored = true;
-
-            // Update the video
-            VideoById::<T>::insert(video_id, video);
-
-            Self::deposit_event(RawEvent::VideoCensored(actor, video_id, rationale));
-        }
-
-        #[weight = 10_000_000] // TODO: adjust weight
-        pub fn uncensor_video(
-            origin,
-            actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
-            video_id: T::VideoId,
-            rationale: Vec<u8>
-        ) {
-            // check that video exists
-            let video = Self::ensure_video_exists(&video_id)?;
+            if video.is_censored == is_censored {
+                return Ok(())
+            }
 
             ensure_actor_authorized_to_censor::<T>(
                 origin,
@@ -1274,12 +1223,12 @@ decl_module! {
 
             let mut video = video;
 
-            video.is_censored = false;
+            video.is_censored = is_censored;
 
             // Update the video
             VideoById::<T>::insert(video_id, video);
 
-            Self::deposit_event(RawEvent::VideoUncensored(actor, video_id, rationale));
+            Self::deposit_event(RawEvent::VideoCensorshipStatusUpdated(actor, video_id, is_censored, rationale));
         }
 
         #[weight = 10_000_000] // TODO: adjust weight
@@ -1453,6 +1402,7 @@ decl_event!(
         ContentParameters = ContentParameters<T>,
         AccountId = <T as system::Trait>::AccountId,
         ContentId = ContentId<T>,
+        IsCensored = bool,
     {
         // Curators
         CuratorGroupCreated(CuratorGroupId),
@@ -1475,8 +1425,12 @@ decl_event!(
         ),
         ChannelAssetsRemoved(ContentActor, ChannelId, Vec<ContentId>),
 
-        ChannelCensored(ContentActor, ChannelId, Vec<u8> /* rationale */),
-        ChannelUncensored(ContentActor, ChannelId, Vec<u8> /* rationale */),
+        ChannelCensorshipStatusUpdated(
+            ContentActor,
+            ChannelId,
+            IsCensored,
+            Vec<u8>, /* rationale */
+        ),
 
         // Channel Ownership Transfers
         ChannelOwnershipTransferRequested(
@@ -1522,8 +1476,12 @@ decl_event!(
         ),
         VideoDeleted(ContentActor, VideoId),
 
-        VideoCensored(ContentActor, VideoId, Vec<u8> /* rationale */),
-        VideoUncensored(ContentActor, VideoId, Vec<u8> /* rationale */),
+        VideoCensorshipStatusUpdated(
+            ContentActor,
+            VideoId,
+            IsCensored,
+            Vec<u8>, /* rationale */
+        ),
 
         // Featured Videos
         FeaturedVideosSet(ContentActor, Vec<VideoId>),

+ 17 - 7
runtime-modules/content/src/tests/channels.rs

@@ -292,18 +292,21 @@ fn channel_censoring() {
         let group_id = curators::add_curator_to_new_group(FIRST_CURATOR_ID);
 
         // Curator can censor channels
-        assert_ok!(Content::censor_channel(
+        let is_censored = true;
+        assert_ok!(Content::update_channel_censorship_status(
             Origin::signed(FIRST_CURATOR_ORIGIN),
             ContentActor::Curator(group_id, FIRST_CURATOR_ID),
             channel_id,
+            is_censored,
             vec![]
         ));
 
         assert_eq!(
             System::events().last().unwrap().event,
-            MetaEvent::content(RawEvent::ChannelCensored(
+            MetaEvent::content(RawEvent::ChannelCensorshipStatusUpdated(
                 ContentActor::Curator(group_id, FIRST_CURATOR_ID),
                 channel_id,
+                is_censored,
                 vec![]
             ))
         );
@@ -313,18 +316,21 @@ fn channel_censoring() {
         assert!(channel.is_censored);
 
         // Curator can un-censor channels
-        assert_ok!(Content::uncensor_channel(
+        let is_censored = false;
+        assert_ok!(Content::update_channel_censorship_status(
             Origin::signed(FIRST_CURATOR_ORIGIN),
             ContentActor::Curator(group_id, FIRST_CURATOR_ID),
             channel_id,
+            is_censored,
             vec![]
         ));
 
         assert_eq!(
             System::events().last().unwrap().event,
-            MetaEvent::content(RawEvent::ChannelUncensored(
+            MetaEvent::content(RawEvent::ChannelCensorshipStatusUpdated(
                 ContentActor::Curator(group_id, FIRST_CURATOR_ID),
                 channel_id,
+                is_censored,
                 vec![]
             ))
         );
@@ -334,11 +340,13 @@ fn channel_censoring() {
         assert!(!channel.is_censored);
 
         // Member cannot censor channels
+        let is_censored = true;
         assert_err!(
-            Content::censor_channel(
+            Content::update_channel_censorship_status(
                 Origin::signed(FIRST_MEMBER_ORIGIN),
                 ContentActor::Member(FIRST_MEMBER_ID),
                 channel_id,
+                is_censored,
                 vec![]
             ),
             Error::<Test>::ActorNotAuthorized
@@ -359,20 +367,22 @@ fn channel_censoring() {
 
         // Curator cannot censor curator group channels
         assert_err!(
-            Content::censor_channel(
+            Content::update_channel_censorship_status(
                 Origin::signed(FIRST_CURATOR_ORIGIN),
                 ContentActor::Curator(group_id, FIRST_CURATOR_ID),
                 curator_channel_id,
+                is_censored,
                 vec![]
             ),
             Error::<Test>::CannotCensoreCuratorGroupOwnedChannels
         );
 
         // Lead can still censor curator group channels
-        assert_ok!(Content::censor_channel(
+        assert_ok!(Content::update_channel_censorship_status(
             Origin::signed(LEAD_ORIGIN),
             ContentActor::Lead,
             curator_channel_id,
+            is_censored,
             vec![]
         ));
     })

+ 12 - 5
runtime-modules/content/src/tests/videos.rs

@@ -160,18 +160,21 @@ fn curators_can_censor_videos() {
         let group_id = curators::add_curator_to_new_group(FIRST_CURATOR_ID);
 
         // Curator can censor videos
-        assert_ok!(Content::censor_video(
+        let is_censored = true;
+        assert_ok!(Content::update_video_censorship_status(
             Origin::signed(FIRST_CURATOR_ORIGIN),
             ContentActor::Curator(group_id, FIRST_CURATOR_ID),
             video_id,
+            is_censored,
             vec![]
         ));
 
         assert_eq!(
             System::events().last().unwrap().event,
-            MetaEvent::content(RawEvent::VideoCensored(
+            MetaEvent::content(RawEvent::VideoCensorshipStatusUpdated(
                 ContentActor::Curator(group_id, FIRST_CURATOR_ID),
                 video_id,
+                is_censored,
                 vec![]
             ))
         );
@@ -181,18 +184,21 @@ fn curators_can_censor_videos() {
         assert!(video.is_censored);
 
         // Curator can un-censor videos
-        assert_ok!(Content::uncensor_video(
+        let is_censored = false;
+        assert_ok!(Content::update_video_censorship_status(
             Origin::signed(FIRST_CURATOR_ORIGIN),
             ContentActor::Curator(group_id, FIRST_CURATOR_ID),
             video_id,
+            is_censored,
             vec![]
         ));
 
         assert_eq!(
             System::events().last().unwrap().event,
-            MetaEvent::content(RawEvent::VideoUncensored(
+            MetaEvent::content(RawEvent::VideoCensorshipStatusUpdated(
                 ContentActor::Curator(group_id, FIRST_CURATOR_ID),
                 video_id,
+                is_censored,
                 vec![]
             ))
         );
@@ -203,10 +209,11 @@ fn curators_can_censor_videos() {
 
         // Members cannot censor videos
         assert_err!(
-            Content::censor_video(
+            Content::update_video_censorship_status(
                 Origin::signed(FIRST_MEMBER_ORIGIN),
                 ContentActor::Member(FIRST_MEMBER_ORIGIN),
                 channel_id,
+                true,
                 vec![]
             ),
             Error::<Test>::ActorNotAuthorized