Skip to content

Commit

Permalink
#74 Adds linking to tasks via tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrem committed Sep 10, 2016
1 parent ec6573f commit fa42b5a
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 9 deletions.
6 changes: 6 additions & 0 deletions chain/contracts/Linker.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "User.sol";
import "Task.sol";

contract Linker {

Expand All @@ -19,4 +20,9 @@ contract Linker {
return User(userAddress).associateWithTeam(teamname);
}

function linkFileToTask(address taskAddr, bytes32 fileHash) returns (bool) {
registerActionEvent('LINK FILE TO TASK');
return Task(taskAddr).associateWithFile(fileHash);
}

}
9 changes: 9 additions & 0 deletions chain/contracts/Task.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import "SequenceArray.sol";

contract Task {

bytes32 public id; // immutable
Expand All @@ -11,6 +13,8 @@ contract Task {
bytes32 public createdAt; // immutable
bytes32 public token; // immutable

SequenceArray attachments = new SequenceArray();

// Constructor
function Task(
bytes32 _id,
Expand All @@ -34,4 +38,9 @@ contract Task {
createdAt = _createdAt;
token = _token;
}

function associateWithFile(bytes32 fileHash) returns (bool isOverwrite) {
isOverwrite = attachments.insert(fileHash, this);
return isOverwrite;
}
}
7 changes: 5 additions & 2 deletions chain/contracts/TaskManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract TaskManager {
{
t = new Task(_id, _title, _desc, _status, _complete, _reward, _participants, _creator, _createdAt, _token);

bool isOverwrite = taskList.insert(_id, t);
bool isOverwrite = taskList.insert(_token, t);
registerActionEvent("ADD TASK");
// TODO needs a verification of insert success
return t;
Expand All @@ -54,9 +54,12 @@ contract TaskManager {
return taskList.keyAtIndex(_idx);
}

function getTaskFromToken(bytes32 token) constant returns (address) {
return taskList.value(token);
}

function getTaskListSize() constant returns (uint) {
registerActionEvent("GET TASKLIST SIZE");
return taskList.size();

}
}
26 changes: 26 additions & 0 deletions chain/js/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ var logger = require(__libs+'/eris/eris-logger');
var log = logger.getLogger('chain.api');


// ######################
// USER ENPOINTS
// ######################

/**
* attachFileToTask - description
*
* @param {type} token description
* @param {type} fileHash description
* @param {type} callback description
* @return {type} description
*/
function attachFileToTask (token, fileHash, callback) {
log.info('chain.attachFileToTask()');
taskManager.getTaskAddressFromToken(token, function (tokenErr, taskAddr) {
if (tokenErr)
return callback(tokenErr, null);

linker.linkFileToTask(taskAddr, fileHash, function (err, isOverwrite) {
return callback(err, isOverwrite);
});
});
}


// ######################
// USER ENPOINTS
// ######################
Expand Down Expand Up @@ -287,6 +312,7 @@ function mintNewId (domain, callback) {


module.exports = {
attachFileToTask: attachFileToTask,
isUsernameTaken: isUsernameTaken,
signup: signup,
login: login,
Expand Down
20 changes: 19 additions & 1 deletion chain/js/linker.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,25 @@ function linkTeamToUser (username, teamname, callback) {
});
}


/**
* linkFileToTask - description
*
* @param {type} taskAddr description
* @param {type} fileHash description
* @param {type} callback description
* @return {type} description
*/
function linkFileToTask (taskAddr, fileHash, callback) {
log.debug("linkFileToTask() -> taskAddr: %s, fileHash: %s", taskAddr, fileHash);
LinkerContract.linkFileToTask(taskAddr, eris.str2hex(fileHash), function (err, isOverwrite) {
log.debug("linkFileToTask() -> LinkerContract -> isOverwrite: ", isOverwrite);
return err ? callback(err, null) : callback(err, isOverwrite);
});
}

module.exports = {
linkTaskToUser: linkTaskToUser,
linkTeamToUser: linkTeamToUser
linkTeamToUser: linkTeamToUser,
linkFileToTask: linkFileToTask
};
12 changes: 9 additions & 3 deletions chain/js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ var init = function () {
});

app.post('/upload', upload.single('attachment'), function (req, res) {
var token = req.body.token;
var fileHash = req.file.filename;

log.info('POST /upload');
log.debug(req.body);
log.debug(req.file); // -> undefined, FIXME
res.sendStatus(200);
log.info("Passed task token: ", token);
log.info("Passed .txt file: ", req.file);

chain.attachFileToTask(token, fileHash, function (err, isOverwrite) {
err ? res.sendStatus(500) : res.sendStatus(200);
});
});


Expand Down
20 changes: 19 additions & 1 deletion chain/js/taskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,30 @@ var chainUtils = require(__js+'/util/chainUtils');
});
}


/**
* getTaskAddressFromToken - description
*
* @param {type} token description
* @param {type} callback description
* @return {type} description
*/
function getTaskAddressFromToken (token, callback) {
taskManagerContract.getTaskFromToken(eris.str2hex(token), function (error, taskAddr) {
error
? log.error("getTaskAddressFromToken() -> Error: " + error.stack)
: log.debug("getTaskAddressFromToken: ", token);
callback(error, taskAddr);
});
}

module.exports = {
addTask: addTask,
getAllTaskAddresses: getAllTaskAddresses,
getTaskAtIndex: getTaskAtIndex,
getTaskAtAddress: getTaskAtAddress,
getTaskListSize: getTaskListSize,
getTaskKeyAtIndex: getTaskKeyAtIndex
getTaskKeyAtIndex: getTaskKeyAtIndex,
getTaskAddressFromToken: getTaskAddressFromToken
};
}());
15 changes: 14 additions & 1 deletion chain/test/chain_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable */

var assert = require('chai').assert;
var randtoken = require('rand-token');
var chain = require(__js+'/chain');

var mockUser = {
Expand All @@ -22,7 +23,8 @@ var mockTask = {
reward: "200",
participants: ["alpha", "beta", "gamma"],
creator: "Ben",
createdAt: String(Date.now())
createdAt: String(Date.now()),
token: randtoken.generate(8)
};

var mockTeamAddress;
Expand Down Expand Up @@ -149,6 +151,17 @@ describe('chain', function () {
});
});

describe('attachFileToTask', function () {
it('attaches a file reference hash to the passed token\'s associated task', function (done) {
var mockHash = 'abcdefghijklmnop';
chain.attachFileToTask(mockTask.token, mockHash, function (err, isOverwrite) {
assert.isNull(err);
assert.isBoolean(isOverwrite);
done();
});
});
})

describe('mintNewId(task)', function () {
it('mints a new ID for the `task` data domain', function (done) {
chain.mintNewId('task', function (err, id) {
Expand Down
2 changes: 1 addition & 1 deletion chain/test/linker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var username = 'test_usermanager1';
var pseudoTaskAddr = '2BEBADFCA9F0A28AF3631BC01E8DFF1AA18ABABA';

describe('Linker', function () {
this.timeout(3000);
this.timeout(5000);

describe('linkTaskToUser()', function () {
it('links a task address to a user contract', function (done) {
Expand Down
10 changes: 10 additions & 0 deletions chain/test/taskManager_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe("Task Manager", function () {
});
});

describe("getTaskAddressFromToken", function () {
it("retrieves a task address that matches the passed attachment token", function (done) {
taskManager.getTaskAddressFromToken(testTask.token, function (err, taskAddr) {
assert.isNull(err);
assert.strictEqual(taskAddr, refAddr);
done();
});
});
});

});

require('./userManager_test');

0 comments on commit fa42b5a

Please sign in to comment.