forked from Adityaranjanpatra/Btecky2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterSmartContract.sol
204 lines (86 loc) · 2.97 KB
/
TwitterSmartContract.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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;//0xabc - 2,10,15,19,20,22
mapping(address=>Message[]) public conversations;
mapping(address=>mapping(address=>bool)) public operators;
mapping(address=>address[]) public following;
uint nextId;//0
uint nextMessageId;
function _tweet(address _from,string memory _content) internal{//tweet access check - owner,authority
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=nextId+1;
}
function _sendMessage(address _from,address _to,string memory _content) internal{ //tweet access check - owner,authority
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 { //owner
_tweet(msg.sender,_content);
}
function tweet(address _from,string memory _content) public{//owner->address access
_tweet(_from,_content);
}
function sendMessage(string memory _content,address _to) public {//owner
_sendMessage(msg.sender,_to,_content);
}
function sendMessage(address _from,address _to,string memory _content) public { //owner - address access
_sendMessage(_from,_to,_content);
}
function follow(address _followed) public{//abc - def,ghi,opr
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); //array length - count
uint j;
for(uint i=nextId-count;i<nextId;i++){//count = 5 nextId=7; 7-5=2,3,4,5,6
Tweet storage _structure = tweets[i];
_tweets[j]=Tweet(_structure.id,
_structure.author,
_structure.content,
_structure.createdAt);
j=j+1;
}
return _tweets;
}
function getLatestofUser(address _user,uint count) public view returns(Tweet[] memory) { //7
Tweet[] memory _tweets= new Tweet[](count);//new memory array whoose length is count
//tweetsOf[_user] is having all the id's of the user
uint[] memory ids = tweetsOf[_user]; ///ids is an array
require(count>0 && count<=ids.length,"Count is not defined");
uint j;
for (uint i= ids.length-count;i<ids.length;i++){//5-3 = 2 to 5
Tweet storage _structure = tweets[ids[i]];//i=2 id[2]=15 tweets[15]
_tweets[j]=Tweet(_structure.id,
_structure.author,
_structure.content,
_structure.createdAt);
j=j+1;
}
return _tweets;
}
}