Motoko:
import List "mo:base/List";import Iter "mo:base/Iter";import Principal "mo:base/Principal";import Time "mo:base/Time";import Nat "mo:base/Nat";actor {public type Message = {author: ?Text;text: Text;time: Time.Time;};public type User= {uid: Principal;uname: ?Text;};var king: Text = "Linnuo";public type Microblog = actor {follow: shared(Principal) -> async (); // add關(guān)注對象follows: shared query () -> async [User]; // return關(guān)注對象列表post: shared (Text) -> async (); // 發(fā)布新消息posts: shared (Time.Time) -> async [Message]; // return 發(fā)布消息列表posts2: shared (Text) -> async [Message]; // return 所有關(guān)注對象發(fā)布的消息timeline: shared (Time.Time) -> async [Message]; // return 所有關(guān)注對象發(fā)布的消息set_name: shared (Text) -> async ();// set 名字get_name: shared query () -> async ?Text; // get 名字unfollow: shared () -> async ();// 清空跟隨};// stable 修飾:升級不清空內(nèi)存stable var followed : List.List<User> = List.nil();public shared func follow(id: Principal) : async (){let canister : Microblog = actor(Principal.toText(id));let name: ?Text = await canister.get_name();let u = {uid = id;uname = name;};followed := List.push(u, followed);};public shared query func follows() : async [User] {List.toArray(followed);};stable var messages : List.List<Message> = List.nil();// (msg):獲取消息屬性public shared (msg) func post(pwd: Text, text: Text) : async (){// 獲取發(fā)送者: dfx identity get-principal// assert(Principal.toText(msg.caller) == "vw7ov-537vk-abslh-s2gx7-gw2ff-v4u6y-thlcs-hejxf-hkkc5-bjiq7-pqe"); //消息發(fā)送者assert(pwd == "qwe123");let m = {author = ?king;text = text;time = Time.now();};messages := List.push(m, messages);// 用錢包調(diào)用正常返回失敗:dfx canister --wallet=$(dfx identity get-wallet) call microblog_backend post "(\"Second post\")"};public shared func posts(since: Time.Time) : async [Message] {var list : List.List<Message> = List.nil();for (m in Iter.fromList(messages)) {if (m.time >= since){list := List.push(m, list);}};List.toArray(list);};public shared func posts2(pid: Principal, since: Time.Time) : async [Message] {try {let canister : Microblog = actor(Principal.toText(pid));await canister.posts(since);} catch (err) {[]}};public shared func timeline(since: Time.Time) : async [Message] {var all : List.List<Message> = List.nil();for (user in Iter.fromList(followed)){let canister : Microblog = actor(Principal.toText(user.uid));let msgs = await canister.posts(since);for(msg in Iter.fromArray(msgs)){all := List.push(msg, all);};};List.toArray(all);};public shared func set_name(name: Text) {king := name;};public shared query func get_name() : async ?Text {return ?king;};public shared func unfollow() : async (){followed := List.nil();};// 發(fā)送消息:id和名稱可以呼喚//dfx canister call rrkah-fqaaa-aaaaa-aaaaq-cai post "(\"First post\")"http:// 用Id發(fā)消息//dfx canister call microblog_backend post "(\"Second post\")"http:// 用名稱發(fā)消息//dfx canister call rrkah-fqaaa-aaaaa-aaaaq-cai posts "()"http:// 獲取消息列表//dfx canister call microblog_backend2 follow "(principal \"$(dfx canister id microblog_backend)\")"http:// 添加關(guān)注對象//dfx canister call microblog_backend2 follows "()"http:// 獲取關(guān)注對象列表//dfx canister call microblog_backend2 timeline"()"http:// 獲取所有關(guān)注對象發(fā)布的消息};代碼的實現(xiàn)并不復(fù)雜 , 歡迎童鞋們指導(dǎo)哈
為了避免直接下載項目 , 這里就不貼 Github 入口了 , 好好學(xué)習(xí) , 加油
經(jīng)驗總結(jié)擴展閱讀
- jmeter錄制登錄腳本
- 漂洗是什么意思
- 正能量干凈的笑話段子
- 長寧區(qū)美食
- 葷食與素食結(jié)合者這遵循了什么
- 孕婦用什么補水面膜比較好
- 比特幣漲幅會影響狗狗幣嗎
- 淘寶店怎么開通
- 怎么取消鎖屏的時間
- 野山椒白肉
