You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.5 KiB

6 years ago
pragma solidity ^0.5.4;
7 years ago
contract Forum {
7 years ago
//----------------------------------------USER----------------------------------------
7 years ago
struct User {
6 years ago
string username;
7 years ago
uint[] topicIDs; // IDs of the topics the user created
uint[] postIDs; // IDs of the posts the user created
uint timestamp;
bool signedUp; // Helper variable for hasUserSignedUp()
7 years ago
}
mapping (address => User) users;
mapping (string => address) userAddresses;
event UserSignedUp(string username, address userAddress);
event UsernameUpdated(string newName, string oldName,address userAddress);
7 years ago
6 years ago
function signUp(string memory username) public returns (bool) {
require (!hasUserSignedUp(msg.sender), "User has already signed up.");
require(!isUserNameTaken(username), "Username is already taken.");
users[msg.sender] = User(username,
new uint[](0), new uint[](0), block.timestamp, true);
userAddresses[username] = msg.sender;
emit UserSignedUp(username, msg.sender);
7 years ago
return true;
}
6 years ago
function updateUsername(string memory newUsername) public returns (bool) {
require (hasUserSignedUp(msg.sender), "User hasn't signed up yet.");
require(!isUserNameTaken(newUsername), "Username is already taken.");
string memory oldUsername = getUsername(msg.sender);
delete userAddresses[users[msg.sender].username];
users[msg.sender].username = newUsername;
userAddresses[newUsername] = msg.sender;
emit UsernameUpdated(newUsername, oldUsername, msg.sender);
return true;
7 years ago
}
6 years ago
function getUsername(address userAddress) public view returns (string memory) {
require (hasUserSignedUp(userAddress), "User hasn't signed up yet.");
return users[userAddress].username;
7 years ago
}
6 years ago
function getUserAddress(string memory username) public view returns (address) {
return userAddresses[username];
7 years ago
}
function hasUserSignedUp(address userAddress) public view returns (bool) {
return users[userAddress].signedUp;
7 years ago
}
6 years ago
function isUserNameTaken(string memory username) public view returns (bool) {
if (getUserAddress(username)!=address(0))
7 years ago
return true;
return false;
}
function getUserDateOfRegister(address userAddress) public view returns (uint) {
require (hasUserSignedUp(userAddress), "User hasn't signed up yet.");
return users[userAddress].timestamp;
}
7 years ago
}