Skip to content

Instantly share code, notes, and snippets.

@MCarlomagno
Last active January 24, 2022 20:30
Show Gist options
  • Save MCarlomagno/a083d7bccfb320c4138b61f4e44838f8 to your computer and use it in GitHub Desktop.
Save MCarlomagno/a083d7bccfb320c4138b61f4e44838f8 to your computer and use it in GitHub Desktop.
pragma solidity >=0.7.0 <0.9.0;
contract Chat {
// the list of old messages in the chat
mapping(uint => Message) public messagesList;
function listMessages() public constant returns (uint[]){
// if the chat is empty
if(lastMessageId == 0) {
return new uint[](0);
}
// give me the ids.
uint[] memory ids = new uint[](lastMessageId);
// loads all the message ids on 'ids' list.
for (uint i = 1; i <= lastMessageId; i++) {
// if the sender is different than me.
if(messages[i].sender != msg.sender) {
ids[numOfMessages] = messagesList[i].id;
}
}
return ids;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment