Skip to content

Commit

Permalink
fix: all post getters should fail if post doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
vicnaum committed Dec 30, 2024
1 parent fabd708 commit 50f007b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
11 changes: 9 additions & 2 deletions contracts/core/primitives/feed/Feed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand All @@ -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;
}

Expand Down
15 changes: 5 additions & 10 deletions contracts/core/primitives/feed/FeedCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
// }
}

0 comments on commit 50f007b

Please sign in to comment.