From 50f007bb4fbabefc93ad34db9bcec60146c76034 Mon Sep 17 00:00:00 2001 From: vicnaum Date: Mon, 30 Dec 2024 12:57:21 +0100 Subject: [PATCH] fix: all post getters should fail if post doesn't exist --- contracts/core/primitives/feed/Feed.sol | 11 +++++++++-- contracts/core/primitives/feed/FeedCore.sol | 15 +++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/contracts/core/primitives/feed/Feed.sol b/contracts/core/primitives/feed/Feed.sol index 30b9a23e..fb301334 100644 --- a/contracts/core/primitives/feed/Feed.sol +++ b/contracts/core/primitives/feed/Feed.sol @@ -191,7 +191,7 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour // Getters function getPost(uint256 postId) external view override returns (Post memory) { - // TODO: Should fail if post doesn't exist + require(Core._postExists(postId), "POST_DOES_NOT_EXIST"); return Post({ author: Core.$storage().posts[postId].author, authorPostSequentialId: Core.$storage().posts[postId].authorPostSequentialId, @@ -208,8 +208,12 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour }); } + function postExists(uint256 postId) external view override returns (bool) { + return Core._postExists(postId); + } + function getPostAuthor(uint256 postId) external view override returns (address) { - // TODO: Should fail if post doesn't exist? + require(Core._postExists(postId), "POST_DOES_NOT_EXIST"); return Core.$storage().posts[postId].author; } @@ -222,6 +226,7 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour } function getPostExtraData(uint256 postId, bytes32 key) external view override returns (bytes memory) { + require(Core._postExists(postId), "POST_DOES_NOT_EXIST"); address postAuthor = Core.$storage().posts[postId].author; return _getEntityExtraData(postAuthor, postId, key); } @@ -231,10 +236,12 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour } function getPostSequentialId(uint256 postId) external view override returns (uint256) { + require(Core._postExists(postId), "POST_DOES_NOT_EXIST"); return Core.$storage().posts[postId].postSequentialId; } function getAuthorPostSequentialId(uint256 postId) external view override returns (uint256) { + require(Core._postExists(postId), "POST_DOES_NOT_EXIST"); return Core.$storage().posts[postId].authorPostSequentialId; } diff --git a/contracts/core/primitives/feed/FeedCore.sol b/contracts/core/primitives/feed/FeedCore.sol index f48ab219..0fd94b7b 100644 --- a/contracts/core/primitives/feed/FeedCore.sol +++ b/contracts/core/primitives/feed/FeedCore.sol @@ -52,16 +52,16 @@ library FeedCore { _newPost.contentURI = postParams.contentURI; uint256 rootPostId = postId; if (postParams.quotedPostId != 0) { - _requirePostExistence(postParams.quotedPostId); + _require(_postExists(postParams.quotedPostId), "QUOTED_POST_DOES_NOT_EXIST"); _newPost.quotedPostId = postParams.quotedPostId; } if (postParams.repliedPostId != 0) { - _requirePostExistence(postParams.repliedPostId); + _require(_postExists(postParams.repliedPostId), "REPLIED_POST_DOES_NOT_EXIST"); _newPost.repliedPostId = postParams.repliedPostId; rootPostId = $storage().posts[postParams.repliedPostId].rootPostId; } if (postParams.repostedPostId != 0) { - _requirePostExistence(postParams.repostedPostId); + _require(_postExists(postParams.repostedPostId), "REPOSTED_POST_DOES_NOT_EXIST"); _newPost.repostedPostId = postParams.repostedPostId; rootPostId = $storage().posts[postParams.repostedPostId].rootPostId; require( @@ -90,12 +90,7 @@ library FeedCore { delete $storage().posts[postId]; } - function _requirePostExistence(uint256 postId) internal view { - require($storage().posts[postId].creationTimestamp != 0, "POST_DOES_NOT_EXIST"); + function _postExists(uint256 postId) internal view returns (bool) { + return $storage().posts[postId].creationTimestamp != 0; } - - // TODO: Debate this more. It should be a soft delete, you can reconstruct anyways from tx history. - // function _disablePost(uint256 postId) internal { - // $storage().posts[postId].disabled = true; - // } }