-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContract.sol
More file actions
359 lines (310 loc) · 11 KB
/
Contract.sol
File metadata and controls
359 lines (310 loc) · 11 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract MyContract {
constructor() {}
struct User {
string image;
string name;
string bio;
uint256 id;
}
struct Notification {
string message;
uint256 timestamp;
}
struct Report {
address reporter;
string reason;
uint256 timestamp;
}
struct Posts {
string image;
string text;
uint256 timestamp;
uint256 likes;
uint256 views;
uint256 commentsCount;
address[] taggedUsers; // Added tagged users array
}
struct Messages {
string text;
string image;
uint256 timestamp;
}
// State Variables
mapping(address => Posts[]) public userPosts; // Maps a wallet address to multiple posts.
mapping(address => User) public users; // Maps a wallet address to the User struct.
mapping(address => mapping(address => Messages[])) public messages; // Messages between users.
mapping(address => address[]) public followers; // Tracks followers of each user.
mapping(address => address[]) public following; // Tracks users each address is following.
mapping(address => bool) public isProfilePrivate;
mapping(uint256 => mapping(address => string)) public postReactions; // Maps post ID to user reactions
mapping(address => Notification[]) public notifications;
mapping(address => uint256) public reputation;
mapping(uint256 => bool) public isPostNFT;
mapping(address => Report[]) public reportedProfiles;
mapping(address => mapping(uint256 => Report[])) public reportedPosts;
mapping(address => uint256) public tokens;
// Function to check if image and name are set for a user
function isUserValid(address user) public view returns (bool) {
User memory u = users[user];
require(
bytes(u.image).length > 0 && bytes(u.name).length > 0,
"Image or name is missing"
);
return true;
}
// Function to fetch all posts of a specific user
function fetchAllPosts(address user) public view returns (Posts[] memory) {
return userPosts[user];
}
// Display all posts of all users
function displayAllPosts() public view returns (Posts[] memory) {
uint256 totalPosts;
for (uint256 i = 0; i < following[msg.sender].length; i++) {
totalPosts += userPosts[following[msg.sender][i]].length;
}
Posts[] memory allPosts = new Posts[](totalPosts);
uint256 index = 0;
for (uint256 i = 0; i < following[msg.sender].length; i++) {
address user = following[msg.sender][i];
Posts[] memory posts = userPosts[user];
for (uint256 j = 0; j < posts.length; j++) {
allPosts[index] = posts[j];
index++;
}
}
return allPosts;
}
// Send a message to another user
function sendMessage(
address to,
string memory text,
string memory image
) public {
messages[msg.sender][to].push(
Messages({text: text, image: image, timestamp: block.timestamp})
);
}
// See messages between sender and a specific user
function seeMessages(
address withUser
) public view returns (Messages[] memory) {
return messages[msg.sender][withUser];
}
// See all user profiles
function seeAllProfiles() public view returns (User[] memory) {
uint256 totalUsers = following[msg.sender].length;
User[] memory allUsers = new User[](totalUsers);
for (uint256 i = 0; i < totalUsers; i++) {
allUsers[i] = users[following[msg.sender][i]];
}
return allUsers;
}
// Follow another user
function followUser(address userToFollow) public {
require(userToFollow != msg.sender, "You cannot follow yourself.");
// Check if already following
for (uint256 i = 0; i < following[msg.sender].length; i++) {
require(
following[msg.sender][i] != userToFollow,
"Already following this user."
);
}
followers[userToFollow].push(msg.sender);
following[msg.sender].push(userToFollow);
notifyUser(userToFollow, "You have a new follower!");
}
// Comment on a post (increase comment count)
function commentPost(address user, uint256 postIndex) public {
require(postIndex < userPosts[user].length, "Invalid post index");
userPosts[user][postIndex].commentsCount++;
}
// Share a post
function sharePost(address user, uint256 postIndex) public {
require(postIndex < userPosts[user].length, "Invalid post index");
userPosts[msg.sender].push(userPosts[user][postIndex]);
}
// Get views for a specific post
function getPostViews(
address user,
uint256 postIndex
) public view returns (uint256) {
require(postIndex < userPosts[user].length, "Invalid post index");
return userPosts[user][postIndex].views;
}
// Get all followers of a user
function getAllFollowers(
address user
) public view returns (address[] memory) {
return followers[user];
}
// Check if the current user follows another user
function userFollow(address user) public view returns (bool) {
for (uint256 i = 0; i < following[msg.sender].length; i++) {
if (following[msg.sender][i] == user) {
return true;
}
}
return false;
}
// Unfriend a user (remove from following list)
function unFriend(address user) public {
for (uint256 i = 0; i < following[msg.sender].length; i++) {
if (following[msg.sender][i] == user) {
following[msg.sender][i] = following[msg.sender][
following[msg.sender].length - 1
];
following[msg.sender].pop();
break;
}
}
for (uint256 i = 0; i < followers[user].length; i++) {
if (followers[user][i] == msg.sender) {
followers[user][i] = followers[user][
followers[user].length - 1
];
followers[user].pop();
break;
}
}
}
// Add a friend (mutual following)
function addFriend(address user) public {
followUser(user);
followers[msg.sender].push(user);
}
// Function to retrieve the caller's wallet address
function seeWalletAddress() public view returns (address) {
return msg.sender; // Returns the address of the caller
}
// Function to display the profile of the caller
function displayProfile() public view returns (User memory) {
User memory user = users[msg.sender];
require(bytes(user.name).length > 0, "Profile does not exist.");
return user;
}
// Function to update the profile of the caller
function updateProfile(
string memory _image,
string memory _name,
string memory _bio
) public {
require(bytes(_name).length > 0, "Name cannot be empty.");
require(bytes(_image).length > 0, "Image cannot be empty.");
users[msg.sender] = User({
image: _image,
name: _name,
bio: _bio,
id: uint256(uint160(msg.sender)) // Example ID based on wallet address
});
}
function setProfilePrivacy(bool _isPrivate) public {
isProfilePrivate[msg.sender] = _isPrivate;
}
function getProfile(address user) public view returns (User memory) {
if (isProfilePrivate[user]) {
require(isFollower(msg.sender, user), "This profile is private.");
}
return users[user];
}
function isFollower(
address follower,
address user
) internal view returns (bool) {
for (uint256 i = 0; i < followers[user].length; i++) {
if (followers[user][i] == follower) {
return true;
}
}
return false;
}
function createPost(
string memory image,
string memory text,
address[] memory taggedUsers
) public {
userPosts[msg.sender].push(
Posts({
image: image,
text: text,
timestamp: block.timestamp,
likes: 0,
views: 0,
commentsCount: 0,
taggedUsers: taggedUsers // Added tagged users
})
);
}
function viewPost(address user, uint256 postIndex) public {
require(postIndex < userPosts[user].length, "Invalid post index");
userPosts[user][postIndex].views++;
}
function reactToPost(
address user,
uint256 postIndex,
string memory emoji
) public {
require(postIndex < userPosts[user].length, "Invalid post index");
postReactions[postIndex][msg.sender] = emoji;
}
function notifyUser(address user, string memory message) internal {
notifications[user].push(
Notification({message: message, timestamp: block.timestamp})
);
}
function increaseReputation(address user, uint256 amount) internal {
reputation[user] += amount;
}
function likePost(address user, uint256 postIndex) public {
require(postIndex < userPosts[user].length, "Invalid post index");
userPosts[user][postIndex].likes++;
increaseReputation(user, 10); // Reward reputation for likes
}
function mintPostAsNFT(uint256 postIndex) public {
require(postIndex < userPosts[msg.sender].length, "Invalid post index");
isPostNFT[postIndex] = true;
}
function reportProfile(address user, string memory reason) public {
reportedProfiles[user].push(
Report({
reporter: msg.sender,
reason: reason,
timestamp: block.timestamp
})
);
}
function reportPost(
address user,
uint256 postIndex,
string memory reason
) public {
reportedPosts[user][postIndex].push(
Report({
reporter: msg.sender,
reason: reason,
timestamp: block.timestamp
})
);
}
function rewardUser(address user, uint256 amount) internal {
tokens[user] += amount;
}
function likePostAndReward(address user, uint256 postIndex) public {
require(postIndex < userPosts[user].length, "Invalid post index");
userPosts[user][postIndex].likes++;
rewardUser(user, 5); // Reward tokens for getting likes
}
function getMostLikedPost(address user) public view returns (Posts memory) {
Posts[] memory posts = userPosts[user];
uint256 maxLikes = 0;
uint256 maxIndex = 0;
for (uint256 i = 0; i < posts.length; i++) {
if (posts[i].likes > maxLikes) {
maxLikes = posts[i].likes;
maxIndex = i;
}
}
return posts[maxIndex];
}
}