Created
May 13, 2018 14:14
-
-
Save firmianavan/035897c21a0779052df58197d241a7a4 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.23+commit.124ca40d.js&optimize=false&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract Forum { | |
uint16 constant maxValue = 10000;//when currentValue>maxValue let currentValue=currentValue%maxValue | |
uint16 currentValue;//increase by 1 in every Publish/Comment, and set item.valueSend with it as locater in current block | |
struct User { | |
address addr; | |
uint weight; //used to exercise his rights | |
string name; | |
string photo; //can be a url of your photo image | |
string words; //sth you want to show | |
} | |
struct Item { | |
uint id; //index in items | |
address author; | |
//bytes32 txHash; //hash of tx which contains the data | |
//there is no way to get current tx hash to locate the item, | |
//now we use ( blockNumber, author address, value send in the tx) instead | |
uint blockNumber; | |
uint16 valueSend; //used to locate the item | |
bool isRoot; // if this item is a article | |
uint parent; // if it is not root, its a comments of a article or other comment | |
uint approve; // num of people who approve this item | |
uint opposition;//num of people who opposite this item | |
uint[] comments;//index of the comment in items | |
} | |
User[] public userList;//all users | |
mapping(address => uint) users;// relation from address to user | |
Item[] public items;//all articles and comments, identifed by its index in this array | |
Item public latest; | |
mapping(address => uint[]) userItems;//relation from user to its items | |
event publishDone(address author, string article, bool isSuccess); | |
function signIn(string name, string photo, string words) private { | |
uint len = userList.length; | |
userList.push(User({ | |
addr: msg.sender, | |
weight: 0, | |
name: name, | |
photo: photo, | |
words: words | |
})); | |
users[msg.sender] = len; | |
} | |
function signInIfHaveNot() private { | |
uint idx = users[msg.sender]; | |
if ( idx <= 0 ){ | |
signIn('','',''); | |
} | |
} | |
///register with your address and edit your info{name: $(name), words: $(words), photo: $(photo)} | |
function setUserInfo(string name, string photo, string words) public{ | |
uint idx = users[msg.sender]; | |
if ( idx <= 0 ){ | |
signIn(name,photo,words); | |
}else { | |
userList[idx].name=name; | |
userList[idx].photo=photo; | |
userList[idx].words=words; | |
} | |
} | |
/// Publish your article | |
/// Note that you should convert it to byte[] with utf-8 encoding. | |
function publish(string article, bool isArticle, uint parent) public { | |
signInIfHaveNot(); | |
if ( msg.sender.call.value(currentValue)(article) ){ | |
uint len = items.length; | |
//use send value as article id | |
latest = Item({ | |
blockNumber: block.number, | |
approve: 0, | |
opposition: 0, | |
id: len, | |
valueSend: currentValue, | |
isRoot: isArticle, | |
parent: parent, | |
author: msg.sender, | |
comments: new uint[](0) | |
}); | |
items.push(latest); | |
userItems[msg.sender].push(len); | |
currentValue++; | |
if (currentValue >= maxValue){ | |
currentValue = currentValue % maxValue; | |
} | |
emit publishDone(msg.sender,article,true); | |
}else{ | |
emit publishDone(msg.sender,article,false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment