Skip to content

Commit

Permalink
misc: Post removal and existance reworked + skip root rule processing…
Browse files Browse the repository at this point in the history
… if deleted

Co-authored-by: Victor Naumik <[email protected]>
  • Loading branch information
donosonaumczuk and vicnaum committed Feb 5, 2025
1 parent e1bc05e commit 9e407df
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
3 changes: 3 additions & 0 deletions contracts/core/interfaces/IFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Post {
address creationSource;
uint80 lastUpdatedTimestamp;
address lastUpdateSource;
bool isDeleted;
}

interface IFeed is IMetadataBased {
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 24 additions & 9 deletions contracts/core/primitives/feed/Feed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand All @@ -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
});
}

Expand Down
8 changes: 5 additions & 3 deletions contracts/core/primitives/feed/FeedCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct PostStorage {
uint256 repliedPostId;
uint80 creationTimestamp;
uint80 lastUpdatedTimestamp;
bool isDeleted;
}

library FeedCore {
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
}

0 comments on commit 9e407df

Please sign in to comment.