import Iter "mo:base/Iter";
import List "mo:base/List";
import Microblog "mo:base/Principal";
import Principal "mo:base/Principal";
import Time "mo:base/Time";
actor {
public type Message = {
content : Text;
time : Time.Time;
author: Text;
};
public type Follow_info = {
name : ?Text;
cid : Text;
};
public type Microblog = actor {
follow: shared(Principal) -> async ();
follows: shared query () -> async [Principal];
post: shared (Text) -> async ();
posts: shared query (Int) -> async [Message];
timeline: shared () -> async [Message];
set_name: shared (Text) -> async ();
get_name: shared query () -> async ?Text;
visit_count: shared () -> async ();
get_visit_time: shared query () -> async Int;
someone_posts: shared (Text) -> async [Message];
get_follow_infos: shared () -> async [Follow_info];
update_follow_infos: shared () -> async ();
reset_follows: shared () -> async ();
follow_by_text: shared (Text) -> async ();
};
stable var follow_infos : List.List<Follow_info> = List.nil();
stable var followed : List.List<Principal> = List.nil();
stable var name : Text = "nil";
stable var visit_time : Int = 0;
public shared func visit_count() : async (){
visit_time := visit_time + 1;
};
public shared query func get_visit_time() : async Int{
visit_time
};
public shared (msg) func set_name(text : Text) : async () {
name := text;
};
public shared query func get_name() : async Text{
name
};
public shared func get_follow_infos() : async [Follow_info]{
List.toArray(follow_infos);
};
public shared func update_follow_infos() : async (){
follow_infos := List.nil();
for (id in Iter.fromList(followed)) {
let canister : Microblog = actor(Principal.toText(id));
let ms = await canister.get_name();
let info : Follow_info = {name=ms;cid=Principal.toText(id)};
follow_infos := List.push(info,follow_infos);
};
};
public shared func follow(id : Principal) : async (){
var unfollowed = true;
for(p in Iter.fromList(followed)){
if(p.equal(id)){
unfollowed = false;
}
}
if(unfollowed){
followed := List.push(id,followed);
await update_follow_infos();
}
};
public shared func follow_by_text(cid : Text) : async (){
let id : Principal = Principal.fromText(cid);
var unfollowed = true;
for(p in Iter.fromList(followed)){
if(p.equal(id)){
unfollowed = false;
}
}
if(unfollowed){
followed := List.push(id,followed);
await update_follow_infos();
}
};
public shared func reset_follows() : async (){
followed := List.nil();
};
public shared query func follows() : async [Principal]{
List.toArray(followed)
};
stable var messages : List.List<Message> = List.nil();
public shared (msg) func post(text : Text) : async () {
let message : Message = { content=text; time = Time.now(); author = name};
messages := List.push(message, messages)
};
public shared query func posts(since: Time.Time) : async [Message]{
var res : List.List<Message> = List.nil();
for(msg in Iter.fromList(messages)){
if(msg.time > since){
res := List.push(msg, res);
};
};
List.toArray(res)
};
public shared (msg) func timeline(since: Time.Time) : async [Message] {
var msgs : List.List<Message> = List.nil();
for (id in Iter.fromList(followed)) {
let canister : Microblog = actor(Principal.toText(id));
let ms = await canister.posts(since);
for (msg in Iter.fromArray(ms)) {
msgs := List.push(msg, msgs);
}
};
List.toArray(msgs);
};
public shared func someone_posts(cid: Text) : async [Message]{
let canister : Microblog = actor(cid);
let ms = await canister.posts(0);
ms;
}
};
作业5-motoko源码
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- java 企业网站源码 前后台都有 静态模版引擎, 代码生成器大大提高开发效率 前台: 支持三套模版, 可以在后台...
- 最近做了一个二手闲置免费信息发布的小程序,功能比较简单易懂,花费了一个多月进行构思设计和开发,可做为毕业设计或者计...
- ->点击访问个人博客地址,相互交流学习<- C++程序设计 利用C++实现的小游戏:2048,俄罗斯方块和贪吃蛇[...
- 数据详情 数据详情 日课35:“我们不确定情境下的人生重大决策的秘密——当你“意识”到要做一个决定的时候,你的情绪...
- 在分布式的场景下由于网络、时钟等原因,可能导致Zookeeper的数据与真实运行的作业产生不一致,这种不一致通过正...