Skip to content

Commit

Permalink
replies count added
Browse files Browse the repository at this point in the history
  • Loading branch information
tempe-techie committed Sep 19, 2024
1 parent 66e8edc commit 5168198
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions contracts/chat/ChatContextV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ contract ChatContextV1 is Ownable {

struct Message {
address author;
string url; // URL pointing to the message stored on Arweave or IPFS etc., e.g. ar://some-identifier, ipfs://someIdentifier, ...
uint256 createdAt; // timestamp when the message was created
bool deleted; // whether the message is deleted or not
uint256 repliesCount; // number of replies to the message
string url; // URL pointing to the message stored on Arweave or IPFS etc., e.g. ar://some-identifier, ipfs://someIdentifier, ...
}

// CONSTRUCTOR
Expand Down Expand Up @@ -142,7 +143,8 @@ contract ChatContextV1 is Ownable {
author: msg.sender,
url: url_,
createdAt: block.timestamp,
deleted: false
deleted: false,
repliesCount: 0
});

mainMessages.push(newMsg);
Expand All @@ -163,10 +165,12 @@ contract ChatContextV1 is Ownable {
author: msg.sender,
url: url_,
createdAt: block.timestamp,
deleted: false
deleted: false,
repliesCount: 0
});

replies[mainMsgIndex_].push(newReply);
mainMessages[mainMsgIndex_].repliesCount++;
emit MessageReplied(msg.sender, url_, mainMsgIndex_, block.timestamp);
}

Expand Down Expand Up @@ -196,6 +200,7 @@ contract ChatContextV1 is Ownable {
"Not the author or owner"
);
replies[mainMsgIndex_][replyMsgIndex_].deleted = true;
mainMessages[mainMsgIndex_].repliesCount--;
emit ReplyDeleted(msg.sender, replies[mainMsgIndex_][replyMsgIndex_].url, mainMsgIndex_, replyMsgIndex_, block.timestamp);
}

Expand Down
11 changes: 10 additions & 1 deletion test/chat/chatContextV1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ describe("ChatContextV1", function () {

it("allows creating a reply", async function () {
await chatContract.connect(user2).createMessage("ipfs://message1");

// get replies count before
const message1 = await chatContract.getMainMessage(0);
expect(message1.repliesCount).to.equal(0);

const tx = await chatContract.connect(user3).createReply(0, "ipfs://reply1");
await expect(tx).to.emit(chatContract, "MessageReplied")
.withArgs(user3.address, "ipfs://reply1", 0, await ethers.provider.getBlock('latest').then(b => b.timestamp));


// get replies count after
const message2 = await chatContract.getMainMessage(0);
expect(message2.repliesCount).to.equal(1);

const reply = await chatContract.getReply(0, 0);
expect(reply.author).to.equal(user3.address);
expect(reply.url).to.equal("ipfs://reply1");
Expand Down

0 comments on commit 5168198

Please sign in to comment.