-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
executable file
Β·2448 lines (2169 loc) Β· 110 KB
/
lib.rs
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
ABOUT THIS CONTRACT...
This contract allows users to privately message each other the Geode Blockchain.
- Send messages privately to other accounts,
- Only those you choose can message you,
- Others can pay to get into your PAID inbox,
- powerful group and list messaging features
- maximum user control!
*/
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[ink::contract]
mod geode_messaging {
use ink::prelude::vec::Vec;
use ink::prelude::vec;
use ink::prelude::string::String;
use ink::storage::Mapping;
use ink::storage::StorageVec;
use ink::env::hash::{Sha2x256, HashOutput};
// PRELIMINARY STORAGE STRUCTURES >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct Settings {
user_account: AccountId,
username: Vec<u8>,
interests: Vec<u8>,
inbox_fee: Balance,
hide_from_search: bool,
last_update: u64,
}
impl Default for Settings {
fn default() -> Settings {
Settings {
user_account: AccountId::from([0x0; 32]),
username: <Vec<u8>>::default(),
interests: <Vec<u8>>::default(),
inbox_fee: Balance::default(),
hide_from_search: false,
last_update: u64::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct SettingsData {
interests: Vec<Vec<u8>>,
inbox_fee: Vec<Balance>,
last_update: Vec<u64>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct MessageDetails {
message_id: Hash,
from_acct: AccountId,
from_username: Vec<u8>,
to_acct: AccountId,
message: Vec<u8>,
file_url: Vec<u8>,
timestamp: u64,
}
impl Default for MessageDetails {
fn default() -> MessageDetails {
MessageDetails {
message_id: Hash::default(),
from_acct: AccountId::from([0x0; 32]),
from_username: <Vec<u8>>::default(),
to_acct: AccountId::from([0x0; 32]),
message: <Vec<u8>>::default(),
file_url: <Vec<u8>>::default(),
timestamp: u64::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct ListMessageDetails {
message_id: Hash,
from_acct: AccountId,
username: Vec<u8>,
to_list_id: Hash,
to_list_name: Vec<u8>,
message: Vec<u8>,
file_url: Vec<u8>,
timestamp: u64,
}
impl Default for ListMessageDetails {
fn default() -> ListMessageDetails {
ListMessageDetails {
message_id: Hash::default(),
from_acct: AccountId::from([0x0; 32]),
username: <Vec<u8>>::default(),
to_list_id: Hash::default(),
to_list_name: <Vec<u8>>::default(),
message: <Vec<u8>>::default(),
file_url: <Vec<u8>>::default(),
timestamp: u64::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct PaidMessageDetails {
message_id: Hash,
from_acct: AccountId,
from_username: Vec<u8>,
to_acct: AccountId,
message: Vec<u8>,
file_url: Vec<u8>,
timestamp: u64,
bid: Balance,
}
impl Default for PaidMessageDetails {
fn default() -> PaidMessageDetails {
PaidMessageDetails {
message_id: Hash::default(),
from_acct: AccountId::from([0x0; 32]),
from_username: <Vec<u8>>::default(),
to_acct: AccountId::from([0x0; 32]),
message: <Vec<u8>>::default(),
file_url: <Vec<u8>>::default(),
timestamp: u64::default(),
bid: Balance::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct AccountVector {
accountvector: Vec<AccountId>,
}
impl Default for AccountVector {
fn default() -> AccountVector {
AccountVector {
accountvector: <Vec<AccountId>>::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct HashVector {
hashvector: Vec<Hash>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct GroupDetails {
group_id: Hash,
group_name: Vec<u8>,
hide_from_search: bool,
description: Vec<u8>,
group_accounts: Vec<AccountId>,
subscribers: u128,
}
impl Default for GroupDetails {
fn default() -> GroupDetails {
GroupDetails {
group_id: Hash::default(),
group_name: <Vec<u8>>::default(),
hide_from_search: false,
description: <Vec<u8>>::default(),
group_accounts: <Vec<AccountId>>::default(),
subscribers: u128::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct GroupPublicDetails {
group_id: Hash,
group_name: Vec<u8>,
description: Vec<u8>,
subscribers: u128,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct OpenListDetails {
list_id: Hash,
owner: AccountId,
list_name: Vec<u8>,
hide_from_search: bool,
description: Vec<u8>,
list_accounts: u128,
}
impl Default for OpenListDetails {
fn default() -> OpenListDetails {
OpenListDetails {
list_id: Hash::default(),
owner: AccountId::from([0x0; 32]),
list_name: <Vec<u8>>::default(),
hide_from_search: false,
description: <Vec<u8>>::default(),
list_accounts: u128::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct OpenListPublicDetails {
list_id: Hash,
owner: AccountId,
list_name: Vec<u8>,
description: Vec<u8>,
list_accounts: u128,
}
impl Default for OpenListPublicDetails {
fn default() -> OpenListPublicDetails {
OpenListPublicDetails {
list_id: Hash::default(),
owner: AccountId::from([0x0; 32]),
list_name: <Vec<u8>>::default(),
description: <Vec<u8>>::default(),
list_accounts: u128::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct ConversationWithAccount {
allowed_account: AccountId,
username: Vec<u8>,
conversation: Vec<MessageDetails>
}
impl Default for ConversationWithAccount {
fn default() -> ConversationWithAccount {
ConversationWithAccount {
allowed_account: AccountId::from([0x0; 32]),
username: <Vec<u8>>::default(),
conversation: <Vec<MessageDetails>>::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct MessagesFromList {
allowed_list: Hash,
list_name: Vec<u8>,
list_messages: Vec<ListMessageDetails>
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct MyInboxLists {
lists: Vec<MessagesFromList>,
defunct_lists: Vec<Hash>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct GroupSearchResults {
search: Vec<Vec<u8>>,
groups: Vec<GroupPublicDetails>,
}
#[derive(Clone, Debug, PartialEq, Eq, Default)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct ListSearchResults {
search: Vec<Vec<u8>>,
lists: Vec<OpenListPublicDetails>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct AccountsAllowedAndBlocked {
allowed_accounts: Vec<AccountId>,
blocked_accounts: Vec<AccountId>,
}
impl Default for AccountsAllowedAndBlocked {
fn default() -> AccountsAllowedAndBlocked {
AccountsAllowedAndBlocked {
allowed_accounts: <Vec<AccountId>>::default(),
blocked_accounts: <Vec<AccountId>>::default(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std",derive(ink::storage::traits::StorageLayout,))]
pub struct RewardSettings {
reward_on: u8,
reward_root_set: u8,
reward_root: AccountId,
reward_interval: u128,
reward_amount: Balance,
reward_balance: Balance,
reward_payouts: Balance,
claim_counter: u128,
}
impl Default for RewardSettings {
fn default() -> RewardSettings {
RewardSettings {
reward_on: u8::default(),
reward_root_set: u8::default(),
reward_root: AccountId::from([0x0; 32]),
reward_interval: u128::default(),
reward_amount: Balance::default(),
reward_balance: Balance::default(),
reward_payouts: Balance::default(),
claim_counter: u128::default(),
}
}
}
// EVENT DEFINITIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#[ink(event)]
// when a user updates their settings
pub struct SettingsUpdated {
#[ink(topic)]
from: AccountId,
#[ink(topic)]
username: Vec<u8>,
#[ink(topic)]
interests: Vec<u8>,
inbox_fee: Balance,
}
#[ink(event)]
// when a new public group is created
pub struct NewPublicGroup {
#[ink(topic)]
from: AccountId,
#[ink(topic)]
group_id: Hash,
#[ink(topic)]
group_name: Vec<u8>,
description: Vec<u8>,
}
#[ink(event)]
// when a new public newsletter list is created
pub struct NewPublicList {
#[ink(topic)]
list_id: Hash,
#[ink(topic)]
owner: AccountId,
#[ink(topic)]
list_name: Vec<u8>,
description: Vec<u8>,
}
#[ink(event)]
// when a new public newsletter list is created
pub struct ListDeleted {
#[ink(topic)]
list_id: Hash,
}
#[ink(event)]
// Writes the new reward to the blockchain
pub struct AccountRewardedMessaging {
#[ink(topic)]
claimant: AccountId,
reward: Balance,
}
// ERROR DEFINITIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#[derive(Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum Error {
// Allowing an account that you allow already
DuplicateAllow,
// Disallowing an account that you don't allow anyway
AlreadyNotAllowed,
// Blocking an account that you already blocked
DuplicateBlock,
// Unblocking an account that you never blocked
NotBlocked,
// trying to update your interest before 24 hours have past
CannotUpdateInterestsWithin24Hours,
// Too many interests in your list
InterestsTooLong,
// trying to message a group or list that does not exist
NoSuchList,
// trying to delete a message that does not exist
MessageNotFound,
// trying to message an account that has not allowed you
NotAllowedToMessage,
// trying to make a group whose name is already taken
GroupNameTaken,
// trying to join a group that doesn't exist
NonexistentGroup,
// trying to access a list that does not exist
NonexistentList,
// creating a duplicate list name
ListNameTaken,
// subscribing to a list you already subscribe to
AlreadySubscribed,
// sending a paid message without enough to cover inbox fees
InsufficientStake,
// if the contract has no money to pay
ZeroBalance,
// if the an inbox or data payment fails
PayoutFailed,
// Returned if the username already belongs to someone else.
UsernameTaken,
// removing an account that was not there
NonexistentAccount,
// input data is too large
DataTooLarge,
// Cannot follow any more accounts or storage otherwise full
StorageFull,
// trying to send a paid message without enough stake or to
// someone who has blocked you
CannotSendPaidMessage,
// attempting to change reward program settings without permission
PermissionDenied,
}
// ACTUAL CONTRACT STORAGE STRUCT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#[ink(storage)]
pub struct ContractStorage {
account_settings: Mapping<AccountId, Settings>,
account_allowed: Mapping<AccountId, AccountVector>,
account_blocked_accounts: Mapping<AccountId, AccountVector>,
account_blocked_lists: Mapping<AccountId, HashVector>,
account_subscribed_groups: Mapping<AccountId, HashVector>,
account_owned_open_lists: Mapping<AccountId, HashVector>,
account_paid_inbox: Mapping<AccountId, HashVector>,
account_subscribed_lists: Mapping<AccountId, HashVector>,
sent_messages_to_account: Mapping<(AccountId, AccountId), HashVector>,
sent_messages_to_list: Mapping<Hash, HashVector>,
sent_messages_to_group: Mapping<(AccountId, Hash), HashVector>,
all_messages_to_group: Mapping<Hash, HashVector>,
message_details: Mapping<Hash, MessageDetails>,
list_message_details: Mapping<Hash, ListMessageDetails>,
paid_message_details: Mapping<Hash, PaidMessageDetails>,
group_message_details: Mapping<Hash, ListMessageDetails>,
open_list_details: Mapping<Hash, OpenListDetails>,
group_details: Mapping<Hash, GroupDetails>,
open_lists: StorageVec<Hash>,
groups: StorageVec<Hash>,
username_map: Mapping<Vec<u8>, AccountId>,
reward_root_set: u8,
reward_root: AccountId,
reward_interval: u128,
reward_amount: Balance,
reward_on: u8,
reward_balance: Balance,
reward_payouts: Balance,
claim_counter: u128,
}
// BEGIN CONTRACT LOGIC >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
impl ContractStorage {
// CONSTRUCTORS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Constructors are implicitly payable.
#[ink(constructor)]
pub fn new() -> Self {
Self {
account_settings: Mapping::default(),
account_allowed: Mapping::default(),
account_blocked_accounts: Mapping::default(),
account_blocked_lists: Mapping::default(),
account_subscribed_groups: Mapping::default(),
account_owned_open_lists: Mapping::default(),
account_paid_inbox: Mapping::default(),
account_subscribed_lists: Mapping::default(),
sent_messages_to_account: Mapping::default(),
sent_messages_to_list: Mapping::default(),
sent_messages_to_group: Mapping::default(),
all_messages_to_group: Mapping::default(),
message_details: Mapping::default(),
list_message_details: Mapping::default(),
paid_message_details: Mapping::default(),
group_message_details: Mapping::default(),
open_list_details: Mapping::default(),
group_details: Mapping::default(),
open_lists: StorageVec::default(),
groups: StorageVec::default(),
username_map: Mapping::default(),
reward_root_set: 0,
reward_root: AccountId::from([0x0; 32]),
reward_interval: 1000000,
reward_amount: 0,
reward_on: 0,
reward_balance: 0,
reward_payouts: 0,
claim_counter: 0,
}
}
// MESSAGE FUNCTIONS THAT CHANGE DATA IN THE CONTRACT STORAGE >>>>>>>>>>>>>>>>>>>
// 0 π’ Update Settings
// lets a user update their list of keyword interests and other settings
// overwrites the mapping in contract storage
#[ink(message)]
pub fn update_settings (&mut self,
my_username: Vec<u8>,
my_interests: Vec<u8>,
my_inbox_fee: Balance,
hide_from_search: bool
) -> Result<(), Error> {
// if the input data is too bid, send an error
if my_username.len() > 100 || my_interests.len() > 600 {
return Err(Error::DataTooLarge);
}
let username_clone1 = my_username.clone();
let username_clone2 = my_username.clone();
let username_clone3 = my_username.clone();
let username_clone4 = my_username.clone();
let interests_clone2 = my_interests.clone();
// get the current settings for this caller and prepare the update
let caller = Self::env().caller();
let current_settings = self.account_settings.get(&caller).unwrap_or_default();
let settings_update: Settings = Settings {
user_account: caller,
username: my_username,
interests: my_interests,
inbox_fee: my_inbox_fee,
hide_from_search: hide_from_search,
last_update: self.env().block_timestamp()
};
// check that this user has not updated their settings in 24 hours
let time_since_last_update = self.env().block_timestamp().saturating_sub(current_settings.last_update);
if time_since_last_update < 86400000 {
// send an error that interest cannot be updated so soon
return Err(Error::CannotUpdateInterestsWithin24Hours)
}
else {
// check that the username is not taken by someone else...
// if the username is in use already...
if self.username_map.contains(username_clone1) {
// get the account that owns that username
let current_owner = self.username_map.get(&username_clone2).unwrap();
// if the caller owns that username, update the storage maps
if current_owner == caller {
if self.account_settings.try_insert(&caller, &settings_update).is_err() {
return Err(Error::DataTooLarge);
}
}
else {
// if the username belongs to someone else, send an error UsernameTaken
return Err(Error::UsernameTaken)
}
}
else {
// if the username is not already in use, update the storage maps
// update the account_settings map
self.account_settings.insert(&caller, &settings_update);
// then update the username map
self.username_map.insert(&username_clone3, &caller);
}
// Emit an event to register the update to the chain
// but only if the caller is not hidden
if hide_from_search == false {
Self::env().emit_event(SettingsUpdated {
from: caller,
username: username_clone4,
interests: interests_clone2,
inbox_fee: my_inbox_fee,
});
}
}
Ok(())
}
// 1 π’ Send A Private Message π
#[ink(message)]
pub fn send_private_message (&mut self,
to_acct: AccountId,
new_message: Vec<u8>,
file_url: Vec<u8>,
) -> Result<(), Error> {
// if the input data is too large, send an error
if new_message.len() > 600 || file_url.len() > 300 {
return Err(Error::DataTooLarge);
}
// get the list of allowed accounts for to_acct
let current_allowed = self.account_allowed.get(&to_acct).unwrap_or_default();
// if caller is in the allowed accounts, proceed
let caller = Self::env().caller();
if current_allowed.accountvector.contains(&caller) {
// set up clones as needed
let new_message_clone = new_message.clone();
// set up the data that will go into the new_message_id hash
let new_timestamp = self.env().block_timestamp();
// create the new_message_id by hashing the above data
let encodable = (caller, to_acct, new_message, new_timestamp); // Implements `scale::Encode`
let mut new_message_id_u8 = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
ink::env::hash_encoded::<Sha2x256, _>(&encodable, &mut new_message_id_u8);
let new_message_id: Hash = Hash::from(new_message_id_u8);
// SET UP THE MESSAGE DETAILS FOR THE NEW MESSAGE
let fromusername = self.account_settings.get(&caller).unwrap_or_default().username;
let new_details = MessageDetails {
message_id: new_message_id,
from_acct: Self::env().caller(),
from_username: fromusername,
to_acct: to_acct,
message: new_message_clone,
file_url: file_url,
timestamp: self.env().block_timestamp(),
};
// get the messages vector for this pair of accounts
let mut current_messages = self.sent_messages_to_account.get((&caller, &to_acct)).unwrap_or_default();
// if the vector is full, kick out the oldest message
if current_messages.hashvector.len() > 2 {
// remove the oldest message from message_details
let oldest = current_messages.hashvector[0];
self.message_details.remove(oldest);
// remove the oldest message from sent_messages_to_account
current_messages.hashvector.remove(0);
}
// update the message_details: Mapping<Hash, MessageDetails>
if self.message_details.try_insert(&new_message_id, &new_details).is_err() {
return Err(Error::DataTooLarge);
}
// update the sent_messages_to_account: Mapping<(AccountId, AccountId), HashVector>
// add this message to the messages vector for this account
current_messages.hashvector.push(new_message_id);
// update the sent_messages_to_account map
self.sent_messages_to_account.insert((&caller, &to_acct), ¤t_messages);
// REWARD PROGRAM ACTIONS... update the claim_counter
self.claim_counter = self.claim_counter.wrapping_add(1);
// IF conditions are met THEN payout a reward
let min = self.reward_amount.saturating_add(10);
let payout: Balance = self.reward_amount;
if self.reward_on == 1 && self.reward_balance > payout && self.env().balance() > min
&& self.claim_counter.checked_rem_euclid(self.reward_interval) == Some(0) {
// payout
if self.env().transfer(caller, payout).is_err() {
return Err(Error::PayoutFailed);
}
// update reward_balance
self.reward_balance = self.reward_balance.saturating_sub(payout);
// update reward_payouts
self.reward_payouts = self.reward_payouts.saturating_add(payout);
// emit an event to register the reward to the chain
Self::env().emit_event(AccountRewardedMessaging {
claimant: caller,
reward: payout
});
}
// END REWARD PROGRAM ACTIONS
Ok(())
}
else {
// otherwise, if the caller is not allowed to message this account, send an error
return Err(Error::NotAllowedToMessage)
}
}
// 2 π’ Send A Message To Group π
#[ink(message)]
pub fn send_message_to_group (&mut self,
to_group_id: Hash,
new_message: Vec<u8>,
file_url: Vec<u8>,
) -> Result<(), Error> {
// if the input data is too large, send an error
if new_message.len() > 600 || file_url.len() > 300 {
return Err(Error::DataTooLarge);
}
// check that the group actually exists
if self.group_details.contains(&to_group_id) {
// check that the caller is in the group
let caller = Self::env().caller();
let mygroups = self.account_subscribed_groups.get(&caller).unwrap_or_default();
if mygroups.hashvector.contains(&to_group_id) {
// set up clones
let new_message_clone = new_message.clone();
// set up the data that will go into the new_message_id hash
let new_timestamp = self.env().block_timestamp();
// create the new_message_id by hashing the above data
let encodable = (caller, to_group_id, new_message, new_timestamp); // Implements `scale::Encode`
let mut new_message_id_u8 = <Sha2x256 as HashOutput>::Type::default(); // 256-bit buffer
ink::env::hash_encoded::<Sha2x256, _>(&encodable, &mut new_message_id_u8);
let new_message_id: Hash = Hash::from(new_message_id_u8);
// SET UP THE MESSAGE DETAILS FOR THE NEW MESSAGE
let fromusername = self.account_settings.get(&caller).unwrap_or_default().username;
let listname = self.group_details.get(&to_group_id).unwrap_or_default().group_name;
let new_details = ListMessageDetails {
message_id: new_message_id,
from_acct: caller,
username: fromusername,
to_list_id: to_group_id,
to_list_name: listname,
message: new_message_clone,
file_url: file_url,
timestamp: self.env().block_timestamp(),
};
// update sent_messages_to_group: Mapping<(AccountId, Hash), HashVector>
// get the messages vector for this pair of account/group
let mut current_messages = self.sent_messages_to_group.get((&caller, &to_group_id)).unwrap_or_default();
// if their storage is full, kick out the oldest message
if current_messages.hashvector.len() > 24 {
let oldest = current_messages.hashvector[0];
// remove oldest from all_messages_to_group
self.all_messages_to_group.remove(oldest);
// remove oldest from group_message_details
self.group_message_details.remove(oldest);
// remove oldest from sent_messages_to_group
current_messages.hashvector.remove(0);
}
// update all_messages_to_group: Mapping<Hash, HashVector>
// get the messages vector for this group
let mut current_group_messages = self.all_messages_to_group.get(&to_group_id).unwrap_or_default();
// if the group message storage is full, kick out the oldest message
if current_group_messages.hashvector.len() > 24 {
let oldest = current_group_messages.hashvector[0];
let oldest_from = self.group_message_details.get(oldest).unwrap_or_default().from_acct;
// remove oldest message in the all_messages_to_group list
current_group_messages.hashvector.remove(0);
// remove oldest message from message_details
self.message_details.remove(oldest);
// remove oldest message from sent_messages_to_group
let mut thislist = self.sent_messages_to_group.get((oldest_from, to_group_id)).unwrap_or_default();
thislist.hashvector.retain(|value| *value != oldest);
self.sent_messages_to_group.insert((oldest_from, to_group_id), &thislist);
}
// add this message to the messages vector for this account
current_messages.hashvector.push(new_message_id);
// update the sent_messages_to_group map
self.sent_messages_to_group.insert((&caller, &to_group_id), ¤t_messages);
// add this message to the messages vector for this account
current_group_messages.hashvector.push(new_message_id);
// update the all_messages_to_group map
self.all_messages_to_group.insert(&to_group_id, ¤t_group_messages);
// update group_message_details: Mapping<Hash, ListMessageDetails>
if self.group_message_details.try_insert(&new_message_id, &new_details).is_err() {
return Err(Error::DataTooLarge);
}
// REWARD PROGRAM ACTIONS... update the claim_counter
self.claim_counter = self.claim_counter.wrapping_add(1);
// IF conditions are met THEN payout a reward
let min = self.reward_amount.saturating_add(10);
let payout: Balance = self.reward_amount;
if self.reward_on == 1 && self.reward_balance > payout && self.env().balance() > min
&& self.claim_counter.checked_rem_euclid(self.reward_interval) == Some(0) {
// payout
if self.env().transfer(caller, payout).is_err() {
return Err(Error::PayoutFailed);
}
// update reward_balance
self.reward_balance = self.reward_balance.saturating_sub(payout);
// update reward_payouts
self.reward_payouts = self.reward_payouts.saturating_add(payout);
// emit an event to register the reward to the chain
Self::env().emit_event(AccountRewardedMessaging {
claimant: caller,
reward: payout
});
}
// END REWARD PROGRAM ACTIONS
}
else {
return Err(Error::NoSuchList)
}
}
else {
return Err(Error::NoSuchList)
}
Ok(())
}
// 3 π’ Allow Account
#[ink(message)]
pub fn allow_account (&mut self, allow: Vec<AccountId>) -> Result<(), Error> {
let caller = Self::env().caller();
let mut current_allowed = self.account_allowed.get(&caller).unwrap_or_default();
// if there is room in the allowed account vector...
if current_allowed.accountvector.len().saturating_add(allow.len()) < 60 {
// proceed to add each account
for acct in allow {
// Is this account already allowed? If TRUE, send ERROR
if current_allowed.accountvector.contains(&acct) {
return Err(Error::DuplicateAllow);
}
else {
// add the new allow to the the vector of accounts caller is allowing
current_allowed.accountvector.push(acct);
}
}
// Update (overwrite) the account_allowed: Mapping<AccountID, AccountVector> map
self.account_allowed.insert(&caller, ¤t_allowed);
}
else {
// error: Account following is full
return Err(Error::StorageFull)
}
Ok(())
}
// 4 π’ Disallow Account
#[ink(message)]
pub fn disallow_account (&mut self, disallow: AccountId) -> Result<(), Error> {
// Is this account currently allowed? If TRUE, proceed...
let caller = Self::env().caller();
let mut current_allowed = self.account_allowed.get(&caller).unwrap_or_default();
if current_allowed.accountvector.contains(&disallow) {
// remove the unwanted account from the the vector of accounts they allow
// by keeping everyone other than that account
current_allowed.accountvector.retain(|value| *value != disallow);
// Update (overwrite) the account_allowed map in the storage
self.account_allowed.insert(&caller, ¤t_allowed);
}
// If the account is not currently allowed, ERROR: Already Not Allowed
else {
return Err(Error::AlreadyNotAllowed);
}
Ok(())
}
// 5 π’ Block Account
#[ink(message)]
pub fn block_account (&mut self, block: AccountId) -> Result<(), Error> {
// Is this account already being blocked? If TRUE, send ERROR
let caller = Self::env().caller();
let mut current_blocked = self.account_blocked_accounts.get(&caller).unwrap_or_default();
// check that there is room in storage
if current_blocked.accountvector.len() < 400 {
if current_blocked.accountvector.contains(&block) {
return Err(Error::DuplicateBlock);
}
// Otherwise, update the account_blocked_accounts for this caller
else {
// add the new block to the the vector of accounts caller is blocking
current_blocked.accountvector.push(block);
// Update (overwrite) the account_blocked_accounts: Mapping<AccountID, AccountVector> map
self.account_blocked_accounts.insert(&caller, ¤t_blocked);
}
}
else {
return Err(Error::StorageFull);
}
Ok(())
}
// 6 π’ Unblock Account
#[ink(message)]
pub fn unblock_account (&mut self, unblock: AccountId) -> Result<(), Error> {
// Is this account currently being blocked? If TRUE, proceed...
let caller = Self::env().caller();
let mut current_blocked = self.account_blocked_accounts.get(&caller).unwrap_or_default();
if current_blocked.accountvector.contains(&unblock) {
// remove the unblock from the the vector of accounts they are blocking
// by keeping everyone other than that account...
current_blocked.accountvector.retain(|value| *value != unblock);
// Update (overwrite) the account_blocked_accounts map in the storage
self.account_blocked_accounts.insert(&caller, ¤t_blocked);
}
// If the account is not currently being followed, ERROR: Already Not blocked
else {
return Err(Error::NotBlocked);
}
Ok(())
}
// 7 π’ Delete A Single Message To An Account
#[ink(message)]
pub fn delete_single_message_to_account (&mut self, message_id_to_delete: Hash) -> Result<(), Error> {
// does this message exist? If it does, proceed
if self.message_details.contains(&message_id_to_delete) {
// get the details for this message
let caller = Self::env().caller();
let current_details = self.message_details.get(&message_id_to_delete).unwrap_or_default();
// get the message hash vector between these two accounts
let to_account = current_details.to_acct;
let mut conversation = self.sent_messages_to_account.get((&caller, &to_account)).unwrap_or_default();
// retain all but this message in that vector
conversation.hashvector.retain(|value| *value != message_id_to_delete);
// update the sent_messages_to_account mapping
self.sent_messages_to_account.insert((&caller, &to_account), &conversation);
// remove this message id from the message_details: Mapping<Hash, MessageDetails> map
self.message_details.remove(message_id_to_delete);
Ok(())
}
else {
return Err(Error::MessageNotFound);
}
}
// 8 π’ Delete All Messages Sent To Account
#[ink(message)]
pub fn delete_all_messages_to_account (&mut self,
delete_all_messages_to: AccountId
) -> Result<(), Error> {
// sent_messages_to_account: Mapping<(AccountId, AccountId), HashVector>
// message_details: Mapping<Hash, MessageDetails>
let caller = Self::env().caller();
// first, get the vector of messages from the caller to that account
let conversation = self.sent_messages_to_account.get((&caller, &delete_all_messages_to)).unwrap_or_default();
// for each message id hash, remove it from the message_details map
for messageid in conversation.hashvector {
self.message_details.remove(messageid);
}
// then remove the pair of caller/account from the sent_messages_to_account map
self.sent_messages_to_account.remove((&caller, &delete_all_messages_to));
Ok(())
}
// 9 π’ Make A Group (public or private) π
#[ink(message)]
pub fn make_a_new_group (&mut self,
new_group_name: Vec<u8>,
hide_from_search: bool,
description: Vec<u8>,
first_message: Vec<u8>,
file_url: Vec<u8>,