-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.sol
94 lines (79 loc) · 3.13 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
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;
contract TweetContract {
struct Tweet {
uint id;
address author;
string content;
uint createdAt;
}
struct Message {
uint id;
string content;
address from;
address to;
uint createdAt;
}
mapping(uint => Tweet) public tweets;
mapping(address => uint[]) public tweetsOf;
mapping(address => Message[]) public conversations;
mapping(address => mapping(address => bool)) public operators;
mapping(address => address[]) public following;
uint nextId;
uint nextMessageId;
function _tweet(address _from, string memory _content) internal {
require(_from == msg.sender || operators[_from][msg.sender], "You don't have access");
tweets[nextId] = Tweet(nextId, _from, _content, block.timestamp);
tweetsOf[_from].push(nextId);
nextId++;
}
function _sendMessage(address _from, address _to, string memory _content) internal {
require(_from == msg.sender || operators[_from][msg.sender], "You don't have access");
conversations[_from].push(Message(nextMessageId, _content, _from, _to, block.timestamp));
nextMessageId++;
}
function tweet(string memory _content) public {
_tweet(msg.sender, _content);
}
function tweet(address _from, string memory _content) public {
_tweet(_from, _content);
}
function sendMessage(string memory _content, address _to) public {
_sendMessage(msg.sender, _to, _content);
}
function sendMessage(address _from, address _to, string memory _content) public {
_sendMessage(_from, _to, _content);
}
function follow(address _followed) public {
following[msg.sender].push(_followed);
}
function allow(address _operator) public {
operators[msg.sender][_operator] = true;
}
function disallow(address _operator) public {
operators[msg.sender][_operator] = false;
}
function getLatestTweets(uint count) public view returns (Tweet[] memory) {
require(count > 0 && count <= nextId, "Count is not proper");
Tweet[] memory _tweets = new Tweet[](count);
uint j;
for (uint i = nextId - count; i < nextId; i++) {
Tweet storage _structure = tweets[i];
_tweets[j] = Tweet(_structure.id, _structure.author, _structure.content, _structure.createdAt);
j++;
}
return _tweets;
}
function getLatestofUser(address _user, uint count) public view returns (Tweet[] memory) {
Tweet[] memory _tweets = new Tweet[](count);
uint[] memory ids = tweetsOf[_user];
require(count > 0 && count <= ids.length, "Count is not defined");
uint j;
for (uint i = ids.length - count; i < ids.length; i++) {
Tweet storage _structure = tweets[ids[i]];
_tweets[j] = Tweet(_structure.id, _structure.author, _structure.content, _structure.createdAt);
j++;
}
return _tweets;
}
}