Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor asynchronous calls in the code to use async/await #50

Open
wants to merge 3 commits into
base: javascript
Choose a base branch
from
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
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ services:

# this container's job is just run the command to initialize the replica set.
# it will run the command and remove himself (it will not stay running)
# mongo-init-replica:
# image: mongo:3.2
# command: 'mongo mongo/rocketchat --eval "rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''localhost:27017'' } ]})"'
# depends_on:
# - mongo
mongo-init-replica:
image: mongo:3.2
command: 'mongo mongo/rocketchat --eval "rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''localhost:27017'' } ]})"'
depends_on:
- mongo

hubot-natural:
image: diegodorgam/hubot-natural:latest
Expand Down
48 changes: 26 additions & 22 deletions scripts/actions/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const { msgVariables, stringElseRandomKey } = require('../lib/common');

const livechat_department = (process.env.LIVECHAT_DEPARTMENT_ID || null );
const livechat_department = (process.env.LIVECHAT_DEPARTMENT_ID || null);

class Respond {
constructor(interaction) {
Expand Down Expand Up @@ -40,29 +40,33 @@ class Respond {
}
}


livechatTransfer(msg, delay, lc_dept, offline_message, type) {
if (delay == null) { delay = 3000; }
return setTimeout((() => msg.robot.adapter.callMethod('livechat:transfer', {
roomId: msg.envelope.room,
departmentId: lc_dept
}
).then(function(result) {
if (result === true) {
return console.log('livechatTransfer executed!');
} else {
console.log('livechatTransfer NOT executed!');
switch (type) {
case 'block':
var messages = offline_message.map(line => msgVariables(line, msg));
return msg.sendWithNaturalDelay(messages);
case 'random':
var message = stringElseRandomKey(offline_message);
message = msgVariables(message, msg);
return msg.sendWithNaturalDelay(message);
}
}
}) ), delay);
async function getResult() {
try {
let result = await msg.robot.adapter.callMethod('livechat:transfer', {
roomId: msg.envelope.room,
department: lc_dept
});
} catch (err) {
console.log(err);
}
if (result === true) {
return console.log('livechatTransfer executed!');
} else {
switch (type) {
case 'block':
var messages = offline_message.map(line => msgVariables(line, msg));
return msg.sendWithNaturalDelay(messages);
case 'random':
var messages = offline_message.map(line => msgVariables(line, msg));
var message = stringElseRandomKey(offline_message);
message = msgVariables(message, msg);
return msg.sendWithNaturalDelay(message);
}
}
}
return setTimeout(getResult(), delay);
}
}

Expand Down
13 changes: 8 additions & 5 deletions scripts/lib/security.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const security = {};

security.getUserRoles = function(robot) {
security.getUserRoles = async function(robot) {
const usersAndRoles = {};
robot.adapter.callMethod('getUserRoles').then(function(users) {
try {
const users = await robot.adapter.callMethod('getUserRoles');
users.forEach(function(user) {
user.roles.forEach(function(role) {
if (typeof usersAndRoles[role] === 'undefined') {
usersAndRoles[role] = [];
}
usersAndRoles[role].push(user.username);
});
});
});
})
})
}catch(err){
console.log(err);
}
return usersAndRoles;
};

Expand Down