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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![cfg_attr(not(feature = "std"), no_std)]
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, ensure,
dispatch::DispatchResult,
traits::Get
};
use sp_std::prelude::*;
use frame_system::{self as system, ensure_signed};
use df_traits::{
SpaceFollowsProvider,
moderation::IsAccountBlocked,
};
use pallet_profiles::{Module as Profiles, SocialAccountById};
use pallet_spaces::{BeforeSpaceCreated, Module as Spaces, Space, SpaceById};
use pallet_utils::{Error as UtilsError, SpaceId, remove_from_vec};
pub mod rpc;
pub trait Trait: system::Trait
+ pallet_utils::Trait
+ pallet_spaces::Trait
+ pallet_profiles::Trait
{
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type BeforeSpaceFollowed: BeforeSpaceFollowed<Self>;
type BeforeSpaceUnfollowed: BeforeSpaceUnfollowed<Self>;
}
decl_error! {
pub enum Error for Module<T: Trait> {
SocialAccountNotFound,
AlreadySpaceFollower,
NotSpaceFollower,
CannotFollowHiddenSpace,
}
}
decl_storage! {
trait Store for Module<T: Trait> as SpaceFollowsModule {
pub SpaceFollowers get(fn space_followers):
map hasher(twox_64_concat) SpaceId => Vec<T::AccountId>;
pub SpaceFollowedByAccount get(fn space_followed_by_account):
map hasher(blake2_128_concat) (T::AccountId, SpaceId) => bool;
pub SpacesFollowedByAccount get(fn spaces_followed_by_account):
map hasher(blake2_128_concat) T::AccountId => Vec<SpaceId>;
}
}
decl_event!(
pub enum Event<T> where
<T as system::Trait>::AccountId,
{
SpaceFollowed( AccountId, SpaceId),
SpaceUnfollowed( AccountId, SpaceId),
}
);
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
#[weight = 10_000 + T::DbWeight::get().reads_writes(5, 5)]
pub fn follow_space(origin, space_id: SpaceId) -> DispatchResult {
let follower = ensure_signed(origin)?;
ensure!(!Self::space_followed_by_account((follower.clone(), space_id)), Error::<T>::AlreadySpaceFollower);
let space = &mut Spaces::require_space(space_id)?;
ensure!(!space.hidden, Error::<T>::CannotFollowHiddenSpace);
ensure!(T::IsAccountBlocked::is_allowed_account(follower.clone(), space.id), UtilsError::<T>::AccountIsBlocked);
Self::add_space_follower(follower, space)?;
<SpaceById<T>>::insert(space_id, space);
Ok(())
}
#[weight = 10_000 + T::DbWeight::get().reads_writes(5, 5)]
pub fn unfollow_space(origin, space_id: SpaceId) -> DispatchResult {
let follower = ensure_signed(origin)?;
ensure!(Self::space_followed_by_account((follower.clone(), space_id)), Error::<T>::NotSpaceFollower);
Self::unfollow_space_by_account(follower, space_id)
}
}
}
impl<T: Trait> Module<T> {
fn add_space_follower(follower: T::AccountId, space: &mut Space<T>) -> DispatchResult {
space.inc_followers();
let mut social_account = Profiles::get_or_new_social_account(follower.clone());
social_account.inc_following_spaces();
T::BeforeSpaceFollowed::before_space_followed(
follower.clone(), social_account.reputation, space)?;
let space_id = space.id;
<SpaceFollowers<T>>::mutate(space_id, |followers| followers.push(follower.clone()));
<SpaceFollowedByAccount<T>>::insert((follower.clone(), space_id), true);
<SpacesFollowedByAccount<T>>::mutate(follower.clone(), |space_ids| space_ids.push(space_id));
<SocialAccountById<T>>::insert(follower.clone(), social_account);
Self::deposit_event(RawEvent::SpaceFollowed(follower, space_id));
Ok(())
}
pub fn unfollow_space_by_account(follower: T::AccountId, space_id: SpaceId) -> DispatchResult {
let space = &mut Spaces::require_space(space_id)?;
space.dec_followers();
let mut social_account = Profiles::social_account_by_id(follower.clone()).ok_or(Error::<T>::SocialAccountNotFound)?;
social_account.dec_following_spaces();
T::BeforeSpaceUnfollowed::before_space_unfollowed(follower.clone(), space)?;
<SpacesFollowedByAccount<T>>::mutate(follower.clone(), |space_ids| remove_from_vec(space_ids, space_id));
<SpaceFollowers<T>>::mutate(space_id, |account_ids| remove_from_vec(account_ids, follower.clone()));
<SpaceFollowedByAccount<T>>::remove((follower.clone(), space_id));
<SocialAccountById<T>>::insert(follower.clone(), social_account);
<SpaceById<T>>::insert(space_id, space);
Self::deposit_event(RawEvent::SpaceUnfollowed(follower, space_id));
Ok(())
}
}
impl<T: Trait> SpaceFollowsProvider for Module<T> {
type AccountId = T::AccountId;
fn is_space_follower(account: Self::AccountId, space_id: SpaceId) -> bool {
Module::<T>::space_followed_by_account((account, space_id))
}
}
impl<T: Trait> BeforeSpaceCreated<T> for Module<T> {
fn before_space_created(creator: T::AccountId, space: &mut Space<T>) -> DispatchResult {
Module::<T>::add_space_follower(creator, space)
}
}
pub trait BeforeSpaceFollowed<T: Trait> {
fn before_space_followed(follower: T::AccountId, follower_reputation: u32, space: &mut Space<T>) -> DispatchResult;
}
impl<T: Trait> BeforeSpaceFollowed<T> for () {
fn before_space_followed(_follower: T::AccountId, _follower_reputation: u32, _space: &mut Space<T>) -> DispatchResult {
Ok(())
}
}
pub trait BeforeSpaceUnfollowed<T: Trait> {
fn before_space_unfollowed(follower: T::AccountId, space: &mut Space<T>) -> DispatchResult;
}
impl<T: Trait> BeforeSpaceUnfollowed<T> for () {
fn before_space_unfollowed(_follower: T::AccountId, _space: &mut Space<T>) -> DispatchResult {
Ok(())
}
}