-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson_4_source_code
60 lines (44 loc) · 1.69 KB
/
Lesson_4_source_code
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
import Iter "mo:base/Iter";
import List "mo:base/List";
import Principal "mo:base/Principal";
import Time "mo:base/Time";
import Debug "mo:base/Debug";
import Text "mo:base/Text";
import Nat64 "mo:base/Nat64";
import Int "mo:base/Int";
actor {
public type Message = Text;
public type Microblog = actor {
follow: shared(Principal) -> async (); // 增添关注对象
follows: shared query() -> async [Principal]; // 返回关注列表
post: shared(Text) -> async (); // 发布新消息
posts: shared query() -> async [Message]; // 返回所有发布的消息
timeline: shared () -> async [Message]; // 返回所有关注对象发布的消息
};
stable var followed : List.List<Principal> = List.nil();
public shared func follow(id: Principal) : async (){
followed := List.push(id, followed);
};
public shared query func follows() : async [Principal]{
List.toArray(followed);
};
stable var messages : List.List<Message> = List.nil();
public shared func post(text: Text) : async (){
messages := List.push(text, messages);
};
public shared query func posts(s : Int) : async [Message]{
List.toArray(messages);
};
public shared func timeline() : async [Message]{
var all : List.List<Message> = List.nil();
for (id in Iter.fromList(followed)){
let canister : Microblog = actor(Principal.toText(id));
let s : Int = 5;
let msgs = await canister.posts(s);
for (msg in Iter.fromArray(msgs)){
all := List.push(msg, all);
};
};
List.toArray(all);
};
};