Skip to content

Commit

Permalink
bug fix & test update
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuanxun committed Nov 26, 2023
1 parent 0db068e commit 738bc72
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 42 deletions.
9 changes: 7 additions & 2 deletions src/feed/database.mo
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,19 @@ module {
};

public func getLatestFeed(n: Nat): [PostImmutable] {
Array.subArray(Iter.toArray(
let feedArray = Iter.toArray(
Iter.sort<PostImmutable>(
feedMap.vals(),
func (x: PostImmutable, y: PostImmutable): Order.Order {
if(x.createdAt > y.createdAt) return #less
else if(x.createdAt < y.createdAt) return #greater
else return #equal
})), 0, n)
}));
if(n <= feedArray.size()) {
Array.subArray(feedArray, 0, n)
} else {
Array.subArray(feedArray, 0, feedArray.size())
}
};

};
Expand Down
2 changes: 1 addition & 1 deletion src/feed/feed.mo
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ actor class Feed(
switch((await bucketActor.getPost(_postId))) {
case(null) { };
case(?_post) {

Debug.print("Canister Feed, Func batchReceiveComment");
feedDirectory.storeFeed(_post);

if(Utils._isRepostUser(_post, owner)) {
Expand Down
10 changes: 7 additions & 3 deletions src/fetch/commentFetch.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Types "./types";
import Array "mo:base/Array";
import Timer "mo:base/Timer";
import Iter "mo:base/Iter";
import Debug "mo:base/Debug";

actor class CommentFetch(
userCanister: Principal
Expand Down Expand Up @@ -33,8 +34,7 @@ actor class CommentFetch(
notifyMap.put(_follower, [postId]);
};
case(?_postIdArray) {
let _newPostIdArray = Array.append(_postIdArray, [postId]);
notifyMap.put(_follower, _newPostIdArray);
notifyMap.put(_follower, Array.append(_postIdArray, [postId]));
};
};
};
Expand Down Expand Up @@ -78,12 +78,16 @@ actor class CommentFetch(
type FeedActor = Types.FeedActor;

func notify(): async () {
for((_user, _postIdArray) in notifyMap.entries()) {
// Debug.print("commentFetch notify !");
let _notifyMap = notifyMap;
for((_user, _postIdArray) in _notifyMap.entries()) {
switch(userToFeed.get(_user)) {
case(null) { };
case(?_feedId) {
Debug.print("commentFetch Notify feed canister " # Principal.toText(_feedId));
let feedActor: FeedActor = actor(Principal.toText(_feedId));
ignore feedActor.batchReceiveComment(_postIdArray);
notifyMap.delete(_user);
};
};
};
Expand Down
6 changes: 5 additions & 1 deletion src/fetch/likeFetch.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Types "./types";
import Array "mo:base/Array";
import Timer "mo:base/Timer";
import Iter "mo:base/Iter";
import Debug "mo:base/Debug";

actor class LikeFetch(
userCanister: Principal
Expand Down Expand Up @@ -77,12 +78,15 @@ actor class LikeFetch(
type FeedActor = Types.FeedActor;

func notify(): async () {
for((_user, _postIdArray) in notifyMap.entries()) {
// Debug.print("likeFetch notify !");
let _notifyMap = notifyMap;
for((_user, _postIdArray) in _notifyMap.entries()) {
switch(userToFeed.get(_user)) {
case(null) { };
case(?_feedId) {
let feedActor: FeedActor = actor(Principal.toText(_feedId));
ignore feedActor.batchReceiveLike(_postIdArray);
notifyMap.delete(_user);
};
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/fetch/postFetch.mo
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ actor class PostFetch() = this {
// 根据算法用 ignore call 分批次通知 followers 的 Feed 。
// 用 Timers 来处理
func notify(): async () {
Debug.print("postFetch notify !");
// Debug.print("postFetch notify !");
let _notifyMap = notifyMap;
for((_user, _postIdArray) in _notifyMap.entries()) {
switch(userToFeed.get(_user)) {
Expand Down
21 changes: 13 additions & 8 deletions src/post/bucket.mo
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,19 @@ shared(msg) actor class Bucket(

// 查询最新的 n 个帖子
public query func getLatestFeed(n: Nat): async [PostImmutable] {
Array.subArray(Iter.toArray(
Iter.sort<PostImmutable>(
feedMap.vals(),
func (x: PostImmutable, y: PostImmutable): Order.Order {
if(x.createdAt > y.createdAt) return #less
else if(x.createdAt < y.createdAt) return #greater
else return #equal
})), 0, n)
let feedArray = Iter.toArray(
Iter.sort<PostImmutable>(
feedMap.vals(),
func (x: PostImmutable, y: PostImmutable): Order.Order {
if(x.createdAt > y.createdAt) return #less
else if(x.createdAt < y.createdAt) return #greater
else return #equal
}));
if(n <= feedArray.size()) {
Array.subArray(feedArray, 0, n)
} else {
Array.subArray(feedArray, 0, feedArray.size())
}
};

// CommentFetchCanister
Expand Down
2 changes: 1 addition & 1 deletion src/utils.mo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module {
let bucket = Principal.fromText(words[0]);
let user = Principal.fromText(words[1]);
let postIndex = Option.unwrap(Nat.fromText(words[2]));
Debug.print("(bucket, user, index) : (" # words[0] # "," # words[1] # "," # words[2] # ")");
// Debug.print("(bucket, user, index) : (" # words[0] # "," # words[1] # "," # words[2] # ")");
(bucket, user, postIndex)
};

Expand Down
21 changes: 18 additions & 3 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ rootFeed_canister_id=$(dfx canister id rootFeed)

echo "增发 cycles"
wallet=$(dfx identity get-wallet)
dfx ledger fabricate-cycles --t 1000 --canister $wallet
dfx ledger fabricate-cycles --t 2000 --canister $wallet
dfx wallet balance

# 给 rootFeed canister 充值cycles
echo "给 rootFeed canister 充值 100T cycles"
dfx wallet send $rootFeed_canister_id 100000000000000
echo "给 rootFeed canister 充值 200T cycles"
dfx wallet send $rootFeed_canister_id 200000000000000
echo "查询 rootFeed canister 状态"
dfx canister status $rootFeed_canister_id

Expand All @@ -76,6 +76,21 @@ dfx wallet send $rootFetch_canister_id 100000000000000
echo "查询 rootPost canister 状态"
dfx canister status $rootFetch_canister_id

echo "给 postFetch canister 充值 50T cycles"
dfx wallet send $postFetch_canister_id 50000000000000
echo "查询 postFetch canister 状态"
dfx canister status $postFetch_canister_id

echo "给 commentFetch canister 充值 50T cycles"
dfx wallet send $commentFetch_canister_id 50000000000000
echo "查询 commentFetch canister 状态"
dfx canister status $commentFetch_canister_id

echo "给 likeFetch canister 充值 50T cycles"
dfx wallet send $likeFetch_canister_id 50000000000000
echo "查询 likeFetch canister 状态"
dfx canister status $likeFetch_canister_id

echo "初始化 rootPost"
dfx canister call rootPost init

Expand Down
9 changes: 9 additions & 0 deletions test/js/feed.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ export class Feed {
const result = await this.actor.getFeedNumber();
return result;
}

async getFeed(postId) {
return await this.actor.getFeed(postId);
}

async getLatestFeed(n) {
return await this.actor.getLatestFeed(n);
}

}
10 changes: 0 additions & 10 deletions test/js/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ const identity = Secp256k1KeyIdentity.fromSeedPhrase(seed);

export const identityTest = identity;

export const identityA = Secp256k1KeyIdentity.generate();

export const identityB = Secp256k1KeyIdentity.generate();

export const identityC = Secp256k1KeyIdentity.generate();

export const identityD = Secp256k1KeyIdentity.generate();

export const identityE = Secp256k1KeyIdentity.generate();

export function newIdentity() {
return Secp256k1KeyIdentity.generate();
}
Loading

0 comments on commit 738bc72

Please sign in to comment.