-
Notifications
You must be signed in to change notification settings - Fork 107
/
24-Twitter-GetTotalLikes-Exercise.sol
88 lines (64 loc) · 2.5 KB
/
24-Twitter-GetTotalLikes-Exercise.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-License-Identifier: MIT
// 1️⃣ Create a function, getTotalLikes, to get total Tweet Likes for the user
// USE parameters of author
// 2️⃣ Loop over all the tweets
// 3️⃣ Sum up totalLikes
// 4️⃣ Return totalLikes
pragma solidity ^0.8.0;
contract Twitter {
uint16 public MAX_TWEET_LENGTH = 280;
struct Tweet {
uint256 id;
address author;
string content;
uint256 timestamp;
uint256 likes;
}
mapping(address => Tweet[] ) public tweets;
address public owner;
// Define the events
event TweetCreated(uint256 id, address author, string content, uint256 timestamp);
event TweetLiked(address liker, address tweetAuthor, uint256 tweetId, uint256 newLikeCount);
event TweetUnliked(address unliker, address tweetAuthor, uint256 tweetId, uint256 newLikeCount);
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "YOU ARE NOT THE OWNER!");
_;
}
function changeTweetLength(uint16 newTweetLength) public onlyOwner {
MAX_TWEET_LENGTH = newTweetLength;
}
function createTweet(string memory _tweet) public {
require(bytes(_tweet).length <= MAX_TWEET_LENGTH, "Tweet is too long bro!" );
Tweet memory newTweet = Tweet({
id: tweets[msg.sender].length,
author: msg.sender,
content: _tweet,
timestamp: block.timestamp,
likes: 0
});
tweets[msg.sender].push(newTweet);
// Emit the TweetCreated event
emit TweetCreated(newTweet.id, newTweet.author, newTweet.content, newTweet.timestamp);
}
function likeTweet(address author, uint256 id) external {
require(tweets[author][id].id == id, "TWEET DOES NOT EXIST");
tweets[author][id].likes++;
// Emit the TweetLiked event
emit TweetLiked(msg.sender, author, id, tweets[author][id].likes);
}
function unlikeTweet(address author, uint256 id) external {
require(tweets[author][id].id == id, "TWEET DOES NOT EXIST");
require(tweets[author][id].likes > 0, "TWEET HAS NO LIKES");
tweets[author][id].likes--;
emit TweetUnliked(msg.sender, author, id, tweets[author][id].likes );
}
function getTweet( uint _i) public view returns (Tweet memory) {
return tweets[msg.sender][_i];
}
function getAllTweets(address _owner) public view returns (Tweet[] memory ){
return tweets[_owner];
}
}