Skip to content

Commit

Permalink
Merge pull request #2352 from conectado/drop-thread-archival-status
Browse files Browse the repository at this point in the history
Drop archival status from forum thread
  • Loading branch information
shamil-gadelshin authored Apr 20, 2021
2 parents f900972 + e9b5822 commit 40c9c80
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 554 deletions.
92 changes: 0 additions & 92 deletions runtime-modules/forum/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,6 @@ benchmarks! {
category_id,
title_hash: T::calculate_hash(&title),
author_id: forum_user_id.saturated_into(),
archived: false,
poll: poll.clone(),
cleanup_pay_off: T::ThreadDeposit::get(),
number_of_posts: 1,
Expand Down Expand Up @@ -860,81 +859,6 @@ benchmarks! {
);
}

update_thread_archival_status_lead {
let i in 1 .. (T::MaxCategoryDepth::get() + 1) as u32;

let forum_user_id = 0;
let caller_id =
insert_a_leader::<T>(forum_user_id);
let text = vec![1u8].repeat(MAX_BYTES as usize);

// Generate categories tree
let (category_id, _) = generate_categories_tree::<T>(caller_id.clone(), i, None);

// Create thread
let thread_id = create_new_thread::<T>(caller_id.clone(), forum_user_id.saturated_into(), category_id, text.clone(), text.clone(), None);
let mut thread = Module::<T>::thread_by_id(category_id, thread_id);
let new_archival_status = true;

}: update_thread_archival_status(RawOrigin::Signed(caller_id), PrivilegedActor::Lead, category_id, thread_id, new_archival_status)
verify {
thread.archived = new_archival_status;

assert_eq!(Module::<T>::thread_by_id(category_id, thread_id), thread);

assert_last_event::<T>(
RawEvent::ThreadUpdated(
thread_id,
new_archival_status,
PrivilegedActor::Lead,
category_id
).into()
);
}

update_thread_archival_status_moderator {
let i in 1 .. (T::MaxCategoryDepth::get() + 1) as u32;

let forum_user_id = 0;
let caller_id =
insert_a_leader::<T>(forum_user_id);
let text = vec![1u8].repeat(MAX_BYTES as usize);

// Generate categories tree
let (category_id, _) = generate_categories_tree::<T>(caller_id.clone(), i, None);

// Create thread
let thread_id = create_new_thread::<T>(caller_id.clone(), forum_user_id.saturated_into(), category_id, text.clone(), text.clone(), None);
let mut thread = Module::<T>::thread_by_id(category_id, thread_id);
let new_archival_status = true;

let moderator_id = ModeratorId::<T>::from(forum_user_id.try_into().unwrap());

// Set up category membership of moderator.
Module::<T>::update_category_membership_of_moderator(
RawOrigin::Signed(caller_id.clone()).into(),
moderator_id,
category_id,
true,
)
.unwrap();

}: update_thread_archival_status(RawOrigin::Signed(caller_id), PrivilegedActor::Moderator(moderator_id), category_id, thread_id, new_archival_status)
verify {
thread.archived = new_archival_status;

assert_eq!(Module::<T>::thread_by_id(category_id, thread_id), thread);

assert_last_event::<T>(
RawEvent::ThreadUpdated(
thread_id,
new_archival_status,
PrivilegedActor::Moderator(moderator_id),
category_id
).into()
);
}

delete_thread {
let i in 1 .. (T::MaxCategoryDepth::get() + 1) as u32;
let hide = false;
Expand Down Expand Up @@ -1818,22 +1742,6 @@ mod tests {
});
}

#[test]
fn test_update_thread_archival_status_lead() {
with_test_externalities(|| {
assert_ok!(test_benchmark_update_thread_archival_status_lead::<Runtime>());
});
}

#[test]
fn test_update_thread_archival_status_moderator() {
with_test_externalities(|| {
assert_ok!(test_benchmark_update_thread_archival_status_moderator::<
Runtime,
>());
});
}

#[test]
fn test_delete_thread() {
with_test_externalities(|| {
Expand Down
80 changes: 1 addition & 79 deletions runtime-modules/forum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,12 @@ pub trait WeightInfo {
fn delete_category_moderator(i: u32) -> Weight;
fn create_thread(j: u32, k: u32, i: u32) -> Weight;
fn edit_thread_title(i: u32, j: u32) -> Weight;
fn update_thread_archival_status_lead(i: u32) -> Weight;
fn update_thread_archival_status_moderator(i: u32) -> Weight;
fn delete_thread(i: u32) -> Weight;
fn move_thread_to_category_lead(i: u32) -> Weight;
fn move_thread_to_category_moderator(i: u32) -> Weight;
fn vote_on_poll(i: u32, j: u32) -> Weight;
fn moderate_thread_lead(i: u32, k: u32) -> Weight;
fn moderate_thread_moderator(i: u32, j: u32, k: u32) -> Weight;
fn moderate_thread_moderator(i: u32, k: u32) -> Weight;
fn add_post(i: u32, j: u32) -> Weight;
fn react_post(i: u32) -> Weight;
fn edit_post_text(i: u32, j: u32) -> Weight;
Expand Down Expand Up @@ -242,9 +240,6 @@ pub struct Thread<ForumUserId, CategoryId, Moment, Hash, Balance> {
/// Author of post.
pub author_id: ForumUserId,

/// Whether thread is archived.
pub archived: bool,

/// poll description.
pub poll: Option<Poll<Moment, Hash>>,

Expand Down Expand Up @@ -340,9 +335,6 @@ decl_error! {
/// Thread not being updated.
ThreadNotBeingUpdated,

/// Thread is immutable, i.e. archived.
ThreadImmutable,

/// Not enough balance to create thread
InsufficientBalanceForThreadCreation,

Expand Down Expand Up @@ -805,7 +797,6 @@ decl_module! {
category_id,
title_hash: T::calculate_hash(&title),
author_id: forum_user_id,
archived: false,
poll: poll.clone(),
cleanup_pay_off: T::ThreadDeposit::get(),
number_of_posts: 0,
Expand Down Expand Up @@ -890,53 +881,6 @@ decl_module! {
Ok(())
}

/// Update thread archival status
///
/// <weight>
///
/// ## Weight
/// `O (W)` where:
/// - `W` is the category depth
/// - DB:
/// - O(W)
/// # </weight>
#[weight = WeightInfoForum::<T>::update_thread_archival_status_lead(
T::MaxCategoryDepth::get() as u32,
).max(WeightInfoForum::<T>::update_thread_archival_status_moderator(
T::MaxCategoryDepth::get() as u32,
))]
fn update_thread_archival_status(origin, actor: PrivilegedActor<T>, category_id: T::CategoryId, thread_id: T::ThreadId, new_archival_status: bool) -> DispatchResult {
// Ensure data migration is done
Self::ensure_data_migration_done()?;

let account_id = ensure_signed(origin)?;

// Ensure actor can update category
let (_, thread) = Self::ensure_can_update_thread_archival_status(account_id, &actor, &category_id, &thread_id)?;

// No change, invalid transaction
if new_archival_status == thread.archived {
return Err(Error::<T>::ThreadNotBeingUpdated.into());
}

//
// == MUTATION SAFE ==
//

// Mutate thread, and set possible new change parameters
<ThreadById<T>>::mutate(thread.category_id, thread_id, |c| c.archived = new_archival_status);

// Generate event
Self::deposit_event(RawEvent::ThreadUpdated(
thread_id,
new_archival_status,
actor,
category_id
));

Ok(())
}

/// Delete thread
///
/// <weight>
Expand Down Expand Up @@ -1121,7 +1065,6 @@ decl_module! {
).max(
WeightInfoForum::<T>::moderate_thread_moderator(
T::MaxCategoryDepth::get() as u32,
0,
rationale.len().saturated_into(),
)
)]
Expand Down Expand Up @@ -1692,33 +1635,12 @@ impl<T: Trait> Module<T> {
// Make sure thread exists
let thread = Self::ensure_thread_exists(category_id, thread_id)?;

if thread.archived {
return Err(Error::<T>::ThreadImmutable);
}

// and corresponding category is mutable
let category = Self::ensure_category_is_mutable(category_id)?;

Ok((category, thread))
}

fn ensure_can_update_thread_archival_status(
account_id: T::AccountId,
actor: &PrivilegedActor<T>,
category_id: &T::CategoryId,
thread_id: &T::ThreadId,
) -> Result<(Category<T::CategoryId, T::ThreadId, T::Hash>, ThreadOf<T>), Error<T>> {
// Check actor's role
Self::ensure_actor_role(&account_id, actor)?;

let (category, thread) = Self::ensure_thread_is_mutable(category_id, thread_id)?;

// Ensure actor can delete category
Self::ensure_can_moderate_category_path(actor, category_id)?;

Ok((category, thread))
}

fn ensure_thread_exists(
category_id: &T::CategoryId,
thread_id: &T::ThreadId,
Expand Down
39 changes: 1 addition & 38 deletions runtime-modules/forum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,6 @@ impl WeightInfo for () {
fn edit_thread_title(_: u32, _: u32) -> Weight {
0
}
fn update_thread_archival_status_lead(_: u32) -> Weight {
0
}
fn update_thread_archival_status_moderator(_: u32) -> Weight {
0
}
fn delete_thread(_: u32) -> Weight {
0
}
Expand All @@ -468,7 +462,7 @@ impl WeightInfo for () {
fn moderate_thread_lead(_: u32, _: u32) -> Weight {
0
}
fn moderate_thread_moderator(_: u32, _: u32, _: u32) -> Weight {
fn moderate_thread_moderator(_: u32, _: u32) -> Weight {
0
}
fn add_post(_: u32, _: u32) -> Weight {
Expand Down Expand Up @@ -876,37 +870,6 @@ pub fn move_thread_mock(
}
}

pub fn update_thread_archival_status_mock(
origin: OriginType,
actor: PrivilegedActor<Runtime>,
category_id: <Runtime as Trait>::CategoryId,
thread_id: <Runtime as Trait>::ThreadId,
new_archival_status: bool,
result: DispatchResult,
) {
assert_eq!(
TestForumModule::update_thread_archival_status(
mock_origin(origin),
actor.clone(),
category_id,
thread_id,
new_archival_status
),
result
);
if result.is_ok() {
assert_eq!(
System::events().last().unwrap().event,
TestEvent::forum_mod(RawEvent::ThreadUpdated(
thread_id,
new_archival_status,
actor,
category_id
))
);
}
}

pub fn create_post_mock(
origin: OriginType,
account_id: <Runtime as frame_system::Trait>::AccountId,
Expand Down
Loading

0 comments on commit 40c9c80

Please sign in to comment.