Skip to content

Commit

Permalink
add reply threads
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed Oct 4, 2022
1 parent a24b486 commit fc7c58d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
52 changes: 45 additions & 7 deletions contracts/SCPostStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,33 @@ contract SCPostStorage is Ownable, ERC2771Recipient, Versioned {
uint256 public maxPostLength;
uint256 public infixLength;
Counters.Counter public currentPostId;
address public replyAllAddress;

// Events
event PostSaved(
uint256 id,
string post,
address indexed derivativeAddress,
address indexed sender,
uint256 timestamp
uint256 timestamp,
uint256 threadId,
uint256 replyToId
);

constructor(
address _ledgerAddress,
uint256 _maxPostLength,
uint256 _infixLength,
address _forwarder,
string memory _version
string memory _version,
address _replyAllAddress
) Versioned(_version) {
ledgerAddress = _ledgerAddress;
maxPostLength = _maxPostLength;
infixLength = _infixLength;
_setTrustedForwarder(_forwarder);
version = _version;
replyAllAddress = _replyAllAddress;
}

/**
Expand All @@ -123,6 +128,13 @@ contract SCPostStorage is Ownable, ERC2771Recipient, Versioned {
infixLength = _infixLength;
}

/**
* @dev Modifies reply all address
*/
function setReplyAllAddress(address _replyAllAddress) external onlyOwner {
replyAllAddress = _replyAllAddress;
}

/**
* @dev Returns posts
*/
Expand All @@ -148,7 +160,12 @@ contract SCPostStorage is Ownable, ERC2771Recipient, Versioned {
/**
* @dev Adds a post to the storage
*/
function savePost(string memory post, string memory original) external {
function savePost(
string memory post,
string memory original,
uint256 threadId,
uint256 replyToId
) external {
// Get the derivative
address derivativeAddress = ILedger(ledgerAddress).getDerivative(original);
// Check preconditions
Expand All @@ -165,20 +182,41 @@ contract SCPostStorage is Ownable, ERC2771Recipient, Versioned {
symbolSuffixLength,
"Post exceeds max post length"
);
require(threadId <= currentPostId.current(), "Thread not found");
// Check abitily to post
if (threadId > 0) {
require(replyToId > 0, "replyToId must be provided with threadId");
Post memory threadPost = posts[threadId - 1];
require(
threadPost.sender == _msgSender() ||
threadPost.sender == replyAllAddress,
"You can only reply to your own thread or to the thread of replyAllAddress"
);
}
// Increment the current post id
currentPostId.increment();
// Post the post
uint256 id = currentPostId.current();
Post memory newPost = Post(
id,
post,
derivativeAddress,
_msgSender(),
block.timestamp
block.timestamp,
threadId,
replyToId
);
posts.push(newPost);
// Emit the psot event
emit PostSaved(id, post, derivativeAddress, _msgSender(), block.timestamp);
// Increment the current post id
currentPostId.increment();
emit PostSaved(
id,
post,
derivativeAddress,
_msgSender(),
block.timestamp,
threadId,
replyToId
);
}

function _msgSender()
Expand Down
2 changes: 2 additions & 0 deletions contracts/models/Post.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ struct Post {
address derivativeAddress;
address sender;
uint256 timestamp;
uint256 threadId; // must be another post's ID
uint256 replyToId; // external client post ID to reply to
}

0 comments on commit fc7c58d

Please sign in to comment.