|
@ -4,7 +4,8 @@ contract Forum { |
|
|
|
|
|
|
|
|
//----------------------------------------USER---------------------------------------- |
|
|
//----------------------------------------USER---------------------------------------- |
|
|
struct User { |
|
|
struct User { |
|
|
string username; |
|
|
string username; // TODO: set an upper bound instead of arbitrary string |
|
|
|
|
|
OrbitDB orbitdb; |
|
|
uint[] topicIDs; // IDs of the topics the user created |
|
|
uint[] topicIDs; // IDs of the topics the user created |
|
|
uint[] postIDs; // IDs of the posts the user created |
|
|
uint[] postIDs; // IDs of the posts the user created |
|
|
uint timestamp; |
|
|
uint timestamp; |
|
@ -17,10 +18,11 @@ contract Forum { |
|
|
event UserSignedUp(string username, address userAddress); |
|
|
event UserSignedUp(string username, address userAddress); |
|
|
event UsernameUpdated(string newName, string oldName,address userAddress); |
|
|
event UsernameUpdated(string newName, string oldName,address userAddress); |
|
|
|
|
|
|
|
|
function signUp(string memory username) public returns (bool) { |
|
|
function signUp(string memory username, string memory orbitDBId, string memory orbitTopicsDB, string memory orbitPostsDB, string memory orbitPublicKey, string memory orbitPrivateKey) public returns (bool) { |
|
|
require (!hasUserSignedUp(msg.sender), "User has already signed up."); |
|
|
require (!hasUserSignedUp(msg.sender), "User has already signed up."); |
|
|
require(!isUserNameTaken(username), "Username is already taken."); |
|
|
require(!isUserNameTaken(username), "Username is already taken."); |
|
|
users[msg.sender] = User(username, |
|
|
users[msg.sender] = User(username, |
|
|
|
|
|
OrbitDB(orbitDBId,orbitTopicsDB, orbitPostsDB, orbitPublicKey, orbitPrivateKey), |
|
|
new uint[](0), new uint[](0), block.timestamp, true); |
|
|
new uint[](0), new uint[](0), block.timestamp, true); |
|
|
userAddresses[username] = msg.sender; |
|
|
userAddresses[username] = msg.sender; |
|
|
emit UserSignedUp(username, msg.sender); |
|
|
emit UserSignedUp(username, msg.sender); |
|
@ -57,9 +59,149 @@ contract Forum { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getUserTopics(address userAddress) public view returns (uint[] memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up yet."); |
|
|
|
|
|
return users[userAddress].topicIDs; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getUserPosts(address userAddress) public view returns (uint[] memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up yet."); |
|
|
|
|
|
return users[userAddress].postIDs; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function getUserDateOfRegister(address userAddress) public view returns (uint) { |
|
|
function getUserDateOfRegister(address userAddress) public view returns (uint) { |
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up yet."); |
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up yet."); |
|
|
return users[userAddress].timestamp; |
|
|
return users[userAddress].timestamp; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------OrbitDB---------------------------------------- |
|
|
|
|
|
struct OrbitDB { |
|
|
|
|
|
string id; // TODO: set an upper bound instead of arbitrary string |
|
|
|
|
|
string topicsDB; //TODO: not sure yet which of these are actually needed |
|
|
|
|
|
string postsDB; |
|
|
|
|
|
string publicKey; |
|
|
|
|
|
string privateKey; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getOrbitDBId(address userAddress) public view returns (string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return users[userAddress].orbitdb.id; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getOrbitTopicsDB(address userAddress) public view returns (string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return users[userAddress].orbitdb.topicsDB; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getOrbitPostsDB(address userAddress) public view returns (string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return users[userAddress].orbitdb.postsDB; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getOrbitPublicKey(address userAddress) public view returns (string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return users[userAddress].orbitdb.publicKey; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//TODO: encrypt using Metamask in the future |
|
|
|
|
|
function getOrbitPrivateKey(address userAddress) public view returns (string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return users[userAddress].orbitdb.privateKey; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getOrbitDBInfo(address userAddress) public view returns (string memory, string memory, string memory, string memory, string memory) { |
|
|
|
|
|
require (hasUserSignedUp(userAddress), "User hasn't signed up."); |
|
|
|
|
|
return ( |
|
|
|
|
|
users[userAddress].orbitdb.id, |
|
|
|
|
|
users[userAddress].orbitdb.topicsDB, |
|
|
|
|
|
users[userAddress].orbitdb.postsDB, |
|
|
|
|
|
users[userAddress].orbitdb.publicKey, |
|
|
|
|
|
users[userAddress].orbitdb.privateKey |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------POSTING---------------------------------------- |
|
|
|
|
|
struct Topic { |
|
|
|
|
|
uint topicID; |
|
|
|
|
|
address author; |
|
|
|
|
|
uint timestamp; |
|
|
|
|
|
uint[] postIDs; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct Post { |
|
|
|
|
|
uint postID; |
|
|
|
|
|
address author; |
|
|
|
|
|
uint timestamp; |
|
|
|
|
|
uint topicID; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint numTopics; // Total number of topics |
|
|
|
|
|
uint numPosts; // Total number of posts |
|
|
|
|
|
|
|
|
|
|
|
mapping (uint => Topic) topics; |
|
|
|
|
|
mapping (uint => Post) posts; |
|
|
|
|
|
|
|
|
|
|
|
event TopicCreated(uint topicID, uint postID); |
|
|
|
|
|
event PostCreated(uint postID, uint topicID); |
|
|
|
|
|
|
|
|
|
|
|
function createTopic() public returns (uint, uint) { |
|
|
|
|
|
require(hasUserSignedUp(msg.sender)); // Only registered users can create topics |
|
|
|
|
|
//Creates topic |
|
|
|
|
|
uint topicID = numTopics++; |
|
|
|
|
|
topics[topicID] = Topic(topicID, msg.sender, block.timestamp, new uint[](0)); |
|
|
|
|
|
users[msg.sender].topicIDs.push(topicID); |
|
|
|
|
|
|
|
|
|
|
|
//Adds first post to topic |
|
|
|
|
|
uint postID = numPosts++; |
|
|
|
|
|
posts[postID] = Post(postID, msg.sender, block.timestamp, topicID); |
|
|
|
|
|
topics[topicID].postIDs.push(postID); |
|
|
|
|
|
users[msg.sender].postIDs.push(postID); |
|
|
|
|
|
|
|
|
|
|
|
emit TopicCreated(topicID, postID); |
|
|
|
|
|
return (topicID, postID); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function createPost(uint topicID) public returns (uint) { |
|
|
|
|
|
require(hasUserSignedUp(msg.sender)); // Only registered users can create posts |
|
|
|
|
|
require(topicID<numTopics); // Only allow posting to a topic that exists |
|
|
|
|
|
uint postID = numPosts++; |
|
|
|
|
|
posts[postID] = Post(postID, msg.sender, block.timestamp, topicID); |
|
|
|
|
|
topics[topicID].postIDs.push(postID); |
|
|
|
|
|
users[msg.sender].postIDs.push(postID); |
|
|
|
|
|
emit PostCreated(postID, topicID); |
|
|
|
|
|
return postID; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getNumberOfTopics() public view returns (uint) { |
|
|
|
|
|
return numTopics; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getTopic(uint topicID) public view returns (string memory, address, string memory, uint, uint[] memory) { |
|
|
|
|
|
//require(hasUserSignedUp(msg.sender)); needed? |
|
|
|
|
|
require(topicID<numTopics); |
|
|
|
|
|
return (getOrbitTopicsDB(topics[topicID].author), |
|
|
|
|
|
topics[topicID].author, |
|
|
|
|
|
users[topics[topicID].author].username, |
|
|
|
|
|
topics[topicID].timestamp, |
|
|
|
|
|
topics[topicID].postIDs |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getTopicPosts(uint topicID) public view returns (uint[] memory) { |
|
|
|
|
|
require(topicID<numTopics); // Topic should exist |
|
|
|
|
|
return topics[topicID].postIDs; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function getPost(uint postID) public view returns (string memory, address, string memory, uint, uint) { |
|
|
|
|
|
//require(hasUserSignedUp(msg.sender)); needed? |
|
|
|
|
|
require(postID<numPosts); |
|
|
|
|
|
return (getOrbitPostsDB(posts[postID].author), |
|
|
|
|
|
posts[postID].author, |
|
|
|
|
|
users[posts[postID].author].username, |
|
|
|
|
|
posts[postID].timestamp, |
|
|
|
|
|
posts[postID].topicID |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
} |
|
|
} |