Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Social.sol #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 52 additions & 36 deletions contracts/Social.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ contract SocialMedia {
event CommentAdded(address indexed commenter, uint256 indexed postId, string content, uint256 timestamp);

modifier onlyRegisteredUser() {
require(users[msg.sender].isRegistered, "User is not registered");
_;
}
require(users[msg.sender].isRegistered, "User is not registered");
_;
}


modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
Expand All @@ -51,17 +52,29 @@ contract SocialMedia {
}

function registerUser(string memory _username) external {
require(!users[msg.sender].isRegistered, "User is already registered");
require(bytes(_username).length > 0, "Username should not be empty");
require(!users[msg.sender].isRegistered, "User is already registered");
require(bytes(_username).length > 0, "Username should not be empty");
require(bytes(_username).length <= 32, "Username should not exceed 32 characters");

// Check if the username is already taken
bool isUsernameTaken = false;
for (uint256 i = 0; i < posts.length; i++) {
if (keccak256(bytes(users[posts[i].author].username)) == keccak256(bytes(_username))) {
isUsernameTaken = true;
break;
}
}
require(!isUsernameTaken, "Username is already taken");

users[msg.sender] = User({
username: _username,
userAddress: msg.sender,
isRegistered: true
});
users[msg.sender] = User({
username: _username,
userAddress: msg.sender,
isRegistered: true
});

emit UserRegistered(msg.sender, _username);
}

emit UserRegistered(msg.sender, _username);
}


function getUserByAddress(address _userAddress) external view returns (User memory) {
Expand All @@ -70,18 +83,19 @@ contract SocialMedia {
}

function createPost(string memory _content) external onlyRegisteredUser {
require(bytes(_content).length > 0, "Content should not be empty");

posts.push(Post({
author: msg.sender,
content: _content,
timestamp: block.timestamp,
likes: 0,
commentsCount: 0
}));

emit PostCreated(msg.sender, _content, block.timestamp);
}
require(bytes(_content).length > 0, "Content should not be empty");
require(bytes(_content).length <= 280, "Content should not exceed 280 characters");

posts.push(Post({
author: msg.sender,
content: _content,
timestamp: block.timestamp,
likes: 0,
commentsCount: 0
}));

emit PostCreated(msg.sender, _content, block.timestamp);
}

function likePost(uint256 _postId) external onlyRegisteredUser {
require(_postId < posts.length, "Post does not exist");
Expand All @@ -93,21 +107,23 @@ contract SocialMedia {
}

function addComment(uint256 _postId, string memory _content) external onlyRegisteredUser {
require(_postId < posts.length, "Post does not exist");
require(bytes(_content).length > 0, "Comment should not be empty");
require(_postId < posts.length, "Post does not exist");
require(bytes(_content).length > 0, "Comment should not be empty");
require(bytes(_content).length <= 200, "Comment should not exceed 200 characters");

uint256 commentId = postCommentsCount[_postId];
postComments[_postId][commentId] = Comment({
commenter: msg.sender,
content: _content,
timestamp: block.timestamp
});
uint256 commentId = postCommentsCount[_postId];
postComments[_postId][commentId] = Comment({
commenter: msg.sender,
content: _content,
timestamp: block.timestamp
});

postCommentsCount[_postId]++;
posts[_postId].commentsCount++;
postCommentsCount[_postId]++;
posts[_postId].commentsCount++;

emit CommentAdded(msg.sender, _postId, _content, block.timestamp);
}

emit CommentAdded(msg.sender, _postId, _content, block.timestamp);
}

function getPostsCount() external view returns (uint256) {
return posts.length;
Expand Down