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
#![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 pallet_profiles::{Module as Profiles, SocialAccountById};
use pallet_utils::remove_from_vec;
pub mod rpc;
pub trait Trait: system::Trait
+ pallet_utils::Trait
+ pallet_profiles::Trait
{
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
type BeforeAccountFollowed: BeforeAccountFollowed<Self>;
type BeforeAccountUnfollowed: BeforeAccountUnfollowed<Self>;
}
decl_storage! {
trait Store for Module<T: Trait> as ProfileFollowsModule {
pub AccountFollowers get(fn account_followers):
map hasher(blake2_128_concat) T::AccountId => Vec<T::AccountId>;
pub AccountFollowedByAccount get(fn account_followed_by_account):
map hasher(blake2_128_concat) (T::AccountId, T::AccountId) => bool;
pub AccountsFollowedByAccount get(fn accounts_followed_by_account):
map hasher(blake2_128_concat) T::AccountId => Vec<T::AccountId>;
}
}
decl_event!(
pub enum Event<T> where
<T as system::Trait>::AccountId,
{
AccountFollowed( AccountId, AccountId),
AccountUnfollowed( AccountId, AccountId),
}
);
decl_error! {
pub enum Error for Module<T: Trait> {
FollowerAccountNotFound,
FollowedAccountNotFound,
AccountCannotFollowItself,
AccountCannotUnfollowItself,
AlreadyAccountFollower,
NotAccountFollower,
}
}
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(4, 4)]
pub fn follow_account(origin, account: T::AccountId) -> DispatchResult {
let follower = ensure_signed(origin)?;
ensure!(follower != account, Error::<T>::AccountCannotFollowItself);
ensure!(!<AccountFollowedByAccount<T>>::contains_key((follower.clone(), account.clone())),
Error::<T>::AlreadyAccountFollower);
let mut follower_account = Profiles::get_or_new_social_account(follower.clone());
let mut followed_account = Profiles::get_or_new_social_account(account.clone());
follower_account.inc_following_accounts();
followed_account.inc_followers();
T::BeforeAccountFollowed::before_account_followed(
follower.clone(), follower_account.reputation, account.clone())?;
<SocialAccountById<T>>::insert(follower.clone(), follower_account);
<SocialAccountById<T>>::insert(account.clone(), followed_account);
<AccountsFollowedByAccount<T>>::mutate(follower.clone(), |ids| ids.push(account.clone()));
<AccountFollowers<T>>::mutate(account.clone(), |ids| ids.push(follower.clone()));
<AccountFollowedByAccount<T>>::insert((follower.clone(), account.clone()), true);
Self::deposit_event(RawEvent::AccountFollowed(follower, account));
Ok(())
}
#[weight = 10_000 + T::DbWeight::get().reads_writes(4, 4)]
pub fn unfollow_account(origin, account: T::AccountId) -> DispatchResult {
let follower = ensure_signed(origin)?;
ensure!(follower != account, Error::<T>::AccountCannotUnfollowItself);
ensure!(<AccountFollowedByAccount<T>>::contains_key((follower.clone(), account.clone())), Error::<T>::NotAccountFollower);
let mut follower_account = Profiles::social_account_by_id(follower.clone()).ok_or(Error::<T>::FollowerAccountNotFound)?;
let mut followed_account = Profiles::social_account_by_id(account.clone()).ok_or(Error::<T>::FollowedAccountNotFound)?;
follower_account.dec_following_accounts();
followed_account.dec_followers();
T::BeforeAccountUnfollowed::before_account_unfollowed(follower.clone(), account.clone())?;
<SocialAccountById<T>>::insert(follower.clone(), follower_account);
<SocialAccountById<T>>::insert(account.clone(), followed_account);
<AccountsFollowedByAccount<T>>::mutate(follower.clone(), |account_ids| remove_from_vec(account_ids, account.clone()));
<AccountFollowers<T>>::mutate(account.clone(), |account_ids| remove_from_vec(account_ids, follower.clone()));
<AccountFollowedByAccount<T>>::remove((follower.clone(), account.clone()));
Self::deposit_event(RawEvent::AccountUnfollowed(follower, account));
Ok(())
}
}
}
pub trait BeforeAccountFollowed<T: Trait> {
fn before_account_followed(follower: T::AccountId, follower_reputation: u32, following: T::AccountId) -> DispatchResult;
}
impl<T: Trait> BeforeAccountFollowed<T> for () {
fn before_account_followed(_follower: T::AccountId, _follower_reputation: u32, _following: T::AccountId) -> DispatchResult {
Ok(())
}
}
pub trait BeforeAccountUnfollowed<T: Trait> {
fn before_account_unfollowed(follower: T::AccountId, following: T::AccountId) -> DispatchResult;
}
impl<T: Trait> BeforeAccountUnfollowed<T> for () {
fn before_account_unfollowed(_follower: T::AccountId, _following: T::AccountId) -> DispatchResult {
Ok(())
}
}