Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ app.post('/submit_question', function(request, response) {
console.log("POST /submit_question", "title: " + request.body.title, "details: " + request.body.details,
"user_id: " + request.body.user_id, "bounty: " + request.body.bounty);
let bounty = Number(request.body.bounty);
let user_addr = request.body.user_id
let placeholder_id = ObjectId("73b312067720199e377e6fb9"); // random 24 digit hex string
let questionHash = sha256(bounty + request.body.title + request.body.details + request.body.time_exp + placeholder_id);

model.createQuestion(bounty, request.body.time_exp, request.body.title, request.body.details, placeholder_id, questionHash);
model.createQuestion(bounty, request.body.time_exp, request.body.title, request.body.details, placeholder_id, questionHash, user_addr);

response.set('Content-type', 'application/json');
response.status(STATUS_OK);
Expand All @@ -74,18 +75,18 @@ app.post('/submit_question', function(request, response) {
app.post('/upvote', function(request, response) {
console.log("POST /upvotes " + "user_ID: " + request.body.user_id + "; answer_ID: " + request.body.answer_id);
let answer_id = ObjectId(request.body.answer_id);
let user_id = ObjectId(request.body.user_id);
model.upvoteAnswer(answer_id, user_id);
let user_addr = request.body.user_id;
model.upvoteAnswer(answer_id, user_addr);
response.set('Content-type', 'application/json');
response.status(STATUS_OK);
response.send();
})

app.post('/add_answer', function(request, response) {
console.log("POST /add_answer " + "question_ID: " + request.body.question_id + "; answer: " + request.body.text)
console.log("POST /add_answer " + "question_ID: " + request.body.question_id + "; answer: " + request.body.text)
let placeholder_id = ObjectId("915bed12d3704298d62224fe");
let question_id = ObjectId(request.body.question_id);
model.createAnswer(placeholder_id, question_id, request.body.text);
model.createAnswer(request.body.user_addr, question_id, request.body.text);
response.set('Content-type', 'application/json');
response.status(STATUS_OK);
response.send();
Expand Down
2 changes: 1 addition & 1 deletion public/app/js/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function app() {
asker_address: userAccount
}

addPostRequest.open('POST', '/submit_question')
addPostRequest.open('POST', '/create_user')
addPostRequest.setRequestHeader('Content-type', 'application/json')
addPostRequest.send(JSON.stringify(user_details));

Expand Down
20 changes: 13 additions & 7 deletions public/app/js/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ var createUser = async function(username, address) {
upvotes: []
});
try {
let savedUser = await newUser.save();
let savedUser = await schema.User.findOneAndUpdate({address: address}, {$setOnInsert: newUser}, {upsert: true, returnNewDocument: true});
console.log("saved user successfully");
console.log(savedUser);
return savedUser.id;
} catch (err) {
console.log("err in createUser");
console.log(err);
}
}

var createQuestion = async function(bounty, timeExp, title, body, askerId, questionHash) {
var createQuestion = async function(bounty, timeExp, title, body, askerId, questionHash, askerAddr) {
console.log("askerId: " + askerId);
let newQuestion = new schema.Question ({
answers: [],
Expand All @@ -45,7 +46,7 @@ var createQuestion = async function(bounty, timeExp, title, body, askerId, quest
});
try {
let savedQuestion = await newQuestion.save();
await schema.User.findOneAndUpdate({_id: askerId}, {$push: {questions: savedQuestion.id}}, {upsert: true});
await schema.User.findOneAndUpdate({address: askerAddr}, {$push: {questions: savedQuestion.id}}, {upsert: true});
return savedQuestion.id;
} catch (err) {
console.log("error in create question!");
Expand All @@ -61,20 +62,25 @@ var createAnswer = async function(answererId, questionId, body) {
body: body
});
try {
console.log(newAnswer);
let savedAnswer = await newAnswer.save();
let updatedQuestion = await schema.Question.findOneAndUpdate({_id: questionId}, {$push: {answers: savedAnswer.id}}, {upsert: true});
let udpatedUser = await schema.User.findOneAndUpdate({_id: answererId}, {$push: {answers: savedAnswer.id}}, {upsert: true});
let updatedUser = await schema.User.findOneAndUpdate({address: answererId}, {$push: {answers: savedAnswer.id}}, {upsert: true});
return savedAnswer.id;
} catch (err) {
console.log("error in createanswer");
console.log(err);
}
}

var upvoteAnswer = async function(answerId, voterId) {
var upvoteAnswer = async function(answerId, voterAddr) {
// voterID: 0xC6941bc0804722076716F4ba131D7B7B663E0a92
// answerID: 5af9d02e092138575b67a58a
// user id wasnt being used and its actually user address. change schema? or figure out new way to do this
try {
let updatedAnswer = await schema.Answer.findOneAndUpdate({_id: answerId}, {$push: {voters: voterId}}, {upsert: true});
let updatedUser = await schema.User.findOneAndUpdate({_id: voterId}, {$push: {answers: answerId}}, {upsert: true});
let placeholder_id = ObjectId("73b312067720199e377e6fb9"); // random 24 digit hex string
let updatedAnswer = await schema.Answer.findOneAndUpdate({_id: answerId}, {$push: {voters: placeholder_id}}, {upsert: true});
let updatedUser = await schema.User.findOneAndUpdate({address: voterAddr}, {$push: {answers: answerId}}, {upsert: true});
console.log("updated answer successfully");
} catch (err) {
console.log("error in upvote answer");
Expand Down
7 changes: 5 additions & 2 deletions public/app/js/question-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ function submitAnswer() {
console.log('omg')
var box_text = document.getElementById("answer_input").value
console.log("answer_submission " + box_text)
console.log(QuestionView.question_id)
console.log(localStorage.getItem("userAccount"))
answer_data = {
question_id: QuestionView.question_id,
user_id: localStorage.getItem("userAccount"),
user_addr: localStorage.getItem("userAccount"),
text: box_text
}
PostModel.addAnswer(answer_data)
Expand Down Expand Up @@ -144,9 +146,10 @@ function closeQuestion() {
console.log("winner is " + highest_answerer_id);
console.log(question_detail);
console.log("call metamask");
distributeBounty(highest_answerer_id, "hi claire", question_detail.question.questionHash);
distributeBounty(highest_answerer_id, 1234, question_detail.question.questionHash);
}
});

xmlQuestionDetail.open("GET", QuestionView.remoteHost + 'question_detail' + "?q_id=" + encodeURIComponent(QuestionView.post_data._id));
xmlQuestionDetail.send(null);
}
Expand Down
7 changes: 6 additions & 1 deletion public/app/question_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<h1>
Pancake's Q&A Blockchain Site
</h1>
<button type="submit" style="float: right;margin-top: 60px;display: none" onclick="closeQuestion();" id="master">
<button type="submit" style="float: right;margin-top: 60px;" onclick="closeQuestion();" id="master">
Distribute Bounty
</button>
</div>
Expand Down Expand Up @@ -66,6 +66,11 @@ <h3> I think Max is very cool. I just want to know exactly how cool. </h3>
<script src="./js/question-main-view.js"></script>
<script src="./js/search-view.js"></script>

<!-- App -->
<script src="/js/metamask.js"></script>

<!-- Main -->
<script src="./js/question-main.js"></script>

<script src="https://dapps.stanford.edu/web3/web3-1.0.0-beta.34.min.js"></script>
</body>