Skip to content

Commit

Permalink
Merge pull request #7 from ljharb/factoid_username_tracking
Browse files Browse the repository at this point in the history
Adding the factoid's creator and a list of its editors to the JSON database
  • Loading branch information
dsamarin committed Sep 23, 2012
2 parents 808ed52 + a441223 commit 8a35950
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 24 additions & 3 deletions lib/factoidserv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,43 @@ var FactoidServer = module.exports = function(filename) {
this.db = new JSONSaver(filename);
};

var caseInsensitiveDeDupingInsert = function (array, value) {
var valueLower = value.toLowerCase();
var found = array.some(function (item, index) {
if (valueLower === item.toLowerCase()) {
array[index] = value;
return true;
});
});
if (!found) {
array.push(value);
}
};

FactoidServer.prototype.learn = function(key, value) {

FactoidServer.prototype.learn = function(key, value, username) {
if (!username) {
username = '';
}

var db = this.db.object.factoids;

var popularity = 0;
var creator = username;
var editors = [];
key = key.toLowerCase();

if (typeof db[key] !== "undefined") {
if (typeof db[key].alias !== "undefined") {
return this.learn(db[key].alias, value);
return this.learn(db[key].alias, value, username);
}
creator = db[key].creator;
editors = db[key].editors || editors;
caseInsensitiveDeDupingInsert(editors, username);
popularity = db[key].popularity || 0;
}

db[key] = {value: value, popularity: popularity};
db[key] = {value: value, popularity: popularity, creator: creator, editors: editors};
this.db.activity();
};

Expand Down
4 changes: 2 additions & 2 deletions shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var Shared = module.exports = {

/* Setting the text of a factoid */
if (operation === "=") {
this.factoids.learn(factoid, value);
this.factoids.learn(factoid, value, context.sender.name);
context.channel.send_reply(context.sender, "Learned `"+factoid+"`.");
return;

Expand All @@ -139,7 +139,7 @@ var Shared = module.exports = {
if (old === result) {
context.channel.send_reply(context.sender, "Nothing changed.");
} else {
this.factoids.learn(factoid, result);
this.factoids.learn(factoid, result, context.sender.name);
context.channel.send_reply(context.sender, "Changed `"+factoid+
"` to: "+result);
}
Expand Down

0 comments on commit 8a35950

Please sign in to comment.