Skip to content

Commit

Permalink
misc: _beforeChangeEntityRules implemented for Feed and Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
donosonaumczuk committed Dec 29, 2024
1 parent eb59185 commit 916dc6b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
46 changes: 34 additions & 12 deletions contracts/core/primitives/feed/Feed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour
emit Events.Lens_Contract_Deployed("feed", "lens.feed", "feed", "lens.feed");
}

function _emitMetadataURISet(string memory metadataURI) internal override {
function _emitMetadataURISet(
string memory metadataURI
) internal override {
emit Lens_Feed_MetadataURISet(metadataURI);
}

Expand All @@ -41,19 +43,23 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour

// Access Controlled functions

function _beforeMetadataURIUpdate(string memory /* metadataURI */ ) internal view override {
function _beforeMetadataURIUpdate(
string memory /* metadataURI */
) internal view override {
_requireAccess(msg.sender, SET_METADATA_PID);
}

function _beforeChangePrimitiveRules(RuleChange[] calldata /* ruleChanges */ ) internal virtual override {
function _beforeChangePrimitiveRules(
RuleChange[] calldata /* ruleChanges */
) internal virtual override {
_requireAccess(msg.sender, SET_RULES_PID);
}

function _beforeChangeEntityRules(
uint256 entityId,
RuleChange[] calldata /* ruleChanges */
) internal virtual override {
// TODO: What should we validate here?
require(msg.sender == Core.$storage().posts[entityId].author);
}

// Public user functions
Expand Down Expand Up @@ -169,7 +175,9 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour
emit Lens_Feed_PostDeleted(postId, author, customParams, source);
}

function setExtraData(KeyValue[] calldata extraDataToSet) external override {
function setExtraData(
KeyValue[] calldata extraDataToSet
) external override {
_requireAccess(msg.sender, SET_EXTRA_DATA_PID);
for (uint256 i = 0; i < extraDataToSet.length; i++) {
bool hadAValueSetBefore = _setPrimitiveExtraData(extraDataToSet[i]);
Expand All @@ -190,7 +198,9 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour

// Getters

function getPost(uint256 postId) external view override returns (Post memory) {
function getPost(
uint256 postId
) external view override returns (Post memory) {
// TODO: Should fail if post doesn't exist
return Post({
author: Core.$storage().posts[postId].author,
Expand All @@ -208,7 +218,9 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour
});
}

function getPostAuthor(uint256 postId) external view override returns (address) {
function getPostAuthor(
uint256 postId
) external view override returns (address) {
// TODO: Should fail if post doesn't exist?
return Core.$storage().posts[postId].author;
}
Expand All @@ -217,7 +229,9 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour
return Core.$storage().postCount;
}

function getPostCount(address author) external view override returns (uint256) {
function getPostCount(
address author
) external view override returns (uint256) {
return Core.$storage().authorPostCount[author];
}

Expand All @@ -226,19 +240,27 @@ contract Feed is IFeed, RuleBasedFeed, AccessControlled, ExtraStorageBased, Sour
return _getEntityExtraData(postAuthor, postId, key);
}

function getExtraData(bytes32 key) external view override returns (bytes memory) {
function getExtraData(
bytes32 key
) external view override returns (bytes memory) {
return _getPrimitiveExtraData(key);
}

function getPostSequentialId(uint256 postId) external view override returns (uint256) {
function getPostSequentialId(
uint256 postId
) external view override returns (uint256) {
return Core.$storage().posts[postId].postSequentialId;
}

function getAuthorPostSequentialId(uint256 postId) external view override returns (uint256) {
function getAuthorPostSequentialId(
uint256 postId
) external view override returns (uint256) {
return Core.$storage().posts[postId].authorPostSequentialId;
}

function getNextPostId(address author) external view returns (uint256) {
function getNextPostId(
address author
) external view returns (uint256) {
return Core._generatePostId(author, Core.$storage().authorPostCount[author] + 1);
}
}
15 changes: 9 additions & 6 deletions contracts/core/primitives/feed/RuleBasedFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity ^0.8.0;
import {IPostRule} from "./../../interfaces/IPostRule.sol";
import {IFeedRule} from "./../../interfaces/IFeedRule.sol";
import {IFeed} from "./../../interfaces/IFeed.sol";
import {FeedCore as Core} from "./FeedCore.sol";
import {RulesStorage, RulesLib} from "./../../libraries/RulesLib.sol";
import {RuleProcessingParams, Rule, RuleChange, KeyValue} from "./../../types/Types.sol";
import {EditPostParams, CreatePostParams} from "./../../interfaces/IFeed.sol";
Expand All @@ -32,13 +31,17 @@ abstract contract RuleBasedFeed is IFeed, RuleBasedPrimitive {
return $ruleBasedStorage().feedRulesStorage;
}

function $postRulesStorage(uint256 postId) private view returns (RulesStorage storage _storage) {
function $postRulesStorage(
uint256 postId
) private view returns (RulesStorage storage _storage) {
return $ruleBasedStorage().postRulesStorage[postId];
}

//////////////////////////// CONFIGURATION FUNCTIONS ////////////////////////////

function changeFeedRules(RuleChange[] calldata ruleChanges) external virtual override {
function changeFeedRules(
RuleChange[] calldata ruleChanges
) external virtual override {
_changePrimitiveRules($feedRulesStorage(), ruleChanges);
}

Expand All @@ -47,8 +50,6 @@ abstract contract RuleBasedFeed is IFeed, RuleBasedPrimitive {
RuleChange[] calldata ruleChanges,
RuleProcessingParams[] calldata ruleChangesProcessingParams
) external virtual override {
// TODO: msg.sender must be author
// TODO: Post must exist before we allow changing its rules
_changeEntityRules($postRulesStorage(postId), postId, ruleChanges, ruleChangesProcessingParams);
}

Expand Down Expand Up @@ -102,7 +103,9 @@ abstract contract RuleBasedFeed is IFeed, RuleBasedPrimitive {
}
}

function _amountOfRules(bytes4 ruleSelector) internal view returns (uint256) {
function _amountOfRules(
bytes4 ruleSelector
) internal view returns (uint256) {
return $feedRulesStorage()._getRulesArray(ruleSelector, false).length
+ $feedRulesStorage()._getRulesArray(ruleSelector, true).length;
}
Expand Down
31 changes: 22 additions & 9 deletions contracts/core/primitives/graph/Graph.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ contract Graph is IGraph, RuleBasedGraph, AccessControlled, ExtraStorageBased, S
emit Events.Lens_Contract_Deployed("graph", "lens.graph", "graph", "lens.graph");
}

function _emitMetadataURISet(string memory metadataURI) internal override {
function _emitMetadataURISet(
string memory metadataURI
) internal override {
emit Lens_Graph_MetadataURISet(metadataURI);
}

Expand All @@ -40,23 +42,28 @@ contract Graph is IGraph, RuleBasedGraph, AccessControlled, ExtraStorageBased, S

// Access Controlled functions

function _beforeMetadataURIUpdate(string memory /* metadataURI */ ) internal view override {
function _beforeMetadataURIUpdate(
string memory /* metadataURI */
) internal view override {
_requireAccess(msg.sender, SET_METADATA_PID);
}

function _beforeChangePrimitiveRules(RuleChange[] calldata /* ruleChanges */ ) internal virtual override {
function _beforeChangePrimitiveRules(
RuleChange[] calldata /* ruleChanges */
) internal virtual override {
_requireAccess(msg.sender, SET_RULES_PID);
}

function _beforeChangeEntityRules(
uint256 entityId,
RuleChange[] calldata /* ruleChanges */
) internal virtual override {
address account = address(uint160(entityId));
// TODO: What should we validate here?
require(msg.sender == address(uint160(entityId))); // Follow rules can only be changed in your own account
}

function setExtraData(KeyValue[] calldata extraDataToSet) external override {
function setExtraData(
KeyValue[] calldata extraDataToSet
) external override {
_requireAccess(msg.sender, SET_EXTRA_DATA_PID);
for (uint256 i = 0; i < extraDataToSet.length; i++) {
bool hadAValueSetBefore = _setPrimitiveExtraData(extraDataToSet[i]);
Expand Down Expand Up @@ -134,15 +141,21 @@ contract Graph is IGraph, RuleBasedGraph, AccessControlled, ExtraStorageBased, S
return Core.$storage().follows[followerAccount][targetAccount];
}

function getFollowersCount(address account) external view override returns (uint256) {
function getFollowersCount(
address account
) external view override returns (uint256) {
return Core.$storage().followersCount[account];
}

function getFollowingCount(address account) external view override returns (uint256) {
function getFollowingCount(
address account
) external view override returns (uint256) {
return Core.$storage().followingCount[account];
}

function getExtraData(bytes32 key) external view override returns (bytes memory) {
function getExtraData(
bytes32 key
) external view override returns (bytes memory) {
return _getPrimitiveExtraData(key);
}
}

0 comments on commit 916dc6b

Please sign in to comment.