Browse Source

moved assets into update_parameters data struct & updated tests

ignazio-bovo 3 years ago
parent
commit
11af25e8ea

+ 22 - 17
runtime-modules/content/src/lib.rs

@@ -218,17 +218,22 @@ type ChannelCreationParameters<T> =
 /// Information about channel being updated.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Debug)]
-pub struct ChannelUpdateParametersRecord<StorageAssets, AccountId> {
+pub struct ChannelUpdateParametersRecord<StorageAssets, AccountId, DataObjectId: Ord> {
     /// Asset collection for the channel, referenced by metadata    
-    assets: Option<StorageAssets>,
+    assets_to_upload: Option<StorageAssets>,
     /// If set, metadata update for the channel.
     new_meta: Option<Vec<u8>>,
     /// If set, updates the reward account of the channel
     reward_account: Option<Option<AccountId>>,
+    /// assets to be removed from channel
+    assets_to_remove: BTreeSet<DataObjectId>,
 }
 
-type ChannelUpdateParameters<T> =
-    ChannelUpdateParametersRecord<StorageAssets<T>, <T as frame_system::Trait>::AccountId>;
+type ChannelUpdateParameters<T> = ChannelUpdateParametersRecord<
+    StorageAssets<T>,
+    <T as frame_system::Trait>::AccountId,
+    DataObjectId<T>,
+>;
 
 /// A category that videos can belong to.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
@@ -282,14 +287,16 @@ type VideoCreationParameters<T> = VideoCreationParametersRecord<StorageAssets<T>
 /// Information about the video being updated
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Default, Clone, PartialEq, Eq, Debug)]
-pub struct VideoUpdateParametersRecord<StorageAssets> {
+pub struct VideoUpdateParametersRecord<StorageAssets, DataObjectId: Ord> {
     /// Assets referenced by metadata
-    assets: Option<StorageAssets>,
+    assets_to_upload: Option<StorageAssets>,
     /// If set, metadata update for the video.
     new_meta: Option<Vec<u8>>,
+    /// video assets to be removed from channel
+    assets_to_remove: BTreeSet<DataObjectId>,
 }
 
-type VideoUpdateParameters<T> = VideoUpdateParametersRecord<StorageAssets<T>>;
+type VideoUpdateParameters<T> = VideoUpdateParametersRecord<StorageAssets<T>, DataObjectId<T>>;
 
 /// A video which belongs to a channel. A video may be part of a series or playlist.
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
@@ -676,7 +683,6 @@ decl_module! {
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
             params: ChannelUpdateParameters<T>,
-            assets: BTreeSet<DataObjectId<T>>,
         ) {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
@@ -687,10 +693,10 @@ decl_module! {
                 &channel.owner,
             )?;
 
-            Self::remove_assets_from_storage(&assets, &channel_id, &channel.deletion_prize_source_account_id)?;
+            Self::remove_assets_from_storage(&params.assets_to_remove, &channel_id, &channel.deletion_prize_source_account_id)?;
 
             // atomically upload to storage and return the # of uploaded assets
-            if let Some(upload_assets) = params.assets.as_ref() {
+            if let Some(upload_assets) = params.assets_to_upload.as_ref() {
                 Self::upload_assets_to_storage(
                     upload_assets,
                     &channel_id,
@@ -721,7 +727,7 @@ decl_module! {
             origin,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             channel_id: T::ChannelId,
-            assets: BTreeSet<DataObjectId<T>>,
+            assets_to_remove: BTreeSet<DataObjectId<T>>,
         ) -> DispatchResult {
             // check that channel exists
             let channel = Self::ensure_channel_exists(&channel_id)?;
@@ -737,7 +743,7 @@ decl_module! {
             ensure!(channel.num_videos == 0, Error::<T>::ChannelContainsVideos);
 
             // remove specified assets from storage
-            Self::remove_assets_from_storage(&assets, &channel_id, &channel.deletion_prize_source_account_id)?;
+            Self::remove_assets_from_storage(&assets_to_remove, &channel_id, &channel.deletion_prize_source_account_id)?;
 
             // delete channel dynamic bag
             let dyn_bag = DynamicBagIdType::<T::MemberId, T::ChannelId>::Channel(channel_id);
@@ -945,7 +951,6 @@ decl_module! {
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
             params: VideoUpdateParameters<T>,
-            assets: BTreeSet<DataObjectId<T>>,
         ) {
             // check that video exists, retrieve corresponding channel id.
             let video = Self::ensure_video_exists(&video_id)?;
@@ -960,10 +965,10 @@ decl_module! {
             )?;
 
             // remove specified assets from channel bag in storage
-            Self::remove_assets_from_storage(&assets, &channel_id, &channel.deletion_prize_source_account_id)?;
+            Self::remove_assets_from_storage(&params.assets_to_remove, &channel_id, &channel.deletion_prize_source_account_id)?;
 
             // atomically upload to storage and return the # of uploaded assets
-            if let Some(upload_assets) = params.assets.as_ref() {
+            if let Some(upload_assets) = params.assets_to_upload.as_ref() {
                 Self::upload_assets_to_storage(
                     upload_assets,
                     &channel_id,
@@ -983,7 +988,7 @@ decl_module! {
             origin,
             actor: ContentActor<T::CuratorGroupId, T::CuratorId, T::MemberId>,
             video_id: T::VideoId,
-            assets: BTreeSet<DataObjectId<T>>,
+            assets_to_remove: BTreeSet<DataObjectId<T>>,
         ) {
 
             // check that video exists
@@ -1005,7 +1010,7 @@ decl_module! {
             Self::ensure_video_can_be_removed(&video)?;
 
             // remove specified assets from channel bag in storage
-            Self::remove_assets_from_storage(&assets, &channel_id, &channel.deletion_prize_source_account_id)?;
+            Self::remove_assets_from_storage(&assets_to_remove, &channel_id, &channel.deletion_prize_source_account_id)?;
 
             //
             // == MUTATION SAFE ==

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

@@ -120,7 +120,7 @@ fn successful_channel_assets_deletion() {
         );
 
         // delete assets
-        let assets_to_delete = [0u64, 1u64].iter().map(|&x| x).collect::<BTreeSet<_>>();
+        let assets_to_remove = [0u64, 1u64].iter().map(|&x| x).collect::<BTreeSet<_>>();
 
         // delete channel assets
         assert_ok!(Content::update_channel(
@@ -128,11 +128,11 @@ fn successful_channel_assets_deletion() {
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id,
             ChannelUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
                 reward_account: None,
+                assets_to_remove: assets_to_remove,
             },
-            assets_to_delete,
         ));
     })
 }
@@ -201,11 +201,11 @@ fn succesful_channel_update() {
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id,
             ChannelUpdateParametersRecord {
-                assets: Some(second_batch),
+                assets_to_upload: Some(second_batch),
                 new_meta: Some(vec![]),
                 reward_account: None,
+                assets_to_remove: BTreeSet::new(),
             },
-            BTreeSet::new(),
             Ok(()),
         );
 
@@ -215,11 +215,11 @@ fn succesful_channel_update() {
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id,
             ChannelUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
                 reward_account: None,
+                assets_to_remove: first_batch_ids,
             },
-            first_batch_ids,
             Ok(()),
         );
     })
@@ -378,11 +378,11 @@ fn curator_owned_channels() {
             ContentActor::Curator(FIRST_CURATOR_GROUP_ID, FIRST_CURATOR_ID),
             channel_id,
             ChannelUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
                 reward_account: None,
+                assets_to_remove: BTreeSet::new(),
             },
-            BTreeSet::new(),
         ));
 
         // Lead can update curator owned channels
@@ -391,11 +391,11 @@ fn curator_owned_channels() {
             ContentActor::Lead,
             channel_id,
             ChannelUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
                 reward_account: None,
+                assets_to_remove: BTreeSet::new(),
             },
-            BTreeSet::new(),
         ));
     })
 }
@@ -494,11 +494,11 @@ fn member_owned_channels() {
             ContentActor::Member(FIRST_MEMBER_ID),
             channel_id_1,
             ChannelUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
                 reward_account: None,
+                assets_to_remove: BTreeSet::new(),
             },
-            BTreeSet::new(),
         ));
 
         assert_eq!(
@@ -515,9 +515,10 @@ fn member_owned_channels() {
                     num_videos: 0,
                 },
                 ChannelUpdateParametersRecord {
-                    assets: None,
+                    assets_to_upload: None,
                     new_meta: None,
                     reward_account: None,
+                    assets_to_remove: BTreeSet::new(),
                 }
             ))
         );
@@ -529,11 +530,11 @@ fn member_owned_channels() {
                 ContentActor::Member(FIRST_MEMBER_ID),
                 channel_id_2,
                 ChannelUpdateParametersRecord {
-                    assets: None,
+                    assets_to_upload: None,
                     new_meta: None,
                     reward_account: None,
+                    assets_to_remove: BTreeSet::new(),
                 },
-                BTreeSet::new(),
             ),
             Error::<Test>::ActorNotAuthorized
         );

+ 0 - 4
runtime-modules/content/src/tests/mock.rs

@@ -472,7 +472,6 @@ pub fn update_channel_mock(
     actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
     channel_id: ChannelId,
     params: ChannelUpdateParameters<Test>,
-    assets: BTreeSet<<Test as storage::Trait>::DataObjectId>,
     result: DispatchResult,
 ) {
     let channel_pre = ChannelById::<Test>::get(channel_id.clone());
@@ -483,7 +482,6 @@ pub fn update_channel_mock(
             actor.clone(),
             channel_id.clone(),
             params.clone(),
-            assets.clone(),
         ),
         result.clone(),
     );
@@ -573,7 +571,6 @@ pub fn update_video_mock(
     actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
     video_id: <Test as Trait>::VideoId,
     params: VideoUpdateParameters<Test>,
-    assets: BTreeSet<<Test as storage::Trait>::DataObjectId>,
     result: DispatchResult,
 ) {
     // let channel_id = Content::video_by_id(video_id.clone()).in_channel;
@@ -585,7 +582,6 @@ pub fn update_video_mock(
             actor.clone(),
             video_id.clone(),
             params.clone(),
-            assets.clone()
         ),
         result.clone(),
     );

+ 10 - 9
runtime-modules/content/src/tests/videos.rs

@@ -137,7 +137,7 @@ fn video_update_successful() {
 
         // add 1 asset
         let update_params = VideoUpdateParametersRecord {
-            assets: Some(StorageAssetsRecord {
+            assets_to_upload: Some(StorageAssetsRecord {
                 object_creation_list: vec![DataObjectCreationParameters {
                     size: 3,
                     ipfs_content_id: b"first".to_vec(),
@@ -145,6 +145,7 @@ fn video_update_successful() {
                 expected_data_size_fee: storage::DataObjectPerMegabyteFee::<Test>::get(),
             }),
             new_meta: None,
+            assets_to_remove: BTreeSet::new(),
         };
 
         let last_obj_id = Storage::<Test>::next_data_object_id();
@@ -154,7 +155,6 @@ fn video_update_successful() {
             ContentActor::Member(FIRST_MEMBER_ID),
             video_id,
             update_params,
-            BTreeSet::new(),
             Ok(()),
         );
 
@@ -164,10 +164,10 @@ fn video_update_successful() {
             ContentActor::Member(FIRST_MEMBER_ID),
             video_id,
             VideoUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
+                assets_to_remove: (first_obj_id..last_obj_id).collect::<BTreeSet<_>>(),
             },
-            (first_obj_id..last_obj_id).collect::<BTreeSet<_>>(),
             Ok(()),
         );
     })
@@ -214,10 +214,10 @@ fn member_can_create_videos() {
             ContentActor::Member(FIRST_MEMBER_ID),
             video_id,
             VideoUpdateParametersRecord {
-                assets: None,
+                assets_to_upload: None,
                 new_meta: None,
+                assets_to_remove: BTreeSet::new(),
             },
-            BTreeSet::new(),
         ));
 
         assert_eq!(
@@ -226,8 +226,9 @@ fn member_can_create_videos() {
                 ContentActor::Member(FIRST_MEMBER_ID),
                 video_id,
                 VideoUpdateParametersRecord {
-                    assets: None,
+                    assets_to_upload: None,
                     new_meta: None,
+                    assets_to_remove: BTreeSet::new(),
                 }
             ))
         );
@@ -253,10 +254,10 @@ fn member_can_create_videos() {
                 ContentActor::Member(SECOND_MEMBER_ID),
                 video_id,
                 VideoUpdateParametersRecord {
-                    assets: None,
+                    assets_to_upload: None,
                     new_meta: None,
+                    assets_to_remove: BTreeSet::new(),
                 },
-                BTreeSet::new(),
             ),
             Error::<Test>::ActorNotAuthorized
         );