Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Move the checks of users existence and activity to hubot-routines #164

Open
wants to merge 1 commit into
base: master
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
67 changes: 14 additions & 53 deletions src/birthder.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@
return
}

robot.enter(msg => {
robot.enter(async msg => {
if (msg.message.user.roomID === 'GENERAL') {
const brain = robot.brain.data.users
const username = msg.message.user.name
const user = Object.values(brain).filter(item => item.name === username).shift()
const user = await routines.findUserByName(robot, username)
if (!user.dateOfBirth) {
robot.adapter.sendDirect({ user: { name: user.name } }, `Welcome to ${utils.COMPANY_NAME}! :tada:\nEmm... where was I?\nOh! Please, enter your date birth (DD.MM.YYYY).`)
}
Expand Down Expand Up @@ -83,7 +82,6 @@
let date
let name
let user
let users

if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
Expand All @@ -92,45 +90,34 @@

name = msg.match[2].trim()
date = msg.match[3]
users = []

for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
user = await routines.findUserByName(robot, name)

if (!routines.isValidDate(date, utils.DATE_FORMAT)) {
msg.send(utils.MSG_INVALID_DATE)
return
}

if (users.length === 1) {
user = users[0]
if (user) {
user.dateOfBirth = date

return msg.send(`Saving ${name}'s birthday.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
})

// Print the users names whose birthdays match the specified date.
robot.respond(routes.check, async (msg) => {
let allUsers
let date
let message
let users = []
let users
let userNames

date = msg.match[2]
date = moment(msg.match[2], utils.SHORT_DATE_FORMAT)

for (const u of utils.findUsersByDate(utils.BDAY_EVENT_TYPE, moment(date, utils.SHORT_DATE_FORMAT), robot.brain.data.users)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
allUsers = await routines.getAllUsers(robot)
users = utils.findUsersByDate(utils.BDAY_EVENT_TYPE, date, allUsers)

if (users.length === 0) {
return msg.send('Could not find any user with the specified birthday.')
Expand All @@ -145,31 +132,21 @@
// Delete the birthday associated with the specified user name.
robot.respond(routes.delete, async (msg) => {
let name = msg.match[2].trim()
let user
let users = []
let user = await routines.findUserByName(robot, name)

if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
return
}

for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}

if (users.length === 1) {
user = users[0]
if (user) {
if (!user.dateOfBirth) {
return msg.send('A birth date is not specified for the user.')
}

user.dateOfBirth = null

return msg.send(`Removing ${name}'s birthday.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
Expand Down Expand Up @@ -205,13 +182,7 @@
}

let message

const allUsers = []
for (const u of Object.values(robot.brain.data.users)) {
if (await routines.isUserActive(robot, u)) {
allUsers.push(u)
}
}
const allUsers = await routines.getAllUsers(robot)

message = allUsers
.filter(user => routines.isValidDate(user[attr], utils.DATE_FORMAT))
Expand Down Expand Up @@ -240,7 +211,6 @@
let date
let name
let user
let users

if (!await routines.isAdmin(robot, msg.message.user.name.toString())) {
msg.send(utils.MSG_PERMISSION_DENIED)
Expand All @@ -249,26 +219,17 @@

name = msg.match[2].trim()
date = msg.match[3]
users = []

for (const u of robot.brain.usersForFuzzyName(name)) {
if (await routines.isUserActive(robot, u)) {
users.push(u)
}
}
user = await routines.findUserByName(robot, name)

if (!routines.isValidDate(date, utils.DATE_FORMAT)) {
msg.send(utils.MSG_INVALID_DATE)
return
}

if (users.length === 1) {
user = users[0]
if (user) {
user.dateOfFwd = date

return msg.send(`Saving ${name}'s first working day.`)
} else if (users.length > 1) {
return msg.send(utils.getAmbiguousUserText(users))
} else {
return msg.send(`I have never met ${name}.`)
}
Expand Down
51 changes: 16 additions & 35 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ exp.createBirthdayChannel = async (robot, username) => {
const now = moment()
const dayMonth = now.format('DD.MM')
const channelName = `${username}-birthday-channel-${dayMonth}-id${now.milliseconds()}`
const users = Object.values(robot.brain.data.users)
const users = await routines.getAllUsers(robot)
.filter(user => {
return user.name !== username && !exp.BIRTHDAY_CHANNEL_BLACKLIST.includes(user.name)
})
Expand All @@ -117,7 +117,7 @@ exp.createBirthdayChannel = async (robot, username) => {
name: channelName,
members: users
})
const userInstance = robot.brain.userForName(username)
const userInstance = await routines.findUserByName(robot, username)
userInstance.birthdayChannel = {
roomName: channelName
}
Expand All @@ -132,7 +132,7 @@ exp.createBirthdayChannel = async (robot, username) => {
* @returns {boolean}
*/
exp.isBotInBirthdayChannel = async (robot, username) => {
const userInstance = robot.brain.userForName(username)
const userInstance = await routines.findUserByName(robot, username)
if (userInstance.birthdayChannel) {
return true
}
Expand Down Expand Up @@ -308,21 +308,17 @@ exp.removeExpiredBirthdayChannels = async (robot) => {
return
}

let targetDay = moment()
let users = []
let channelName

let targetDay = moment()
targetDay.add(-exp.BIRTHDAY_CHANNEL_TTL, 'day')
for (const bdayUser of exp.findUsersByDate(exp.BDAY_EVENT_TYPE, targetDay, robot.brain.data.users)) {
if (await routines.isUserActive(robot, bdayUser)) {
users.push(bdayUser)
}
}
let allUsers = await routines.getAllUsers(robot)
let users = exp.findUsersByDate(exp.BDAY_EVENT_TYPE, targetDay, allUsers)

if (users.length) {
for (let user of users) {
if (await exp.isBotInBirthdayChannel(robot, user.name)) {
const userInstance = robot.brain.userForName(user.name)
const userInstance = await routines.findUserByName(robot, user.name)
if (userInstance.birthdayChannel) {
channelName = userInstance.birthdayChannel.roomName
await robot.adapter.api.post('groups.delete', { roomName: channelName })
Expand All @@ -340,15 +336,12 @@ exp.removeExpiredBirthdayChannels = async (robot) => {
*/
exp.sendReminders = async (robot, amountOfTime, unitOfTime) => {
let targetDay = moment()
let users = []
let allUsers
let users

targetDay.add(amountOfTime, unitOfTime)

for (const bdayUser of exp.findUsersByDate(exp.BDAY_EVENT_TYPE, targetDay, robot.brain.data.users)) {
if (await routines.isUserActive(robot, bdayUser)) {
users.push(bdayUser)
}
}
allUsers = await routines.getAllUsers(robot)
users = exp.findUsersByDate(exp.BDAY_EVENT_TYPE, targetDay, allUsers)

if (users.length > 0) {
if (exp.CREATE_BIRTHDAY_CHANNELS) {
Expand All @@ -359,7 +352,6 @@ exp.sendReminders = async (robot, amountOfTime, unitOfTime) => {
}
}

const allUsers = Object.values(robot.brain.data.users)
for (let user of allUsers) {
const filteredUsers = users.filter(item => item.name !== user.name)
if (!filteredUsers.length) {
Expand All @@ -379,14 +371,10 @@ exp.sendReminders = async (robot, amountOfTime, unitOfTime) => {
*/
exp.sendCongratulations = async (robot, event) => {
let messageText
let sortedUsers = []
let tenor

for (const user of exp.findUsersByDate(event, moment(), robot.brain.data.users)) {
if (await routines.isUserActive(robot, user)) {
sortedUsers.push(user)
}
}
const allUsers = await routines.getAllUsers(robot)
const sortedUsers = exp.findUsersByDate(event, moment(), allUsers)

if (sortedUsers.length > 0) {
let userNames = sortedUsers.map(user => `@${user.name}`)
Expand Down Expand Up @@ -432,20 +420,13 @@ exp.sendCongratulations = async (robot, event) => {
* @return {void}
*/
exp.detectBirthdaylessUsers = async robot => {
let usersWithoutBirthday = Object.values(robot.brain.data.users)
const usersWithoutBirthday = (await routines.getAllUsers(robot))
.filter(user => !user.dateOfBirth)
let formattedArray = []

for (const i in usersWithoutBirthday) {
const user = usersWithoutBirthday[i]
const valid = await routines.doesUserExist(robot, user) && await routines.isUserActive(robot, user)
if (valid) formattedArray.push(user)
}

for (const user of formattedArray) {
for (const user of usersWithoutBirthday) {
robot.adapter.sendDirect({ user: { name: user.name } }, 'Hmm... \nIt looks like you forgot to set the date of birth. \nPlease enter it (DD.MM.YYYY).')
}
const userList = formattedArray.map(user => ` @${user.name} `)
const userList = usersWithoutBirthday.map(user => ` @${user.name} `)
if (userList.length) {
if (userList.length > 1) {
robot.messageRoom(exp.BIRTHDAY_LOGGING_CHANNEL, `There are the users who did not set the date of birth:\n${userList.join('\n')}`)
Expand Down