pragma solidity ^0.5.4; contract Forum { //----------------------------------------USER---------------------------------------- struct User { string username; 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() } mapping (address => User) users; mapping (string => address) userAddresses; event UserSignedUp(string username, address userAddress); event UsernameUpdated(string newName, string oldName,address userAddress); 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); return true; } 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; } function getUsername(address userAddress) public view returns (string memory) { require (hasUserSignedUp(userAddress), "User hasn't signed up yet."); return users[userAddress].username; } function getUserAddress(string memory username) public view returns (address) { return userAddresses[username]; } function hasUserSignedUp(address userAddress) public view returns (bool) { return users[userAddress].signedUp; } function isUserNameTaken(string memory username) public view returns (bool) { if (getUserAddress(username)!=address(0)) 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; } }