1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use frame_support::{decl_module, decl_storage};
use sp_runtime::RuntimeDebug;
use sp_std::prelude::Vec;
use frame_system::{self as system};

use pallet_posts::{Post, PostUpdate, AfterPostUpdated};
use pallet_utils::{WhoAndWhen, PostId};

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct PostHistoryRecord<T: Trait> {
    pub edited: WhoAndWhen<T>,
    pub old_data: PostUpdate,
}

/// The pallet's configuration trait.
pub trait Trait: system::Trait
    + pallet_utils::Trait
    + pallet_posts::Trait
{}

// This pallet's storage items.
decl_storage! {
    trait Store for Module<T: Trait> as PostHistoryModule {
        pub EditHistory get(fn edit_history):
            map hasher(twox_64_concat) PostId => Vec<PostHistoryRecord<T>>;
    }
}

decl_module! {
  pub struct Module<T: Trait> for enum Call where origin: T::Origin {}
}

impl<T: Trait> PostHistoryRecord<T> {
    fn new(updated_by: T::AccountId, old_data: PostUpdate) -> Self {
        PostHistoryRecord {
            edited: WhoAndWhen::<T>::new(updated_by),
            old_data
        }
    }
}

impl<T: Trait> AfterPostUpdated<T> for Module<T> {
    fn after_post_updated(sender: T::AccountId, post: &Post<T>, old_data: PostUpdate) {
        <EditHistory<T>>::mutate(post.id, |ids|
            ids.push(PostHistoryRecord::<T>::new(sender, old_data)));
    }
}