import Iter "mo:base/Iter";
import List "mo:base/List";
import Principal "mo:base/Principal";
import Time "mo:base/Time";
import Buffer "mo:base/Buffer";
import Array "mo:base/Array";
import Int "mo:base/Int";
actor {
public type Message = {
text: Text;
time: Time.Time;//该时间为从1970-01-01到现在经历的秒数
author: Text;
};
public type FollowInfo = {
name: Text;
id : Text;
};
public type Microblog = actor {
follow: shared(Principal) -> async ();
follows: shared query () -> async [Principal];
post: shared (Text,Text) -> async ();
posts: shared query () -> async [Message];
timeline: shared () -> async [Message];
set_name : shared (Text) -> async ();
get_name : shared query () -> async (?Text);
};
var authors: ?Text = null;
public shared func set_name(text2: Text) : async () {
authors := ?text2;
};
public query func get_name() : async ?Text {
authors
};
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 [FollowInfo] {
var all : List.List<FollowInfo> = List.nil();
for(id in Iter.fromList(followed)){
ignore do ? {
let canister : Microblog = actor(Principal.toText(id));
let name = (await canister.get_name()) !;
let tmp : FollowInfo = {name = name;id =Principal.toText(id)};
all := List.push<FollowInfo>(tmp, all);
}
};
return List.toArray(all);
};
var messages : List.List<Message> = List.nil();
public shared func post(otp:Text, text1: Text) : async () {
assert(otp == "123456");
let _author = switch (auth) {
case (?a) { a };
case (null) { "" };
};
let message1 = {
text = text1;
time = Time.now();
author=_author;
};
messages := List.push(message1,messages)
};
public shared query func posts() : 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 msgs = await canister.posts();
for (msg in Iter.fromArray(msgs)) {
all := List.push(msg,all);
};
};
List.toArray(all);
};
};
|