From 9e407df3c1b2839c5858ad23705cab7540813603 Mon Sep 17 00:00:00 2001 From: Alan Donoso Naumczuk Date: Wed, 5 Feb 2025 15:30:53 -0300 Subject: [PATCH] misc: Post removal and existance reworked + skip root rule processing if deleted Co-authored-by: Victor Naumik --- contracts/core/interfaces/IFeed.sol | 3 ++ contracts/core/primitives/feed/Feed.sol | 33 +++++++++++++++------ contracts/core/primitives/feed/FeedCore.sol | 8 +++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/contracts/core/interfaces/IFeed.sol b/contracts/core/interfaces/IFeed.sol index f5e4f36a..e27949b5 100644 --- a/contracts/core/interfaces/IFeed.sol +++ b/contracts/core/interfaces/IFeed.sol @@ -35,6 +35,7 @@ struct Post { address creationSource; uint80 lastUpdatedTimestamp; address lastUpdateSource; + bool isDeleted; } interface IFeed is IMetadataBased { @@ -157,6 +158,8 @@ interface IFeed is IMetadataBased { function getPost(uint256 postId) external view returns (Post memory); + function getPostUnchecked(uint256 postId) external view returns (Post memory); + function postExists(uint256 postId) external view returns (bool); function getPostAuthor(uint256 postId) external view returns (address); diff --git a/contracts/core/primitives/feed/Feed.sol b/contracts/core/primitives/feed/Feed.sol index 0b953976..96cae1e3 100644 --- a/contracts/core/primitives/feed/Feed.sol +++ b/contracts/core/primitives/feed/Feed.sol @@ -82,6 +82,7 @@ contract Feed is virtual override { + require(Core._postExists(entityId), Errors.DoesNotExist()); require(msg.sender == Core.$storage().posts[entityId].author, Errors.InvalidMsgSender()); require(entityId == Core.$storage().posts[entityId].rootPostId, Errors.CannotHaveRules()); } @@ -103,16 +104,20 @@ contract Feed is _processPostCreationOnFeed(postId, postParams, customParams, feedRulesParams); // Process rules of the Quote (if quoting) if (postParams.quotedPostId != 0) { + // Existence of the quotedPost post was checked in the Core // Just a thought: Maybe quotes shouldn't be limited by rules... Like quotations in real life. uint256 rootOfQuotedPost = Core.$storage().posts[postParams.quotedPostId].rootPostId; - if (rootOfQuotedPost != rootPostId) { + if (rootOfQuotedPost != rootPostId && Core._postExists(rootOfQuotedPost)) { _processPostCreationOnRootPost(rootOfQuotedPost, postId, postParams, customParams, quotedPostRulesParams); } } if (postId != rootPostId) { + // Existence of the Replied/Reposted Post is checked in the Core require(postParams.ruleChanges.length == 0, Errors.CannotHaveRules()); - // This covers the Reply or Repost cases - _processPostCreationOnRootPost(rootPostId, postId, postParams, customParams, rootPostRulesParams); + if (Core._postExists(rootPostId)) { + // This covers the Reply or Repost cases + _processPostCreationOnRootPost(rootPostId, postId, postParams, customParams, rootPostRulesParams); + } } else { _addPostRulesAtCreation(postId, postParams, feedRulesParams); } @@ -150,22 +155,23 @@ contract Feed is // You can have this if you want to allow moderator editing: // require(msg.sender == author || _hasAccess(msg.sender, EDIT_POST_PID)); require(msg.sender == author, Errors.InvalidMsgSender()); - Core._editPost(postId, postParams); - bool[] memory wereExtraDataValuesSet = new bool[](postParams.extraData.length); for (uint256 i = 0; i < postParams.extraData.length; i++) { wereExtraDataValuesSet[i] = _setEntityExtraStorage_Account(postId, postParams.extraData[i]); } - _processPostEditingOnFeed(postId, postParams, customParams, feedRulesParams); uint256 quotedPostId = Core.$storage().posts[postId].quotedPostId; if (quotedPostId != 0) { uint256 rootOfQuotedPost = Core.$storage().posts[quotedPostId].rootPostId; - _processPostEditingOnRootPost(rootOfQuotedPost, postId, postParams, customParams, quotedPostRulesParams); + // Skip the Root rules processing if the Root post was deleted + if (Core._postExists(rootOfQuotedPost)) { + _processPostEditingOnRootPost(rootOfQuotedPost, postId, postParams, customParams, quotedPostRulesParams); + } } uint256 rootPostId = Core.$storage().posts[postId].rootPostId; - if (postId != rootPostId) { + // Skip the Root rules processing if the Root post was deleted + if (postId != rootPostId && Core._postExists(rootPostId)) { _processPostEditingOnRootPost(rootPostId, postId, postParams, customParams, rootPostRulesParams); } address source = _processSourceStamp({ @@ -227,6 +233,14 @@ contract Feed is function getPost(uint256 postId) external view override returns (Post memory) { require(Core._postExists(postId), Errors.DoesNotExist()); + return _getPostUnchecked(postId); + } + + function getPostUnchecked(uint256 postId) external view override returns (Post memory) { + return _getPostUnchecked(postId); + } + + function _getPostUnchecked(uint256 postId) internal view returns (Post memory) { return Post({ author: Core.$storage().posts[postId].author, authorPostSequentialId: Core.$storage().posts[postId].authorPostSequentialId, @@ -239,7 +253,8 @@ contract Feed is creationTimestamp: Core.$storage().posts[postId].creationTimestamp, creationSource: _getSource(postId), lastUpdatedTimestamp: Core.$storage().posts[postId].lastUpdatedTimestamp, - lastUpdateSource: _getSource(DATA__LAST_UPDATED_SOURCE, postId) + lastUpdateSource: _getSource(DATA__LAST_UPDATED_SOURCE, postId), + isDeleted: Core.$storage().posts[postId].isDeleted }); } diff --git a/contracts/core/primitives/feed/FeedCore.sol b/contracts/core/primitives/feed/FeedCore.sol index ac627b01..590c18c9 100644 --- a/contracts/core/primitives/feed/FeedCore.sol +++ b/contracts/core/primitives/feed/FeedCore.sol @@ -16,6 +16,7 @@ struct PostStorage { uint256 repliedPostId; uint80 creationTimestamp; uint80 lastUpdatedTimestamp; + bool isDeleted; } library FeedCore { @@ -75,8 +76,8 @@ library FeedCore { } function _editPost(uint256 postId, EditPostParams calldata postParams) internal { + require(_postExists(postId), Errors.DoesNotExist()); PostStorage storage _post = $storage().posts[postId]; - require(_post.creationTimestamp != 0, Errors.DoesNotExist()); // Post must exist if (_post.repostedPostId != 0) { require(bytes(postParams.contentURI).length == 0, Errors.InvalidParameter()); } else { @@ -86,10 +87,11 @@ library FeedCore { } function _removePost(uint256 postId) internal { - delete $storage().posts[postId]; + require(_postExists(postId), Errors.DoesNotExist()); + $storage().posts[postId].isDeleted = true; } function _postExists(uint256 postId) internal view returns (bool) { - return $storage().posts[postId].creationTimestamp != 0; + return $storage().posts[postId].creationTimestamp != 0 && $storage().posts[postId].isDeleted == false; } }