/
CanCan.mo
1079 lines (994 loc) · 35.7 KB
/
CanCan.mo
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
import Access "../backend/Access";
import Array "mo:base/Array";
import Hash "mo:base/Hash";
import Base "../backend/Base";
import Buffer "mo:base/Buffer";
import Debug "mo:base/Debug";
import Demo "../backend/Demo";
import DemoCan30 "Demo/Can30_VideoRecommendations";
import DemoCan32 "Demo/Can32_ViralEvent";
import HashMap "mo:base/HashMap";
import Int "mo:base/Int";
import Iter "mo:base/Iter";
import List "mo:base/List";
import Nat "mo:base/Nat";
import Option "mo:base/Option";
import P "mo:base/Prelude";
import Param "../backend/Param";
import Prelude "mo:base/Prelude";
import Principal "mo:base/Principal";
import Rel "../backend/Rel";
import RelObj "../backend/RelObj";
import State "../backend/State";
import Text "mo:base/Text";
import Time "mo:base/Time";
import TrieMap "mo:base/TrieMap";
import Types "../backend/Types";
shared ({caller = initPrincipal}) actor class CanCan () /* : Types.Service */ {
public type ProfileInfo = Types.ProfileInfo;
public type ProfileInfoPlus = Types.ProfileInfoPlus;
public type ProfilePic = Types.ProfilePic;
public type Timestamp = Types.Timestamp;
public type UserId = Types.UserId;
public type VideoId = Types.VideoId;
public type ChunkId = Types.ChunkId;
public type ChunkData = Types.ChunkData;
public type VideoInfo = Types.VideoInfo;
public type VideoInit = Types.VideoInit;
public type VideoPic = Types.VideoPic;
public type VideoResult = Types.VideoResult;
public type VideoResults = Types.VideoResults;
var state = State.empty({ admin = initPrincipal });
/*public query*/ func getState() : async State.StateShared {
State.share(state)
};
/*public*/ func setState(st : State.StateShared) : async () {
state := State.fromShared(st);
};
public query func checkUsernameAvailable(userName_ : Text): async Bool {
switch (state.profiles.get(userName_)) {
case (?_) { /* error -- ID already taken. */ false };
case null { /* ok, not taken yet. */ true };
}
};
/// null means that the principal is unrecognized,
/// otherwise, returns a non-empty array of usernames.
public shared(msg) func getUserNameByPrincipal(p:Principal) : async ?[Text] {
if ( msg.caller == p ) {
?state.access.userPrincipal.get1(p)
} else {
// access control check fails; do not reveal username of p.
null
}
};
/// log the given event kind, with a unique ID and current time
func logEvent(ek : State.Event.EventKind) {
state.eventLog.add({
id = state.eventCount ;
time = timeNow_() ;
kind = ek
});
state.eventCount += 1;
};
// responsible for adding metadata from the user to the state.
// a null principal means that the username has no valid callers (yet), and the admin
// must relate one or more principals to it.
func createProfile_(userName_ : Text, p: ?Principal, pic_ : ?ProfilePic) : ?() {
switch (state.profiles.get(userName_)) {
case (?_) { /* error -- ID already taken. */ null };
case null { /* ok, not taken yet. */
let now = timeNow_();
state.profiles.put(userName_, {
userName = userName_ ;
createdAt = now ;
});
// rewards init invariant: rewards is initialized to zero (is non-null).
state.rewards.put(userName_, 0);
logEvent(#createProfile({userName=userName_; pic=pic_}));
state.access.userRole.put(userName_, #user);
switch p {
case null { }; // no related principals, yet.
case (?p) { state.access.userPrincipal.put(userName_, p); }
};
// success
?()
};
}
};
func accessCheck(caller : Principal, action : Types.UserAction, target : Types.ActionTarget) : ?() {
state.access.check(timeNow_(), caller, action, target)
};
public shared(msg) func createProfile(userName : Text, pic : ?ProfilePic) : async ?ProfileInfoPlus {
do ? {
accessCheck(msg.caller, #create, #user userName)!;
createProfile_(userName, ?msg.caller, pic)!;
// return the full profile info
getProfilePlus_(?userName, userName)! // self-view
}
};
var timeMode : {#ic ; #script} =
switch (Param.timeMode) {
case (#ic) #ic;
case (#script _) #script
};
var scriptTime : Int = 0;
func timeNow_() : Int {
switch timeMode {
case (#ic) { Time.now() };
case (#script) { scriptTime };
}
};
public shared(msg) func scriptTimeTick() : async ?() {
do ? {
accessCheck(msg.caller, #admin, #all)!;
assert (timeMode == #script);
scriptTime := scriptTime + 1;
}
};
func reset_( mode : { #ic ; #script : Int } ) {
setTimeMode_(mode);
state := State.empty({ admin = state.access.admin });
};
public shared(msg) func reset( mode : { #ic ; #script : Int } ) : async ?() {
do ? {
accessCheck(msg.caller, #admin, #all)!;
reset_(mode)
}
};
func setTimeMode_( mode : { #ic ; #script : Int } ) {
switch mode {
case (#ic) { timeMode := #ic };
case (#script st) { timeMode := #script ; scriptTime := st };
}
};
public shared(msg) func setTimeMode( mode : { #ic ; #script : Int } ) : async ?() {
do ? {
accessCheck(msg.caller, #admin, #all)!;
setTimeMode_(mode)
}
};
func getProfileInfo_(target : UserId) : ?ProfileInfo {
do ? {
let profile = state.profiles.get(target)!;
let following_ = state.follows.get0(target);
let followers_ = state.follows.get1(target);
let likes_ = state.likes.get0(target);
let superLikes_ = state.superLikes.get0(target);
let uploaded_ = state.uploaded.get0(target);
let rewards_ = state.rewards.get(target)!;
let abuseFlagCount_ = state.abuseFlagVideos.get1Size(target);
{
userName = profile.userName ;
followers = filterOutAbuseUsers(followers_)! ;
following = filterOutAbuseUsers(following_)! ;
likedVideos = filterOutAbuseVideos(likes_)! ;
superLikedVideos = filterOutAbuseVideos(superLikes_)! ;
uploadedVideos = filterOutAbuseVideos(uploaded_)! ;
hasPic = false ;
rewards = rewards_;
abuseFlagCount = abuseFlagCount_ ;
}
}
};
public query(msg) func getProfileInfo(userId : UserId) : async ?ProfileInfo {
do ? {
accessCheck(msg.caller, #view, #user userId)!;
getProfileInfo_(userId)!
}
};
/// "Deeper" version of ProfileInfo.
///
/// Gives Video- and ProfileInfos instead of merely Ids in the results.
///
/// The optional "caller" UserId personalizes the resulting record for
/// various cases:
/// - When caller is not given, less information is non-null in result.
/// - When calling user is viewing their own profile,
/// gives private and quasi-private info to them about their allowances.
/// - When calling user is viewing profile of another user,
/// gives private info about super likes / abuse flags toward that use.
public query(msg) func getProfilePlus(caller: ?UserId, target: UserId): async ?ProfileInfoPlus {
do ? {
accessCheck(msg.caller, #view, #user target)!;
switch caller {
case null { getProfilePlus_(null, target)! };
case (?callerUserName) {
// has private access to our caller view?
accessCheck(msg.caller, #update, #user callerUserName)!;
getProfilePlus_(?callerUserName, target)!
};
}
}
};
func filterOutAbuseVideos(videos: [VideoId]) : ?[VideoId] {
do ? {
let nonAbuse = Buffer.Buffer<VideoId>(0);
for (v in videos.vals()) {
let flags = state.abuseFlagVideos.get1Size(v);
if (flags < Param.contentModerationThreshold) {
nonAbuse.add(v)
}
};
nonAbuse.toArray()
}
};
func filterOutAbuseUsers(users: [UserId]) : ?[UserId] {
do ? {
let nonAbuse = Buffer.Buffer<UserId>(0);
for (u in users.vals()) {
let flags = state.abuseFlagUsers.get1Size(u);
if (flags < Param.contentModerationThreshold) {
nonAbuse.add(u)
}
};
nonAbuse.toArray()
}
};
func getNonAbuseVideos(caller: ?UserId, videos: [VideoId]) : ?[VideoInfo] {
do ? {
let nonAbuse = Buffer.Buffer<VideoInfo>(0);
for (v in videos.vals()) {
let flags = state.abuseFlagVideos.get1Size(v);
if (flags < Param.contentModerationThreshold) {
nonAbuse.add(getVideoInfo_(caller, v)!)
}
};
nonAbuse.toArray()
}
};
func getNonAbuseProfiles(users: [UserId]) : ?[ProfileInfo] {
do ? {
let nonAbuse = Buffer.Buffer<ProfileInfo>(0);
for (u in users.vals()) {
let flags = state.abuseFlagUsers.get1Size(u);
if (flags < Param.contentModerationThreshold) {
nonAbuse.add(getProfileInfo_(u)!)
}
};
nonAbuse.toArray()
}
};
func computeAllowance_(limitPerRecentDuration : Nat,
collectEvent : State.Event.Event -> Bool,
) : Types.AllowanceBalance {
if (limitPerRecentDuration == 0) {
#zeroForever
} else {
let now = timeNow_();
let matches = collectLogMatches(collectEvent);
if (matches.size() < limitPerRecentDuration) {
#nonZero (limitPerRecentDuration - matches.size()) // total remaining.
} else {
// assert invariant: we do not exceed the limit.
assert matches.size() == limitPerRecentDuration;
let leastRecentTime = matches[matches.size() - 1].time;
#zeroUntil (leastRecentTime + Param.recentPastDuration) // total wait.
}
}
};
// targetId -- for hashing the targets of abuse flags
func targetText(target : Types.ActionTarget) : Text {
switch target {
case (#video(i)) "#video=" # i;
case (#user(i)) "#user=" # i;
case _ { loop { assert false } };
}
};
// targetHash -- for collecting sets of targets, and doing set operations.
func targetHash(target : Types.ActionTarget) : Hash.Hash {
Text.hash(targetText(target))
};
func targetEqual(targ1 : Types.ActionTarget, targ2 : Types.ActionTarget) : Bool {
targ1 == targ2
};
func getUserAllowances_(user: UserId) : Types.UserAllowances {
{
abuseFlags = do {
let targets = TrieMap.TrieMap<Types.ActionTarget, Bool>(targetEqual, targetHash);
computeAllowance_(
Param.maxRecentAbuseFlags,
// true when we INCLUDE an event in the total
func (ev: State.Event.Event) : Bool {
switch (ev.kind) {
case (#abuseFlag(af)) {
if (af.reporter != user) { return false };
switch (targets.get(af.target)) {
case null {
targets.put(af.target, af.flag);
af.flag
};
case (?b) { b }
}};
case _ { false };
}
},
)};
superLikes = do {
let targets = TrieMap.TrieMap<Types.ActionTarget, Bool>(targetEqual, targetHash);
computeAllowance_(
Param.maxRecentSuperLikes,
func (ev: State.Event.Event) : Bool {
switch (ev.kind) {
case (#superLikeVideo(slv)) {
if (slv.source != user) { return false };
switch (targets.get(#video(slv.target))) {
case null {
targets.put(#video(slv.target), slv.superLikes);
slv.superLikes
};
case (?b) { b }
}};
case _ { false };
}
}
)};
}
};
func getProfilePlus_(caller: ?UserId, userId: UserId): ?ProfileInfoPlus {
do ? {
let profile = state.profiles.get(userId)!;
{
userName = profile.userName;
following = getNonAbuseProfiles(state.follows.get0(userId))!;
followers = getNonAbuseProfiles(state.follows.get1(userId))!;
likedVideos = getNonAbuseVideos(caller, state.likes.get0(userId))!;
uploadedVideos = getNonAbuseVideos(caller, state.uploaded.get0(userId))!;
hasPic = false;
rewards = state.rewards.get(userId)!;
abuseFlagCount = state.abuseFlagUsers.get1Size(userId) ; // count total for userId.
viewerHasFlagged = do ? { // if caller is non-null,
state.abuseFlagUsers.isMember(caller!, userId) ; // check if we are there.
};
allowances = do ? { if (caller! == userId) {
getUserAllowances_(caller!)
} else { null! } };
}
}
};
public query(msg) func getProfiles() : async ?[ProfileInfo] {
do ? {
accessCheck(msg.caller, #admin, #all)!;
let b = Buffer.Buffer<ProfileInfo>(0);
for ((p, _) in state.profiles.entries()) {
b.add(getProfileInfo_(p)!)
};
b.toArray()
}
};
public query(msg) func getVideos() : async ?[VideoInfo] {
do ? {
accessCheck(msg.caller, #admin, #all)!;
let b = Buffer.Buffer<VideoInfo>(0);
for ((v, _) in state.videos.entries()) {
b.add(getVideoInfo_(null, v)!)
};
b.toArray()
}
};
public query(msg) func getProfilePic(userId : UserId) : async ?ProfilePic {
do ? {
accessCheck(msg.caller, #view, #user userId)!;
state.profilePics.get(userId)!
}
};
public shared(msg) func putRewards(
receiver : UserId,
amount : Nat
) : async ?() {
do ? {
accessCheck(msg.caller, #admin, #user receiver)!;
let bal = state.rewards.get(receiver)!;
state.rewards.put(receiver, bal + amount);
}
};
public shared(msg) func putRewardTransfer(
sender : UserId,
receiver : UserId,
amount : Nat
) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user sender)!;
putRewardTransfer_(sender, receiver, amount)!
}
};
func putRewardTransfer_(sender : UserId,
receiver : UserId, amount : Nat) : ?() {
do ? {
let balSrc = state.rewards.get(sender)!;
let balTgt = state.rewards.get(receiver)!;
if (balSrc >= amount) {
state.rewards.put(sender, balSrc - amount);
state.rewards.put(receiver, balTgt + amount);
logEvent(#rewardPointTransfer({sender = sender; receiver = receiver; amount = amount}));
state.messages.put(receiver,
{ id = state.eventCount;
time = timeNow_();
event = #transferReward {
rewards = amount;
}
});
} else { return null }
}
};
public shared(msg) func putProfilePic(userId : UserId, pic : ?ProfilePic) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user userId)!;
switch pic {
case (?pic) { state.profilePics.put(userId, pic) };
case null { ignore state.profilePics.remove(userId) };
}
}
};
func getVideoResult(i : VideoId) : ?VideoResult {
do ? {
(getVideoInfo_(null, i)!, state.videoPics.get(i))
}
};
func getUserUploaded(userId : UserId, limit : ?Nat) : ?VideoResults {
do ? {
let buf = Buffer.Buffer<VideoResult>(0);
for (vid in state.uploaded.get0(userId).vals()) {
buf.add((getVideoResult vid)!)
};
buf.toArray()
}
};
func getFeedVideos_(userId : UserId, limit : ?Nat) : ?VideoResults {
do ? {
let vids = HashMap.HashMap<Text, ()>(0, Text.equal, Text.hash);
let _ = state.profiles.get(userId)!; // assert userId exists
let buf = Buffer.Buffer<VideoResult>(0);
let followIds = state.follows.get0(userId);
label loopFollows
for (i in followIds.vals()) {
switch limit { case null { }; case (?l) { if (buf.size() == l) { break loopFollows } } };
let vs = getUserUploaded(i, limit)!;
for ((vi, vp) in vs.vals()) {
if (vids.get(vi.videoId) == null) {
vids.put(vi.videoId, ());
buf.add((vi, vp));
}
}
};
label loopAll
for ((vid, v) in state.videos.entries()) {
switch limit { case null { }; case (?l) { if (buf.size() == l) { break loopAll } } };
if (vids.get(vid) == null) {
vids.put(vid, ());
let vPic = state.videoPics.get(vid);
let vi = getVideoInfo_(?userId, vid)!;
buf.add((vi, vPic));
}
};
buf.toArray()
}
};
public query(msg) func getFeedVideos(userId : UserId, limit : ?Nat) : async ?VideoResults {
do ? {
// privacy check: because we personalize the feed (example is abuse flag information).
accessCheck(msg.caller, #update, #user userId)!;
getFeedVideos_(userId, limit)!
}
};
public query(msg) func getProfileVideos(i : UserId, limit : ?Nat) : async ?VideoResults {
do ? {
accessCheck(msg.caller, #view, #user i)!;
let buf = Buffer.Buffer<VideoResult>(0);
let vs = getUserUploaded(i, limit)!;
for (v in vs.vals()) {
buf.add(v)
};
buf.toArray()
}
};
public query(msg) func getSearchVideos(userId : UserId, terms : [Text], limit : ?Nat) : async ?VideoResults {
do ? {
accessCheck(msg.caller, #view, #user userId)!;
getFeedVideos_(userId, limit)!;
}
};
// check if adding the source-target pair "now" in the log is valid.
// needed here (backend logic) and by front-end logic, when rendering enabled/disabled button status for superLike controls.
func getSuperLikeValidNow_(source : UserId, target : VideoId) : Bool {
let notRecent = timeNow_() - Param.recentPastDuration;
let superLiked = HashMap.HashMap<Text, Bool>(0, Text.equal, Text.hash);
superLiked.put(target, true);
var count = 1;
label hugeLog
for (ev in state.eventLog.revVals()) {
if(ev.time <= notRecent){ break hugeLog };
switch (ev.kind) {
case (#superLikeVideo(sl)) {
if(sl.source == source and superLiked.get(sl.target) == null){
superLiked.put(sl.target, sl.superLikes);
if (sl.superLikes) {
count += 1;
};
}
};
case _ { };
};
};
count <= Param.maxRecentSuperLikes;
};
func videoIsViral(videoId : VideoId) : Bool {
Option.isSome(do ? {
let v = state.videos.get(videoId)!;
v.viralAt!
})
};
/// Collect "recent events" that match from the log.
///
/// Visits events and orders array as most-to-least recent matching events.
/// (Most recent match is first visited and first in output, if any.
/// Least recent match is last visited and last in output, if any.)
///
/// Generalizes checkEmitVideoViral_.
///
/// This is "efficient enough" because we never check the full log,
/// and we intend to accelerate this operation further with
/// more pre-emptive caching of what we learn from doing this linear scan.
/// (Util this linear scan is too slow, let's avoid the complexity of more caching.)
func collectLogMatches(
collectEvent : State.Event.Event -> Bool,
) : [State.Event.Event] {
let now = timeNow_();
let notRecent = now - Param.recentPastDuration;
let matches = Buffer.Buffer<State.Event.Event>(0);
label hugeLog
for (ev in state.eventLog.revVals()) {
if(ev.time <= notRecent){ break hugeLog };
if (collectEvent(ev)) {
matches.add(ev)
}
};
matches.toArray()
};
// check if we need to emit viral video signal to CanCan logic.
func checkEmitVideoViral_(video : VideoId) {
let vinfo = Option.unwrap(state.videos.get(video));
if (Option.isSome(vinfo.viralAt)) {
return;
};
let now = timeNow_();
let notRecent = now - Param.recentPastDuration;
let superLiked = HashMap.HashMap<Text, Bool>(0, Text.equal, Text.hash);
let superLikers = Buffer.Buffer<State.Event.ViralVideoSuperLiker>(0);
label hugeLog
for (ev in state.eventLog.revVals()) {
if(ev.time <= notRecent){ break hugeLog };
switch (ev.kind) {
case (#superLikeVideo(sl)) {
if(sl.target == video and superLiked.get(sl.source) == null) {
superLiked.put(sl.source, sl.superLikes);
// The last sl.superLikes decide whether the user eventually liked the video or not
if (sl.superLikes) {
superLikers.add({ user = sl.source ; time = ev.time });
};
}
};
case _ { };
};
};
if(superLikers.size() >= Param.superLikeViralThreshold) {
state.videos.put(video,
{
userId = vinfo.userId ;
uploadedAt = vinfo.uploadedAt ;
viewCount = vinfo.viewCount ;
createdAt = vinfo.createdAt ;
viralAt = ?now;
caption = vinfo.caption ;
tags = vinfo.tags ;
name = vinfo.name ;
chunkCount = vinfo.chunkCount ;
});
/*
state.eventLog.add({time=now;
kind=#emitSignal(
#viralVideo{
video=video;
uploader=vinfo.userId;
superLikers=superLikers.toArray()}
)});*/
let score = Option.get(state.rewards.get(vinfo.userId), 0);
state.rewards.put(vinfo.userId, score + Param.rewardsForUploader);
state.eventCount += 1;
state.messages.put(vinfo.userId,
{ id = state.eventCount;
time = now;
event = #uploadReward {
rewards = Param.rewardsForUploader;
videoId = video;
}
});
for (id in superLikers.vals()) {
let score = Option.get(state.rewards.get(id.user), 0);
state.rewards.put(id.user, score + Param.rewardsForSuperliker);
state.eventCount += 1;
state.messages.put(id.user,
{ id = state.eventCount;
time = now;
event = #superlikerReward {
rewards = Param.rewardsForSuperliker;
videoId = video;
}
});
};
}
};
public query(msg) func getMessages(user: UserId) : async ?[Types.Message] {
do ? {
accessCheck(msg.caller, #view, #user user)!;
state.messages.get0(user)
}
};
public query(msg) func isDropDay() : async ?Bool {
do ? {
accessCheck(msg.caller, #view, #pubView)!;
let now = timeNow_();
now % (Param.dropDayDuration + Param.dropDayNextDuration) < Param.dropDayDuration
}
};
public query(msg) func getSuperLikeValidNow(source : UserId, target : VideoId) : async ?Bool {
do ? {
accessCheck(msg.caller, #view, #user target)!;
getSuperLikeValidNow_(source, target)
}
};
public query(msg) func getIsSuperLiker(source : UserId, target : VideoId) : async ?Bool {
do ? {
accessCheck(msg.caller, #view, #user target)!;
state.superLikes.isMember(source, target)
}
};
func putSuperLike_(userId : UserId, videoId : VideoId, superLikes_ : Bool) : ?() {
do ? {
let _ = state.videos.get(videoId)!; // assert that the videoId is valid
if superLikes_ {
if (getSuperLikeValidNow_(userId, videoId)) {
state.superLikes.put(userId, videoId);
logEvent(#superLikeVideo({ source = userId ;
target = videoId ;
superLikes = true }
));
checkEmitVideoViral_(videoId);
} else {
logEvent(#superLikeVideoFail({ source = userId ;
target = videoId }));
return null // fail
}
} else {
state.superLikes.delete(userId, videoId);
logEvent(#superLikeVideo({ source = userId ;
target = videoId ;
superLikes = false }));
}
}
};
public shared(msg) func putSuperLike
(userId : UserId, videoId : VideoId, willSuperLike : Bool) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user userId)!;
putSuperLike_(userId, videoId, willSuperLike)!
}
};
public shared(msg) func putProfileVideoLike
(userId : UserId, videoId : VideoId, willLike_ : Bool) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user userId)!;
if willLike_ {
state.likes.put(userId, videoId);
} else {
state.likes.delete(userId, videoId)
};
logEvent(#likeVideo({ source = userId ;
target = videoId ;
likes = willLike_ }));
}
};
func putProfileFollow_
(userId : UserId, followedBy : UserId, follows : Bool) : ?() {
if (userId == followedBy) { return null };
if follows {
state.follows.put(userId, followedBy)
} else {
state.follows.delete(userId, followedBy)
};
?()
};
public shared(msg) func putProfileFollow
(userId : UserId, toFollow : UserId, follows : Bool) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user userId)!;
putProfileFollow_(userId, toFollow, follows)!
}
};
// internal function for adding metadata
func createVideo_(i : VideoInit) : ?VideoId {
let now = timeNow_();
let videoId = i.userId # "-" # i.name # "-" # (Int.toText(now));
switch (state.videos.get(videoId)) {
case (?_) { /* error -- ID already taken. */ null };
case null { /* ok, not taken yet. */
state.videos.put(videoId,
{
videoId = videoId;
userId = i.userId ;
name = i.name ;
createdAt = i.createdAt ;
uploadedAt = now ;
viralAt = null ;
caption = i.caption ;
chunkCount = i.chunkCount ;
tags = i.tags ;
viewCount = 0 ;
});
state.uploaded.put(i.userId, videoId);
logEvent(#createVideo({info = i}));
?videoId
};
}
};
public shared(msg) func createVideo(i : VideoInit) : async ?VideoId {
do ? {
accessCheck(msg.caller, #update, #user(i.userId))!;
createVideo_(i)!
}
};
func getVideoInfo_ (caller : ?UserId, videoId : VideoId) : ?VideoInfo {
do ? {
let v = state.videos.get(videoId)!;
{
videoId = videoId;
pic = state.videoPics.get(videoId);
userId = v.userId ;
createdAt = v.createdAt ;
uploadedAt = v.uploadedAt ;
viralAt = v.viralAt ;
caption = v.caption ;
tags = v.tags ;
likes = state.likes.get1(videoId);
superLikes = state.superLikes.get1(videoId);
viewCount = v.viewCount ;
name = v.name ;
chunkCount = v.chunkCount ;
// This implementation makes public all users who flagged every video,
// but if that information should be kept private, get video info
// could return just whether the calling user flagged it.
viewerHasFlagged = do ? {
state.abuseFlagVideos.isMember(caller!, videoId) ;
};
abuseFlagCount = state.abuseFlagVideos.get1Size(videoId);
}
}
};
public query(msg) func getVideoInfo (caller : ?UserId, target : VideoId) : async ?VideoInfo {
do ? {
accessCheck(msg.caller, #view, #video target)!;
switch caller {
case null { getVideoInfo_(null, target)! };
case (?callerUserName) {
// has private access to our caller view?
accessCheck(msg.caller, #update, #user callerUserName)!;
getVideoInfo_(?callerUserName, target)!
};
}
}
};
public query(msg) func getVideoPic(videoId : VideoId) : async ?VideoPic {
do ? {
accessCheck(msg.caller, #view, #video videoId)!;
state.videoPics.get(videoId)!
}
};
public shared(msg) func putVideoInfo(videoId : VideoId, videoInit : VideoInit) : async ?() {
do ? {
accessCheck(msg.caller, #update, #video videoId)!;
let i = videoInit ;
let v = state.videos.get(videoId)!;
state.videos.put(videoId,
{
// some fields are "immutable", regardless of caller data:
userId = v.userId ;
uploadedAt = v.uploadedAt ;
viewCount = v.viewCount ;
videoId = videoId ;
// -- above uses old data ; below is from caller --
createdAt = i.createdAt ;
viralAt = null;
caption = i.caption ;
tags = i.tags ;
name = i.name ;
chunkCount = i.chunkCount ;
})
}
};
/// An abuse flag for a video occurs when a reporting user
/// sets or clears the abuse toggle in their UI for the video.
public shared (msg) func putAbuseFlagVideo
(reporter : UserId, target : VideoId, abuseFlag : Bool) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user reporter)!;
logEvent(#abuseFlag({ reporter = reporter ;
target = #video(target);
flag = abuseFlag }));
if abuseFlag {
state.abuseFlagVideos.put(reporter, target)
} else {
state.abuseFlagVideos.delete(reporter, target)
};
}
};
/// An abuse flag for a user occurs when a reporting user
/// sets or clears the abuse toggle in their UI for the target user.
public shared(msg) func putAbuseFlagUser
(reporter : UserId, target : UserId, abuseFlag : Bool) : async ?() {
do ? {
accessCheck(msg.caller, #update, #user reporter)!;
logEvent(#abuseFlag({ reporter = reporter ;
target = #user(target);
flag = abuseFlag }));
if abuseFlag {
state.abuseFlagUsers.put(reporter, target)
} else {
state.abuseFlagUsers.delete(reporter, target)
}
}
};
public shared(msg) func putVideoPic(videoId : VideoId, pic : ?VideoPic) : async ?() {
do ? {
accessCheck(msg.caller, #update, #video videoId)!;
switch pic {
case (?pic) { state.videoPics.put(videoId, pic) };
case null {
switch (state.videoPics.remove(videoId)) {
case null { /* not found */ return null };
case _ { /* found and removed. */ };
}
};
}
}
};
func chunkId(videoId : VideoId, chunkNum : Nat) : ChunkId {
videoId # (Nat.toText(chunkNum))
};
public shared(msg) func putVideoChunk
(videoId : VideoId, chunkNum : Nat, chunkData : [Nat8]) : async ?()
{
do ? {
accessCheck(msg.caller, #update, #video videoId)!;
state.chunks.put(chunkId(videoId, chunkNum), chunkData);
}
};
public query(msg) func getVideoChunk(videoId : VideoId, chunkNum : Nat) : async ?[Nat8] {
do ? {
accessCheck(msg.caller, #view, #video videoId)!;
state.chunks.get(chunkId(videoId, chunkNum))!
}
};
func createTestData_(users : [UserId], videos : [(UserId, VideoId)]) : ?() {
do ? {
for (u in users.vals()) {
createProfile_(u, null, null)!;
};
for ((u, v) in videos.vals()) {
let _ = createVideo_(
{userId = u ;
name = v ;
createdAt = timeNow_() ;
chunkCount = 0;
caption = "";
tags = [ ];})!;
};
}
};
public shared(msg) func createTestData(users : [UserId], videos : [(UserId, VideoId)]) : async ?() {
do ? {
accessCheck(msg.caller, #admin, #all)!;
createTestData_(users, videos)!
}
};
public shared(msg) func putTestFollows(follows : [(UserId, UserId)]) : async ?() {
do ? {
accessCheck(msg.caller, #admin, #all)!;
for ((u, v) in follows.vals()) {
let _ = putProfileFollow_(u, v, true)!;
}
}
};
public query(msg) func getEventLog() : async ?[State.Event.Event] {
do ? {
accessCheck(msg.caller, #admin, #all)!;
Iter.toArray(state.eventLog.vals())
}
};
public query(msg) func getAccessLog() : async ?[Access.Log.Event.Event] {
do ? {
accessCheck(msg.caller, #admin, #all)!;
Iter.toArray(state.access.log.vals())
}
};
func doDemo_(script : [Demo.Command]) : Demo.Trace {
let trace = Buffer.Buffer<Demo.TraceCommand>(0);
let r = do ? {
for (cmd in script.vals()) {
switch cmd {