Make it possible to submit data to a task through a POST to /~user/task/submit.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3171 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2016-03-17 01:07:54 +00:00
parent a9879b7c8a
commit 9f23147678
2 changed files with 16 additions and 17 deletions

View File

@ -147,7 +147,6 @@ function handleRequest(request, response) {
var promise = handler.invoke(request, response); var promise = handler.invoke(request, response);
if (promise) { if (promise) {
promise.catch(function(error) { promise.catch(function(error) {
print(error);
response.reportError(error); response.reportError(error);
}); });
} }

View File

@ -201,13 +201,17 @@ function handler(request, response, packageOwner, packageName, uri) {
options.userName = credentials.session.name; options.userName = credentials.session.name;
} }
options.credentials = credentials; options.credentials = credentials;
var sessionId = form.decodeForm(request.query).sessionId; if (uri == "/submit") {
var isNewSession = false; process = getServiceProcess(packageOwner, packageName, "submit");
if (!getSessionProcess(packageOwner, packageName, sessionId, {create: false})) { } else {
sessionId = makeSessionId(); var sessionId = form.decodeForm(request.query).sessionId;
isNewSession = true; var isNewSession = false;
if (!getSessionProcess(packageOwner, packageName, sessionId, {create: false})) {
sessionId = makeSessionId();
isNewSession = true;
}
process = getSessionProcess(packageOwner, packageName, sessionId, options);
} }
process = getSessionProcess(packageOwner, packageName, sessionId, options);
process.lastActive = Date.now(); process.lastActive = Date.now();
if (uri === "/send") { if (uri === "/send") {
@ -243,14 +247,10 @@ function handler(request, response, packageOwner, packageName, uri) {
process.terminal.print(error); process.terminal.print(error);
}); });
} }
} else if (uri === "/post") { } else if (uri === "/submit") {
if (isNewSession) { return process.ready.then(function() {
response.writeHead(403, {"Content-Type": "text/plain; charset=utf-8"}); var payload = form.decodeForm(request.body, form.decodeForm(request.query));
print("post too soon"); return invoke(process.eventHandlers['onSubmit'], [payload]).then(function() {
response.end("Too soon.");
} else {
var payload = JSON.parse(request.body);
return invoke(process.eventHandlers['onPost'], [payload]).then(function() {
response.writeHead(200, { response.writeHead(200, {
"Content-Type": "text/plain; charset=utf-8", "Content-Type": "text/plain; charset=utf-8",
"Content-Length": "0", "Content-Length": "0",
@ -258,9 +258,9 @@ function handler(request, response, packageOwner, packageName, uri) {
"Pragma": "no-cache", "Pragma": "no-cache",
"Expires": "0", "Expires": "0",
}); });
response.end(""); return response.end("");
}); });
} });
} else if (uri === "/receive") { } else if (uri === "/receive") {
if (isNewSession) { if (isNewSession) {
var data = JSON.stringify({ var data = JSON.stringify({