generated from Borodutch/smart-contract-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0fc85f
commit 74adb6c
Showing
4 changed files
with
243 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.