-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathover_network.move
13171 lines (11349 loc) · 497 KB
/
over_network.move
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
/*
This quest features the move module for a decentralized social media platform. The platform
allows users to create and manage accounts, follow other accounts, and post, comment, and like
content. Account ownership is handled through NFTs.
NOTE: Remember to visit https://overmind.xyz/quests/over-network and click `Submit Quest` for your
submission to be reviewed and accepted!
Account NFT collection
Every account in the platform is represented by the ownership of an account NFT. The
account collection is an unlimited collection that holds each NFT. The collection's name is
"account collection", the description is "account collection description", and the URI is
"account collection uri". The collection has no royalty fee and the supply is trackable.
Account NFT and data
Each NFT represents an account in the platform. The name of the NFT is the username of the
account. The description and URI are both empty strings (b""). The NFT has no royalty fee.
Each NFT holds two resources: AccountMetaData and Publications. AccountMetaData holds the
metadata for the account and Publications holds all of the posts, comments, and likes that
the account has made.
The data in the AccountMetaData resource is limited as follows:
- The username must be between 1 and 32 characters long (inclusive).
- The name must be no longer than 60 characters long.
- The profile picture URI must be no longer than 256 characters long.
- The bio must be no longer than 160 characters long.
The data in the Publications resource is limited as follows:
- The content of a post must be no longer than 280 characters long.
- The content of a comment must be no longer than 280 characters long.
Account registry
The AccountRegistry struct holds a mapping of registered usernames to the address of the
associated account NFT. The account registry is stored in the module's State resource.
Anyone is able to create a new account as long as the username is not already registered.
Publications
In this platform, the three types of publications are posts, comments, and likes.
Posts
Posts are the main type of publication in the platform. Posts are created by accounts
and can be commented on and liked. Posts are stored in the account's Publications
resource.
Posts cannot be edited or deleted once they are created.
Comments
Comments are publications that are made on posts. Comments are created by accounts and
can be liked. Comments are stored in the account's Publications resource.
Comments cannot be edited or deleted once they are created.
Likes
Likes are publications that are made on posts and comments. Likes are created by
accounts. Likes are stored in the account's Publications resource.
Accounts are able to like a post or comment only once. Accounts are able to unlike a
post or comment only if they have already liked it.
References
All original publication data are stored in each account's Publications resource. In order
to avoid storing duplicate data, references to the original publications are stored in
other accounts' Publications.
For example, if account A posts a post, then the post is stored in account A's Publications
resource. If account B comments on the post, then the comment is stored in account B's
Publications resource. In addition, a reference to the comment is stored in account A's
post in account A's Publications resource and a reference to the post is stored in account
B's comment in account B's Publications resource.
The PublicationReference struct holds the data needed to reference a publication. This
contains the address of the account that owns the publication, the type of the publication,
and the index of the publication in the account's associated Publications resource list.
GlobalTimeline
The GlobalTimeline resource holds references to each post created in the platform. The
GlobalTimeline resource is stored in the module's resource account.
Following
Accounts are able to follow/unfollow other accounts. When an account follows another
account, the metadata of each account is updated to reflect the new follow.
View functions
This platform has a collection of view functions which are used to query the state of the
platform without modifying the it.
Pagination
The view function include pagination parameters so that the caller can specify how much
data they want to receive. The pagination parameters are:
- page_size: The number of items to return in the page
- page: The page number to return (0-indexed)
Aborting in view functions
View functions are designed to not abort but rather return empty data if given invalid
parameters.
The empty values for each type are:
- String - string::utf8(b"")
- vectors - vector[]
- u64 - 0
- address - @0x0
- boolean - false
Error codes
The module provides the following error codes to use:
- EUsernameAlreadyRegistered
This error code is to be used when an account tries to create an account with a
username that is already registered.
- EUsernameNotRegistered
This error code is to be used when an account tries to perform an action with or
on an account that is not registered.
- EAccountDoesNotOwnUsername
This error code is to be used when an account tries to perform an action with an
username that is not owned by them.
- EBioInvalidLength
This error code is to be used when an account tries to create or update an account
with a bio that is not valid length.
- EUserDoesNotFollowUser
This error code is to be used when an account tries to unfollow an account that
they do not follow.
- EUserFollowsUser
This error code is to be used when an account tries to follow an account that they
already follow.
- EPublicationDoesNotExistForUser
This error code is to be used when an account tries to perform an action on a
publication that does not exist.
- EPublicationTypeToLikeIsInvalid
This error code is to be used when an account tries to like a publication with an
invalid type.
- EUserHasLikedPublication
This error code is to be used when an account tries to like a publication that they
have already liked.
- EUserHasNotLikedPublication
This error code is to be used when an account tries to unlike a publication that
they have not liked.
- EUsersAreTheSame
This error code is to be used when an account tries to follow or unfollow itself.
- EInvalidPublicationType
This error code is to be used when an account tries to perform an action on a
publication with an invalid type.
- EUsernameInvalidLength
This error code is to be used when an account tries to create or update an account
with a username that is not valid length.
- ENameInvalidLength
This error code is to be used when an account tries to create or update an account
with a name that is not valid length.
- EProfilePictureUriInvalidLength
This error code is to be used when an account tries to create or update an account
with a profile picture URI that is not valid length.
- EContentInvalidLength
This error code is to be used when an account tries to create a post or comment
with content that is not valid length.
*/
module overmind::over_network {
//==============================================================================================
// Dependencies
//==============================================================================================
use std::signer;
use std::option::{Self};
use aptos_framework::event;
use aptos_framework::object;
use aptos_framework::vector;
use aptos_framework::account;
use std::table::{Self, Table};
use aptos_framework::timestamp;
use aptos_token_objects::token;
use std::string::{Self, String};
use aptos_token_objects::collection;
//==============================================================================================
// Constants - DO NOT MODIFY
//==============================================================================================
const SEED: vector<u8> = b"decentralized platform";
const USERNAME_LENGTH_MAX: u64 = 32;
const NAME_LENGTH_MAX: u64 = 60;
const PROFILE_URI_LENGTH_MAX: u64 = 256;
const BIO_LENGTH_MAX: u64 = 160;
const POST_CONTENT_LENGTH_MAX: u64 = 280;
const COMMENT_CONTENT_LENGTH_MAX: u64 = 280;
//==============================================================================================
// Error codes - DO NOT MODIFY
//==============================================================================================
const EUsernameAlreadyRegistered: u64 = 1;
const EUsernameNotRegistered: u64 = 2;
const EAccountDoesNotOwnUsername: u64 = 3;
const EBioInvalidLength: u64 = 4;
const EUserDoesNotFollowUser: u64 = 5;
const EUserFollowsUser: u64 = 6;
const EPublicationDoesNotExistForUser: u64 = 7;
const EPublicationTypeToLikeIsInvalid: u64 = 8;
const EUserHasLikedPublication: u64 = 9;
const EUserHasNotLikedPublication: u64 = 10;
const EUsersAreTheSame: u64 = 11;
const EInvalidPublicationType: u64 = 12;
const EUsernameInvalidLength: u64 = 13;
const ENameInvalidLength: u64 = 14;
const EProfilePictureUriInvalidLength: u64 = 15;
const EContentInvalidLength: u64 = 16;
//==============================================================================================
// Module Structs - DO NOT MODIFY
//==============================================================================================
/*
The resource that holds all of the publication for an account. To be owned by the account
NFT.
*/
struct Publications has key {
// List of account's posts
posts: vector<Post>,
// List of account's comments
comments: vector<Comment>,
// List of account's likes
likes: vector<Like>
}
/*
The data struct for an account's post. To be stored in the account's Publications resource.
*/
struct Post has store, copy, drop {
// Timestamp of when the post was created
timestamp: u64,
// The id of the post
id: u64,
// The content of the post
content: String,
// The references to the comments on the post
comments: vector<PublicationReference>,
// The usernames of the accounts that like the post
likes: vector<String>
}
/*
The data struct for an account's comment. To be stored in the account's Publications
resource.
*/
struct Comment has store, copy, drop {
// Timestamp of when the comment was created
timestamp: u64,
// The id of the comment
id: u64,
// The content of the comment
content: String,
// The reference to the post the comment is on
reference: PublicationReference,
// The usernames of the accounts that like the comment
likes: vector<String>,
}
/*
The data struct for an account's like. To be stored in the account's Publications resource.
*/
struct Like has store, copy, drop {
// Timestamp of when the like was created
timestamp: u64,
// The reference to the publication that was liked
reference: PublicationReference
}
/*
The data struct for a reference to a publication. To be stored in the account's Publications
resource.
*/
struct PublicationReference has store, copy, drop {
// The address of the account that owns the publication
publication_author_account_address: address,
// The type of the publication
publication_type: String,
// The index of the publication in the account's Publications resource
publication_index: u64
}
/*
The resource that holds all of an account's metadata. To be owned by the account NFT.
*/
struct AccountMetaData has key, copy, drop {
// The timestamp of when the account was created
creation_timestamp: u64,
// The address of the account NFT
account_address: address,
// The username of the account
username: String,
// The name of the account owner
name: String,
// The URI of the profile picture of the account
profile_picture_uri: String,
// The bio of the account
bio: String,
// The usernames of the accounts that follow the account
follower_account_usernames: vector<String>,
// The usernames of the accounts that the account follows
following_account_usernames: vector<String>
}
/*
The data struct that holds all of the accounts registered in the platform. To be stored in
the module's State resource.
*/
struct AccountRegistry has store {
// The mapping of usernames to account addresses
accounts: Table<String, address>
}
/*
The resource that holds the state of the module. To be owned by the module's resource
account.
*/
struct State has key {
// The address of the account NFT collection
account_collection_address: address,
// The account registry
account_registry: AccountRegistry,
// The signer capability for the module's resource account
signer_cap: account::SignerCapability
}
/*
The resource that holds all of the module's event handles. To be owned by the module's
resource account.
*/
struct ModuleEventStore has key {
// The event handle for account created events
account_created_events: event::EventHandle<AccountCreatedEvent>,
// The event handle for account follow events
account_follow_events: event::EventHandle<AccountFollowEvent>,
// The event handle for account unfollow events
account_unfollow_events: event::EventHandle<AccountUnfollowEvent>,
// The event handle for account post events
account_post_events: event::EventHandle<AccountPostEvent>,
// The event handle for account comment events
account_comment_events: event::EventHandle<AccountCommentEvent>,
// The event handle for account like events
account_like_events: event::EventHandle<AccountLikeEvent>,
// The event handle for account unlike events
account_unlike_events: event::EventHandle<AccountUnlikeEvent>,
}
/*
The resource that holds references for each post in the platform. To be owned by the
module's resource account.
*/
struct GlobalTimeline has key {
// The list of references to each post in the platform
posts: vector<PublicationReference>
}
//==============================================================================================
// Event structs - DO NOT MODIFY
//==============================================================================================
/*
Event to be emitted when an account is created.
*/
struct AccountCreatedEvent has store, drop {
// The timestamp of when the account was created
timestamp: u64,
// The address of the account NFT
account_address: address,
// The username of the account
username: String,
// The address of the account that created the account
creator: address
}
/*
Event to be emitted when an account follows another account.
*/
struct AccountFollowEvent has store, drop {
// The timestamp of when the account followed another account
timestamp: u64,
// The username of the account that followed another account
follower_account_username: String,
// The username of the account that was followed
following_account_username: String
}
/*
Event to be emitted when an account unfollows another account.
*/
struct AccountUnfollowEvent has store, drop {
// The timestamp of when the account unfollowed another account
timestamp: u64,
// The username of the account that unfollowed another account
unfollower_account_username: String,
// The username of the account that was unfollowed
unfollowing_account_username: String
}
/*
Event to be emitted when an account posts a new post.
*/
struct AccountPostEvent has store, drop {
// The timestamp of when the account posted a new post
timestamp: u64,
// The username of the account that posted a new post
poster_username: String,
// The id of the post
post_id: u64,
}
/*
Event to be emitted when an account comments on a post.
*/
struct AccountCommentEvent has store, drop {
// The timestamp of when the account commented on a post
timestamp: u64,
// The username of the account that owns the post
post_owner_username: String,
// The id of the post
post_id: u64,
// The username of the account that commented on the post
commenter_username: String,
// The id of the comment
comment_id: u64,
}
/*
Event to be emitted when an account likes a publication.
*/
struct AccountLikeEvent has store, drop {
// The timestamp of when the account liked a publication
timestamp: u64,
// The username of the account owns the publication
publication_owner_username: String,
// The type of the publication that was liked
publication_type: String,
// The id of the publication that was liked
publication_id: u64,
// The username of the account that liked the publication
liker_username: String
}
/*
Event to be emitted when an account unlikes a publication.
*/
struct AccountUnlikeEvent has store, drop {
// The timestamp of when the account unliked a publication
timestamp: u64,
// The username of the account owns the publication
publication_owner_username: String,
// The type of the publication that was unliked
publication_type: String,
// The id of the publication that was unliked
publication_id: u64,
// The username of the account that unliked the publication
unliker_username: String
}
//==============================================================================================
// Functions
//==============================================================================================
/*
Initializes the module by creating the resource account, creating the account NFT collection,
and creating and moving the State, ModuleEventStore, and GlobalTimeline resources to the
resource account.
@param admin - The signer representing the publisher of the module
*/
fun init_module(admin: &signer) {
let (resource_account, resource_signer_cap) = account::create_resource_account(admin, SEED);
collection::create_unlimited_collection(
&resource_account,
string::utf8(b"account collection description"),
string::utf8(b"account collection"),
option::none(),
string::utf8(b"account collection uri"));
let collection_address = collection::create_collection_address(
&signer::address_of(&resource_account),
&string::utf8(b"account collection"));
let account_reg = AccountRegistry {
accounts: table::new<String,address>(),
};
let state = State {
// The address of the account NFT collection
account_collection_address: collection_address,
// The account registry
account_registry: account_reg,
// The signer capability for the module's resource account
signer_cap: resource_signer_cap
};
move_to(&resource_account, state);
let events_store = ModuleEventStore {
// The event handle for account created events
account_created_events: account::new_event_handle<AccountCreatedEvent>(admin),
// The event handle for account follow events
account_follow_events: account::new_event_handle<AccountFollowEvent>(admin),
// The event handle for account unfollow events
account_unfollow_events: account::new_event_handle<AccountUnfollowEvent>(admin),
// The event handle for account post events
account_post_events: account::new_event_handle<AccountPostEvent>(admin),
// The event handle for account comment events
account_comment_events: account::new_event_handle<AccountCommentEvent>(admin),
// The event handle for account like events
account_like_events: account::new_event_handle<AccountLikeEvent>(admin),
// The event handle for account unlike events
account_unlike_events: account::new_event_handle<AccountUnlikeEvent>(admin),
};
move_to(&resource_account, events_store);
let global_timeline = GlobalTimeline {
posts: vector::empty<PublicationReference>(),
};
move_to(&resource_account, global_timeline);
}
/*
Creates and sets up a new account with the associated data for the owner account. Updates
module state as needed. Aborts if the username, name, or profile pic uri is not valid
length, the username is already registered, if any of the usernames to follow are not
registered, if there is a repeated username in the list, or if the list contains the
username of the account being created.
@param owner - The signer representing the account registering the new account
@param username - The username of the new account
@param name - The name of the new account owner
@param profile_pic - The URI of the profile picture of the new account
@param usernames_to_follow - A list of usernames to follow after creating the account. Can
be empty.
*/
entry fun create_account(
owner: &signer,
username: String,
name: String,
profile_pic: String,
usernames_to_follow: vector<String>
) acquires State, ModuleEventStore, AccountMetaData {
assert_name_length_ok(name, NAME_LENGTH_MAX);
assert_username_length_ok(username, USERNAME_LENGTH_MAX);
assert_profile_pic_uri_length_ok(profile_pic, PROFILE_URI_LENGTH_MAX);
let resource_address = get_resource_account_address();
assert_username_not_registered(username);
assert_to_follow_username_list_ok(username, usernames_to_follow, resource_address);
let state_ref = borrow_global_mut<State>(resource_address);
let creator = account::create_signer_with_capability(&state_ref.signer_cap);
let account_token_ref = token::create_named_token(
&creator,
string::utf8(b"account collection"),
string::utf8(b""),
username,
option::none(),
string::utf8(b"")
);
//Transfer the token to the owner account
let account_token_address = create_account_token_address_with_username(username);
let account_token_object = object::address_to_object<token::Token>(account_token_address);
let owner_address = signer::address_of(owner);
object::transfer<token::Token>(&creator, account_token_object, owner_address);
let account_metadata = AccountMetaData {
// The timestamp of when the account was created
creation_timestamp: timestamp::now_seconds(),
// The address of the account NFT
account_address: account_token_address,
// The username of the account
username: username,
// The name of the account owner
name: name,
// The URI of the profile picture of the account
profile_picture_uri: profile_pic,
// The bio of the account
bio: string::utf8(b""),
// The usernames of the accounts that follow the account
follower_account_usernames: vector::empty<String>(),
// The usernames of the accounts that the account follows
following_account_usernames: usernames_to_follow
};
let pubs = Publications {
// List of account's posts
posts: vector::empty<Post>(),
// List of account's comments
comments: vector::empty<Comment>(),
// List of account's likes
likes: vector::empty<Like>()
};
let token_signer = object::generate_signer(&account_token_ref);
move_to(&token_signer, account_metadata);
move_to(&token_signer, pubs);
//update State resource account registry
table::add<String, address>(&mut state_ref.account_registry.accounts, username, account_token_address);
//also need to add created username to each of the followed account' followers list
let events_store_ref = borrow_global_mut<ModuleEventStore>(resource_address);
vector::for_each(usernames_to_follow, |user| {
let user: String = user;
let user_metadata_ref = borrow_global_mut<AccountMetaData>(get_account_token_address(user));
vector::push_back(&mut user_metadata_ref.follower_account_usernames, username);
//fire followed event
let follow_event = AccountFollowEvent {
// The timestamp of when the account followed another account
timestamp: timestamp::now_seconds(),
// The username of the account that followed another account
follower_account_username: username,
// The username of the account that was followed
following_account_username: user
};
event::emit_event(&mut events_store_ref.account_follow_events, follow_event);
});
//emit account created event
let event = AccountCreatedEvent {
// The timestamp of when the account was created
timestamp: timestamp::now_seconds(),
// The address of the account NFT
account_address: account_token_address,
// The username of the account
username: username,
// The address of the account that created the account
creator: owner_address
};
event::emit_event(&mut events_store_ref.account_created_events, event);
}
/*
Updates the name of the account associated with the given username. Aborts if the name is
not valid length, if the username is not registered, or if the account associated with the
username is not owned by the owner account.
@param owner - The signer representing the owner of the account to update
@param username - The username of the account to update
@param name - The new name of the account
*/
entry fun update_name(
owner: &signer,
username: String,
name: String
) acquires State, AccountMetaData {
assert_name_length_ok(name, NAME_LENGTH_MAX);
assert_username_registered(username);
let account_token_address = get_account_token_address(username);
let owner_address = signer::address_of(owner);
assert_user_owns_account(owner_address, account_token_address);
let account_metadata_ref = borrow_global_mut<AccountMetaData>(account_token_address);
account_metadata_ref.name = name;
}
/*
Updates the bio of the account associated with the given username. Aborts if the bio is too
long, if the username is not registered, or if the account associated with the username is
not owned by the owner account.
@param owner - The signer representing the owner of the account to update
@param username - The username of the account to update
@param bio - The new bio of the account
*/
entry fun update_bio(
owner: &signer,
username: String,
bio: String
) acquires State, AccountMetaData {
assert_bio_length_ok(bio, BIO_LENGTH_MAX);
assert_username_registered(username);
let account_token_address = get_account_token_address(username);
let owner_address = signer::address_of(owner);
assert_user_owns_account(owner_address, account_token_address);
let account_metadata_ref = borrow_global_mut<AccountMetaData>(account_token_address);
account_metadata_ref.bio = bio;
}
/*
Updates the profile picture of the account associated with the given username. Aborts if
the new profile pic uri is not valid length, the username is not registered or if the
account associated with the username is not owned by the owner account.
@param owner - The signer representing the owner of the account to update
@param username - The username of the account to update
@param profile_picture_uri - The new profile picture URI of the account
*/
entry fun update_profile_picture(
owner: &signer,
username: String,
profile_picture_uri: String
) acquires State, AccountMetaData {
assert_profile_pic_uri_length_ok(profile_picture_uri, PROFILE_URI_LENGTH_MAX);
assert_username_registered(username);
let account_token_address = get_account_token_address(username);
let owner_address = signer::address_of(owner);
assert_user_owns_account(owner_address, account_token_address);
let account_metadata_ref = borrow_global_mut<AccountMetaData>(account_token_address);
account_metadata_ref.profile_picture_uri = profile_picture_uri;
}
/*
Follows the account associated with the given username. Aborts if either username is not
registered, if the account associated with the follower username is not owned by the owner
account, if the username to follow is the same as the follower username or if the account
associated with the follower username already follows the account to follow.
@param follower - The signer representing the owner of the account to follow with
@param follower_username - The username of the account to follow with
@param username_to_follow - The username of the account to follow
*/
entry fun follow(
follower: &signer,
follower_username: String,
username_to_follow: String
) acquires State, ModuleEventStore, AccountMetaData {
assert_username_registered(follower_username);
assert_username_registered(username_to_follow);
let account_token_address = get_account_token_address(follower_username);
let follower_address = signer::address_of(follower);
assert_user_owns_account(follower_address, account_token_address);
assert_follower_to_follow_name_different(follower_username, username_to_follow);
let follower_metadata_ref = borrow_global_mut<AccountMetaData>(account_token_address);
assert_to_follow_not_already_followed(username_to_follow, follower_metadata_ref.following_account_usernames);
vector::push_back(&mut follower_metadata_ref.following_account_usernames, username_to_follow);
//also need to add follower's username to the followed account's followers' list
let to_follow_token_address = get_account_token_address(username_to_follow);
let to_follow_metadata_ref = borrow_global_mut<AccountMetaData>(to_follow_token_address);
vector::push_back(&mut to_follow_metadata_ref.follower_account_usernames, follower_username);
//emit a new follow event
let event = AccountFollowEvent {
// The timestamp of when the account followed another account
timestamp: timestamp::now_seconds(),
// The username of the account that followed another account
follower_account_username: follower_username,
// The username of the account that was followed
following_account_username: username_to_follow
};
let resource_address = get_resource_account_address();
let events_store_ref = borrow_global_mut<ModuleEventStore>(resource_address);
event::emit_event(&mut events_store_ref.account_follow_events, event);
}
/*
Unfollows the account associated with the given username. Aborts if either username is not
registered, if the account associated with the unfollower username is not owned by the
owner account, if the username to unfollow is the same as the unfollower username or if the
account associated with the unfollower username does not follow the account to unfollow.
@param unfollower - The signer representing the owner of the account to unfollow with
@param unfollower_username - The username of the account to unfollow with
@param username_to_unfollow - The username of the account to unfollow
*/
entry fun unfollow(
unfollower: &signer,
unfollower_username: String,
username_to_unfollow: String
) acquires State, ModuleEventStore, AccountMetaData {
assert_username_registered(unfollower_username);
assert_username_registered(username_to_unfollow);
let unfollower_token_address = get_account_token_address(unfollower_username);
let unfollower_address = signer::address_of(unfollower);
assert_user_owns_account(unfollower_address, unfollower_token_address);
assert_follower_to_follow_name_different(unfollower_username, username_to_unfollow);
let unfollower_metadata_ref = borrow_global_mut<AccountMetaData>(unfollower_token_address);
assert_to_unfollow_was_followed(username_to_unfollow, unfollower_metadata_ref.following_account_usernames);
vector::remove_value<String>(&mut unfollower_metadata_ref.following_account_usernames, &username_to_unfollow);
//also remove unfollower's username from the unfollowed account's followers' list
let to_unfollow_token_address = get_account_token_address(username_to_unfollow);
let to_unfollow_metadata_ref = borrow_global_mut<AccountMetaData>(to_unfollow_token_address);
vector::remove_value<String>(&mut to_unfollow_metadata_ref.follower_account_usernames, &unfollower_username);
//emit a new unfollow event
let event = AccountUnfollowEvent {
// The timestamp of when the account unfollowed another account
timestamp: timestamp::now_seconds(),
// The username of the account that unfollowed another account
unfollower_account_username: unfollower_username,
// The username of the account that was unfollowed
unfollowing_account_username: username_to_unfollow
};
let resource_address = get_resource_account_address();
let events_store_ref = borrow_global_mut<ModuleEventStore>(resource_address);
event::emit_event(&mut events_store_ref.account_unfollow_events, event);
}
/*
Posts a new post with the given content to the account associated with the given username.
Aborts if the username is not registered, if the account associated with the username is
not owned by the owner account, or if the content is not valid length.
@param owner - The signer representing the owner of the account to post with
@param username - The username of the account to post with
@param content - The content of the post
*/
entry fun post(
owner: &signer,
username: String,
content: String,
) acquires State, ModuleEventStore, Publications, GlobalTimeline {
assert_username_registered(username);
assert_content_length_ok(content, POST_CONTENT_LENGTH_MAX);
let account_token_address = get_account_token_address(username);
let owner_address = signer::address_of(owner);
assert_user_owns_account(owner_address, account_token_address);
let pubs_ref = borrow_global_mut<Publications>(account_token_address);
let post_id = vector::length(&pubs_ref.posts);
let new_post = Post {
// Timestamp of when the post was created
timestamp: timestamp::now_seconds(),
// The id of the post
id: post_id,
// The content of the post
content: content,
// The references to the comments on the post
comments: vector::empty<PublicationReference>(),
// The usernames of the accounts that like the post
likes: vector::empty<String>()
};
vector::push_back(&mut pubs_ref.posts, new_post);
//create a publication reference to this post and add it to global timeline
let resource_address = get_resource_account_address();
let global_timeline_ref = borrow_global_mut<GlobalTimeline>(resource_address);
let new_pub_reference = PublicationReference {
// The address of the account that owns the publication
publication_author_account_address: account_token_address,
// The type of the publication
publication_type: string::utf8(b"post"),
// The index of the publication in the account's Publications resource
publication_index: post_id
};
vector::push_back(&mut global_timeline_ref.posts, new_pub_reference);
//emit a new post event
let event = AccountPostEvent {
// The timestamp of when the account posted a new post
timestamp: timestamp::now_seconds(),
// The username of the account that posted a new post
poster_username: username,
// The id of the post
post_id: post_id,
};
let events_store_ref = borrow_global_mut<ModuleEventStore>(resource_address);
event::emit_event(&mut events_store_ref.account_post_events, event);
}
/*
Comments on the post associated with the given username and id with the given content.
Aborts if the content is not valid length, if the commenter username is not registered, if
the post author username is not registered, if the account associated with the
commenter username is not owned by the owner account, or if the post does not exist.
@param commenter - The signer representing the owner of the account to comment with
@param commenter_username - The username of the account to comment with
@param content - The content of the comment
@param post_author_username - The username of the account that owns the post to comment on
@param post_id - The id of the post to comment on
*/
entry fun comment(
commenter: &signer,
commenter_username: String,
content: String,
post_author_username: String,
post_id: u64,
) acquires State, ModuleEventStore, Publications {
assert_content_length_ok(content, COMMENT_CONTENT_LENGTH_MAX);
assert_username_registered(commenter_username);
assert_username_registered(post_author_username);
let commenter_account_token_address = get_account_token_address(commenter_username);
let commenter_address = signer::address_of(commenter);
assert_user_owns_account(commenter_address, commenter_account_token_address);
let post_author_account_token_address = get_account_token_address(post_author_username);
assert_publication_exists(post_author_account_token_address, post_id, string::utf8(b"post"));
let commenter_pub_ref = borrow_global_mut<Publications>(commenter_account_token_address);
let comment_id = vector::length(&commenter_pub_ref.comments);
let post_reference = PublicationReference {
// The address of the account that owns the publication
publication_author_account_address: post_author_account_token_address,
// The type of the publication
publication_type: string::utf8(b"post"),
// The index of the publication in the account's Publications resource
publication_index: post_id
};
let new_comment = Comment {
// Timestamp of when the comment was created
timestamp: timestamp::now_seconds(),
// The id of the comment
id: comment_id,
// The content of the comment
content: content,
// The reference to the post the comment is on
reference: post_reference,
// The usernames of the accounts that like the comment
likes: vector::empty<String>(),
};
vector::push_back(&mut commenter_pub_ref.comments, new_comment);
let comment_reference = PublicationReference {
// The address of the account that owns the publication
publication_author_account_address: commenter_account_token_address,
// The type of the publication
publication_type: string::utf8(b"comment"),
// The index of the publication in the account's Publications resource
publication_index: comment_id
};
let post_author_pub_ref = borrow_global_mut<Publications>(post_author_account_token_address);
let post = vector::borrow_mut(&mut post_author_pub_ref.posts, post_id);
vector::push_back(&mut post.comments, comment_reference);
//emit a new comment event
let event = AccountCommentEvent {
// The timestamp of when the account commented on a post
timestamp: timestamp::now_seconds(),
// The username of the account that owns the post
post_owner_username: post_author_username,
// The id of the post
post_id: post_id,
// The username of the account that commented on the post
commenter_username: commenter_username,
// The id of the comment
comment_id: comment_id,
};
let resource_address = get_resource_account_address();
let events_store_ref = borrow_global_mut<ModuleEventStore>(resource_address);
event::emit_event(&mut events_store_ref.account_comment_events, event);
}
/*
Likes the publication associated with the given username, type, and id. Aborts if the
publication type is invalid, if the liker username is not registered, if the publication
author username is not registered, if the account associated with the liker username is not
owned by the owner account, if the username has already liked the publication or if the
publication does not exist.
@param liker - The signer representing the owner of the account to like with
@param liker_username - The username of the account to like with
@param publication_author_username - The username of the account that owns the publication
@param publication_type - The type of the publication to like
@param publication_id - The id of the publication to like
*/
entry fun like(
liker: &signer,
liker_username: String,
publication_author_username: String,
publication_type: String,
publication_id: u64,
) acquires State, ModuleEventStore, Publications {
assert_publication_type_to_like_valid(publication_type);
assert_username_registered(liker_username);
assert_username_registered(publication_author_username);
let liker_account_token_address = get_account_token_address(liker_username);
let liker_address = signer::address_of(liker);
assert_user_owns_account(liker_address, liker_account_token_address);
let pub_author_account_token_address = get_account_token_address(publication_author_username);
assert_publication_exists(pub_author_account_token_address, publication_id, publication_type);
assert_liker_not_already_liked_publication(liker_username, pub_author_account_token_address, publication_id, publication_type);
let pub_reference = PublicationReference {
// The address of the account that owns the publication
publication_author_account_address: pub_author_account_token_address,
// The type of the publication
publication_type: publication_type,
// The index of the publication in the account's Publications resource
publication_index: publication_id
};
let new_like = Like {
// Timestamp of when the like was created
timestamp: timestamp::now_seconds(),