import List "mo:base/List";
import Iter "mo:base/Iter";
import Principal "mo:base/Principal";
import Time "mo:base/Time";
actor {
//作业要求的Message记录结构
public type Message = {
text : Text;
time : Time.Time;
};
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];
};
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)
};
var messages : List.List<Message> = List.nil();
//作业要求的在post方法中,创建新的Message并记录当前时间
public shared func post(text : Text) : async () {
let mesg : Message = {
text = text;
time = Time.now();
};
messages := List.push(mesg, messages);
};
//作业要求的修改posts方法,仅返回指定时间后的内容
public shared query func posts(since : Time.Time) : async [Message] {
var all : List.List<Message> = List.nil();
let msgs = List.toArray(messages);
for (msg in Iter.fromArray(msgs)){
if(msg.time > since){
all := List.push(msg,all);
};
};
List.toArray(all)
};
//作业要求的修改timeline方法,仅返回指定时间后的内容
public shared func timeline(since : Time.Time) : async [Message] {
var all : List.List<Message> = List.nil();
for(id in Iter.fromList(followed)){
let canister :Microblog = actor(Principal.toText(id));
let msgs = await canister.posts();
for (msg in Iter.fromArray(msgs)){
if(msg.time > since){
all := List.push(msg,all);
};
};
};
List.toArray(all)
};
};