-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.sol
110 lines (82 loc) · 3.65 KB
/
twitter.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/access/Ownable.sol";
pragma solidity 0.8.26;
contract twitter{
uint16 public Max_Length = 280;
// This is a struct to store the content of tweet
struct Tweet{
uint256 id;
address author;
string content;
uint256 timeStamp;
uint256 likes;
}
// mapping is used to map an account with the tweets that it done
mapping (address => Tweet[]) public tweets;
address public owner;
// events are like notifications
event tweetCreated(uint256 id, address author, string content , uint256 timeStamp);
event tweetLiked(address liker,address tweetAuthor,uint256 tweetId,uint256 newLikeCount);
event tweetUnlike(address unliker , address tweetAuthor,uint256 tweetId,uint256 newUnlikeCount);
// constructor is created only when the contract is deployed
// so msg.sender will be the deployer
constructor() {
owner = msg.sender;
}
// modifier is used to create extra functionality to functions , like giving access to specific persons
// "_;" is used to tell compiler that whichever func we use the content of that function will start after this modifier
modifier onlyOwner(){
require(msg.sender == owner, "You are not the owner!");
_;
}
// we use onlyOwner in this function to restrict the use
function changeTweetlength(uint16 newTweetLength) public onlyOwner{
Max_Length = newTweetLength;
}
function createTweet(string memory _tweet) public{
// Using require we restrict the length of tweet content
// require is like a if conditional
require(bytes(_tweet).length <= Max_Length , "Message is too long");
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 uses the event to send the notification in the blockchain
emit tweetCreated(newTweet.id , newTweet.author , newTweet.content , newTweet.timeStamp);
}
// tweets is array of structs
// so we index to find that specific tweet using author(address of tweet)
// we then index to which tweet using i
// now we select the id parameter and update it
function likeTweet(address author,uint256 i) external{
// we are trying to see if the tweet which we are trying to access is existing
require(tweets[author][i].id==i , "No Tweet Available");
tweets[author][i].likes++;
emit tweetLiked(msg.sender,author,i,tweets[author][i].likes);
}
function dislikeTweet(address author,uint256 i) external{
// we are trying to see if the tweet which we are trying to access is existing
require(tweets[author][i].id==i , "No Tweet Available");
require(tweets[author][i].likes > 0 , "There are No Likes");
tweets[author][i].likes--;
emit tweetUnlike(msg.sender,author,i,tweets[author][i].likes);
}
// for loops
function getTotalLikes(address _author) external view returns(uint256){
uint tLikes;
for(uint i=0 ;i<tweets[_author].length ; i++){
tLikes += tweets[_author][i].likes;
}
return tLikes;
}
function getTweet(address _owner, uint _i) public view returns (Tweet memory){
return tweets[_owner][_i];
}
function getAllTweet(address _owner) public view returns (Tweet[] memory){
return tweets[_owner];
}
}