-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.sol
130 lines (100 loc) · 3.15 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SPDX-License-Identifier: MIT
import "@openzeppelin/contracts/access/Ownable.sol";
pragma solidity ^0.8.0;
interface IProfile {
struct UserProfile {
string displayName;
string bio;
}
function getProfile(address _user) external view returns(UserProfile memory);
}
contract Twitter is Ownable {
uint public MAX_TWEET_LENGTH = 280;
struct Tweet {
uint256 id;
address author;
string content;
uint256 timestamp;
uint256 likes;
}
mapping(address => Tweet[]) public tweets;
IProfile public profileContract;
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
);
modifier onlyRegistered(){
IProfile.UserProfile memory userProfileTemp = profileContract.getProfile(msg.sender);
_;
}
constructor(address _profileContract) onlyOwner() {
profileContract = IProfile(_profileContract);
}
function changeTweetLength(uint16 newTweetLength) public onlyOwner {
MAX_TWEET_LENGTH = newTweetLength;
}
function getTotalLikes(address _author) external view returns(uint){
uint totalLikes;
for (uint i = 0; i < tweets[_author].length; i++){
totalLikes += tweets[_author][i].likes;
}
return totalLikes;
}
function createTweet(string memory _tweet) public {
require(
bytes(_tweet).length <= MAX_TWEET_LENGTH,
"Tweet juda uzun brooo!"
);
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 TweetCreated(
newTweet.id,
newTweet.author,
newTweet.content,
newTweet.timestamp
);
}
function likeTweet(address _author, uint256 _id) external onlyRegistered {
require(tweets[_author][_id].id == _id, "Id ga tegishli Tweet yoq");
tweets[_author][_id].likes++;
emit TweetLiked(msg.sender, _author, _id, tweets[_author][_id].likes);
}
function unLikeTweet(address _author, uint _id) external onlyRegistered {
require(tweets[_author][_id].id == _id, "Id ga tegishli Tweet yoq");
require(
tweets[_author][_id].likes > 0,
"Like yoqku brat yana dislike qimoqchi bolasiza brooooooooooooo!"
);
tweets[_author][_id].likes--;
emit TweetLiked(msg.sender, _author, _id, tweets[_author][_id].likes);
}
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];
}
}