Skip to content

Commit

Permalink
refactor feeds and profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed May 16, 2023
1 parent f0fc85f commit 74adb6c
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 154 deletions.
164 changes: 78 additions & 86 deletions contracts/Feeds.sol
Original file line number Diff line number Diff line change
@@ -1,109 +1,101 @@
// ,-,
// * . /.( .
// \|/ \ {
// . _ . , . -*- . `-`
// ,'-. * / \_ * / \_ /|\ * /\'__ *. *
// (____". / \ / \, __ . _/ / \ * . .
// . /\/\ /\/ :' __ \_ / \ _^/ ^/ `—./\ /\ .
// * _ / \/ \ _/ \-‘\/ ` \ /\ /.' ^_ \_ .’\\ /_/\ ,'-.
// /_\ /\ .- `. \/ \ /. / \ ;. _/ \ -. `_/ \/. \ _ (____". *
// . / \ / `-.__ ^ / .-'.--\ - \/ _ `--./ .-' `-/. \ / \ .
// / /. `. / / `. / ` .-' '-._ `._ /. \
// ~._,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'2_,-'
// ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~ ~~~~~~~~
// ~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~~~ ~~
// ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~
// ๐
// _
// ₒ ><_>
// _______ __ _______
// .-' | _ "\ |" \ /" _ "| ๐
// '--./ / _.---. (. |_) :) || | (: ( \___)
// '-, (__..-` \ |: \/ |: | \/ \
// \ . | (| _ \\ |. | // \ ___
// `,.__. ,__.--/ |: |_) :) |\ | (: _( _|
// '._/_.'___.-` (_______/ |__\| \_______) ๐
//
// __ __ ___ __ __ __ ___ _______
// |" |/ \| "| /" | | "\ /""\ |" | /" "|
// ๐ |' / \: |(: (__) :) / \ || | (: ______)
// |: /' | \/ \/ /' /\ \ |: | ₒ \/ |
// \// /\' | // __ \\ // __' \ \ |___ // ___)_
// / / \\ |(: ( ) :) / / \\ \ ( \_|: \ (: "|
// |___/ \___| \__| |__/ (___/ \___) \_______) \_______)
// ₒ৹
// ___ __ _______ ________
// _ |" | ₒ /""\ | _ "\ /" )
// ><_> || | / \ (. |_) :) (: \___/
// |: | /' /\ \ |: \/ \___ \
// \ |___ // __' \ (| _ \\ __/ \\ \_____)\_____
// ( \_|: \ / / \\ \ |: |_) :) /" \ :) /--v____ __`<
// \_______) (___/ \___)(_______/ (_______/ )/
// '
//
// ๐ . ' , ₒ
// ₒ _______
// ____ .`_|___|_`. ____
// \ \ / / ₒ৹
// \ ' / ๐
// ₒ \/
// ₒ / \ ) (
// ( ₒ৹ ( ( )
// ) ) _ ) ) (
// ( ) ( ( ><_> ( ( ( )
// ) ) ( ( ) ) ) ) ) ) (
// ( ( ) ) ( ( ( ( ( ( )
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "./superclasses/KetlAllowMapChecker.sol";
import "./models/Post.sol";
import "./superclasses/Posts.sol";

contract Feeds is
Initializable,
ContextUpgradeable,
OwnableUpgradeable,
KetlAllowMapChecker
{
contract Feeds is Posts {
using Counters for Counters.Counter;

// Structs
struct PostRequest {
uint256 feedId;
CID postMetadata;
}

// State
CID[] public feeds;
Counters.Counter public lastFeedId;
mapping(uint256 => uint256[]) public feedPosts;
mapping(uint256 => Counters.Counter) public lastFeedPostIds;

// Events
event FeedAdded(uint256 indexed id, CID metadata);
event FeedPostAdded(
uint256 indexed feedId,
uint256 indexed postId,
Post post
);

function initialize() public initializer {
__Ownable_init();
}
event FeedAdded(uint indexed id, CID metadata);

function addFeed(CID memory feedMetadata) public onlyOwner returns (uint256) {
uint256 feedId = lastFeedId.current();
function addFeed(CID memory feedMetadata) public onlyOwner returns (uint) {
uint feedId = lastFeedId.current();
feeds.push(feedMetadata);
emit FeedAdded(feedId, feedMetadata);
lastFeedId.increment();
return feedId;
}

/**
* @dev Add a new feed post
* @param postRequest Post to add
*/
function _addFeedPost(
PostRequest memory postRequest
) private onlyAllowedAddresses {
uint256 commentsFeedId = addFeed(postRequest.postMetadata);
Post memory post = Post(
_msgSender(),
postRequest.postMetadata,
commentsFeedId,
block.timestamp
);
uint256 objectId = lastFeedPostIds[postRequest.feedId].current();
posts[commentsFeedId] = post;
feedPosts[postRequest.feedId].push(commentsFeedId);
emit FeedPostAdded(postRequest.feedId, objectId, post);
lastFeedPostIds[postRequest.feedId].increment();
}

function addFeedPost(PostRequest memory postRequest) external {
_addFeedPost(postRequest);
}

function addBatchFeedPosts(PostRequest[] memory batchPosts) public {
uint256 length = batchPosts.length;
for (uint8 i = 0; i < length; ) {
PostRequest memory post = batchPosts[i];
_addFeedPost(post);

unchecked {
++i;
}
}
function addFeedPost(
address sender,
uint feedId,
CID memory postMetadata
) public {
require(feedId < lastFeedId.current(), "Feed does not exist");
super.addPost(sender, feedId, postMetadata);
}

/**
* @dev Get the feed posts
*/
function getFeedPosts(
uint256 feedId,
uint256 skip,
uint256 limit
) external view returns (Post[] memory) {
uint256 countPosts = lastFeedPostIds[feedId].current();
if (skip > countPosts) {
return new Post[](0);
}
uint256 length = skip + limit > countPosts - 1 ? countPosts - skip : limit;
Post[] memory allPosts = new Post[](length);
for (uint256 i = 0; i < length; i++) {
uint256 postId = feedPosts[feedId][skip + i];
Post memory post = posts[postId];
allPosts[i] = post;
}
return allPosts;
function addFeedComment(
address sender,
uint feedId,
uint postId,
CID memory commentMetadata
) public {
require(feedId < lastFeedId.current(), "Feed does not exist");
super.addComment(sender, feedId, postId, commentMetadata);
}
}
28 changes: 27 additions & 1 deletion contracts/OBSSStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pragma solidity ^0.8.20;
import "@opengsn/contracts/src/ERC2771Recipient.sol";
import "./Karma.sol";
import "./Profiles.sol";
import "./Feeds.sol";

/**
* @title OBSSStorage
Expand All @@ -72,13 +73,15 @@ contract OBSSStorage is OwnableUpgradeable, ERC2771Recipient {
string public version;
Karma public karma;
Profiles public profiles;
Feeds public feeds;

// Constructor
function initialize(
address _forwarder,
string memory _version,
address _karma,
address _profiles
address _profiles,
address _feeds
) public initializer {
// Call parent initializers
__Ownable_init();
Expand All @@ -89,6 +92,7 @@ contract OBSSStorage is OwnableUpgradeable, ERC2771Recipient {
// Set sub-contracts
karma = Karma(_karma);
profiles = Profiles(_profiles);
feeds = Feeds(_feeds);
}

// Profiles
Expand Down Expand Up @@ -117,6 +121,28 @@ contract OBSSStorage is OwnableUpgradeable, ERC2771Recipient {
profiles.addProfileComment(_msgSender(), profile, postId, commentMetadata);
}

// Feeds

function addFeedPost(uint feedId, CID memory postMetadata) external {
feeds.addFeedPost(_msgSender(), feedId, postMetadata);
}

function getFeedPosts(
uint feedId,
uint256 skip,
uint256 limit
) external view returns (Post[] memory) {
return feeds.getFeedPosts(feedId, skip, limit);
}

function addFeedComment(
uint feedId,
uint256 postId,
CID memory commentMetadata
) external {
feeds.addFeedComment(_msgSender(), feedId, postId, commentMetadata);
}

// OpenGSN boilerplate

function _msgSender()
Expand Down
76 changes: 9 additions & 67 deletions contracts/Profiles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,16 @@
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/utils/Counters.sol";
import "./superclasses/KetlGuarded.sol";
import "./models/Post.sol";
import "./superclasses/Posts.sol";

contract Profiles is KetlGuarded {
contract Profiles is Posts {
using Counters for Counters.Counter;

// State
mapping(address => CID) public profiles;
mapping(address => Post[]) public profilePosts;
mapping(address => Counters.Counter) public lastProfilePostIds;
mapping(address => mapping(uint => Post[])) public profileComments;
mapping(address => mapping(uint => Counters.Counter))
public lastProfileCommentIds;

// Events
event ProfileSet(address indexed profile, CID metadata);
event ProfilePostAdded(
address indexed profile,
uint256 indexed postId,
Post post
);
event ProfileCommentAdded(
address indexed profile,
uint256 indexed postId,
uint256 indexed commentId,
Post comment
);

function initialize(
address _allowedCaller,
address _token
) public override initializer {
KetlGuarded.initialize(_token, _allowedCaller);
}

function setProfile(
address sender,
Expand All @@ -103,52 +79,18 @@ contract Profiles is KetlGuarded {
emit ProfileSet(sender, profileMetadata);
}

function addProfilePost(
address sender,
CID memory postMetadata
) external onlyAllowedCaller onlyKetlTokenOwners(sender) {
Post memory post = Post(sender, postMetadata, block.timestamp);
profilePosts[sender].push(post);
emit ProfilePostAdded(sender, lastProfilePostIds[sender].current(), post);
lastProfilePostIds[sender].increment();
}

function getProfilePosts(
address profile,
uint256 skip,
uint256 limit
) external view returns (Post[] memory) {
uint256 countPosts = lastProfilePostIds[profile].current();
if (skip > countPosts) {
return new Post[](0);
}
uint256 length = skip + limit > countPosts - 1 ? countPosts - skip : limit;
Post[] memory allPosts = new Post[](length);
for (uint256 i = 0; i < length; i++) {
Post memory post = profilePosts[profile][skip + i];
allPosts[i] = post;
}
return allPosts;
function addProfilePost(address sender, CID memory postMetadata) public {
uint id = uint(keccak256(abi.encodePacked(sender)));
super.addPost(sender, id, postMetadata);
}

function addProfileComment(
address sender,
address profile,
uint256 postId,
uint postId,
CID memory commentMetadata
) external onlyAllowedCaller onlyKetlTokenOwners(sender) {
require(
postId < lastProfilePostIds[profile].current(),
"Profile post does not exist"
);
Post memory post = Post(sender, commentMetadata, block.timestamp);
profileComments[profile][postId].push(post);
emit ProfileCommentAdded(
profile,
postId,
lastProfileCommentIds[profile][postId].current(),
post
);
lastProfileCommentIds[profile][postId].increment();
) public {
uint id = uint(keccak256(abi.encodePacked(profile)));
super.addComment(sender, id, postId, commentMetadata);
}
}
Loading

0 comments on commit 74adb6c

Please sign in to comment.