Skip to content

Commit

Permalink
Allow to enable and disable the chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
oanguenot committed Jan 8, 2018
1 parent aea2db1 commit 5b3ac14
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 48 deletions.
122 changes: 75 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,26 @@ class RainbowAgent {
this._callbackTicket = null;
this._contextTicket = null;

this._isEnabled = true;

return this;
}

enable() {
this._isEnabled = true;
this.logger.log("warn", LOG_ID + "enable() - Mode is enabled");
}

disable() {
this._isEnabled = false;
this.logger.log("warn", LOG_ID + "disable() - Mode is disabled");
this._works.reset();
}

get state() {
return this._isEnabled;
}

start() {

let that = this;
Expand Down Expand Up @@ -124,86 +141,97 @@ class RainbowAgent {
this.events.on("onmessagereceived", (msg) => {
let work = null;

// Qualify message (check tag)
let scenario = that.tags.qualify(msg);

// Get work if exists
work = that.works.getWork(msg, scenario);
if (this._isEnabled) {

// Add to queue if work
if(!work) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Incorrect message received");
return;
}
// Qualify message (check tag)
let scenario = that.tags.qualify(msg);

if(work.queued) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Existing work is running. No user input expected...");
return;
}
// Get work if exists
work = that.works.getWork(msg, scenario);

// Store message if scenario is inProgress
if(work.state === Work.STATE.INPROGRESS) {
// Add to queue if work
if(!work) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Incorrect message received");
return;
}

if(!this.factory.isValid(work, work.scenario[work.step], msg.value)) {
if(work.queued) {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Existing work is running. No user input expected...");
return;
}
work.historize(msg);
}

that.fireEvent(work, msg).then((routedStep) => {
// Store message if scenario is inProgress
if(work.state === Work.STATE.INPROGRESS) {

if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
if(!this.factory.isValid(work, work.scenario[work.step], msg.value)) {
return;
}
work.historize(msg);
}

if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});
that.fireEvent(work, msg).then((routedStep) => {

if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}

if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});
} else {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Input not taken into account. Mode is disabled...");
}
});

// Listen when work has finished a task
this.events.on("ontaskfinished", (work) => {

if (work.state !== Work.STATE.CLOSED &&
work.state !== Work.STATE.BLOCKED &&
work.state !== Work.STATE.ABORTED &&
!work.pending) {

if(work.external) {

that.fireEvent(work, null).then((routedStep) => {

work.external = false;
if(that._isEnabled) {
if(work.external) {

if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}

that.fireEvent(work, null).then((routedStep) => {

work.external = false;

if(routedStep) {
// force next step
work.forcedNextStep = routedStep;
}

if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});

} else {
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
});

} else {
if(work.waiting) {
that.delayer.delay(work);
} else {
that.queue.addToQueue(work);
}
}
else {
that.logger.log("warn", LOG_ID + "onmessagereceived() - Input not taken into account. Mode is disabled...");
}
}
else {
if(work.pending) {
that.logger.log("info", LOG_ID + "ontaskfinished() - Work[" + work.id + "] is waiting for incoming inputs...");
}
else {
that.logger.log("info", LOG_ID + "ontaskfinished() - Work [" + work.id + "] is closed");
that.logger.log("info", LOG_ID + "ontaskfinished() - Work [" + work.id + "] is closed, blocked or aborted");

work.endedOn = new Date();

Expand Down
2 changes: 2 additions & 0 deletions modules/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ class Work {

abort() {
this._state = Work.STATE.ABORTED;
this._pending = false;
this._waiting = 0;
this.log("warn", LOG_ID + "abort() - Work[" + this._id + "] (state) changed to '" + this._state + "'");
}

Expand Down
7 changes: 7 additions & 0 deletions modules/works.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Works {
});
}

reset() {
this._works.forEach((work) => {
work.abort();
this._event.emit("ontaskfinished", work);
});
}

getWork(message, scenario) {

let createWork = (jid, tag, from, scenario) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rainbow-chatbot",
"version": "1.34.1",
"version": "1.34.2",
"description": "Rainbow ChatBot library for the Rainbow SDK for Node.JS",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions test/test_work.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ describe('Work setter and getter', function() {

it('should abort the work', function() {
let work = new Work();
work.pending = true;
work.waiting = 1000;
work.abort();
expect(work.isAborted).is.equal(true);
expect(work.pending).is.equal(false);
expect(work.waiting).is.equal(0);
});

it('should block the work', function() {
Expand Down

0 comments on commit 5b3ac14

Please sign in to comment.