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

[WIP] [update] dependencies #115

Open
wants to merge 9 commits into
base: upgrade
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
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ const debug = require('debug')('bot:main')
const packageInfo = require('./package.json')

// Expect a SLACK_TOKEN environment variable
let slackToken = config.SLACK_TOKEN
const slackToken = config.SLACK_TOKEN
if (!slackToken) {
console.error('SLACK_TOKEN is required!')
process.exit(1)
}

let controller = Botkit.slackbot({
const controller = Botkit.slackbot({
storage: storage
})

let bot = controller.spawn({
const bot = controller.spawn({
retry: 10,
token: slackToken
})
Expand Down
2 changes: 1 addition & 1 deletion lib/check-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

function checkConfig (config, requiredVars) {
let missingVars = requiredVars.filter((key) => config[key] === undefined || config[key] === null)
const missingVars = requiredVars.filter((key) => config[key] === undefined || config[key] === null)
if (missingVars.length > 0) {
throw new Error(`Missing configuration variables: ${missingVars.join(', ')}`)
}
Expand Down
12 changes: 6 additions & 6 deletions lib/guests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const _ = require('lodash')

function guests (bot, message) {
debug('begin')
let user = message.user
let getData = Promise.promisify(bot.botkit.storage.users.get)
const user = message.user
const getData = Promise.promisify(bot.botkit.storage.users.get)

return getData(user)
.then((data) => {
Expand All @@ -28,10 +28,10 @@ function guests (bot, message) {
}

function formatOutput (guests) {
let response = ['*Tus invitados:*']
const realGuests = _(guests).filter({result: 'ok'}).map('guest').value()
const alreadyInvited = _(guests).filter({result: 'already_invited'}).map('guest').value()
const alreadyInTeam = _(guests).filter({result: 'already_in_team'}).map('guest').value()
const response = ['*Tus invitados:*']
const realGuests = _(guests).filter({ result: 'ok' }).map('guest').value()
const alreadyInvited = _(guests).filter({ result: 'already_invited' }).map('guest').value()
const alreadyInTeam = _(guests).filter({ result: 'already_in_team' }).map('guest').value()

realGuests.forEach(guest => response.push(` - ${guest}`))

Expand Down
66 changes: 42 additions & 24 deletions lib/invite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ const orgUrl = `https://${teamName}.slack.com/api/users.admin.invite`

function invite (bot, message) {
debug('begin', message.match[1])
let guest = message.match[1]
let host = message.user
let params = { email: guest, token }
let getData = Promise.promisify(bot.botkit.storage.users.get)
let saveData = Promise.promisify(bot.botkit.storage.users.save)
const guest = message.match[1]
const host = message.user
const params = { email: guest, token }
const getData = Promise.promisify(bot.botkit.storage.users.get)
const saveData = Promise.promisify(bot.botkit.storage.users.save)
let userData

return getData(host)
.then((data) => {
.then(data => {
userData = data
return validatePermissions(data)
})
.then(() => request.post(orgUrl).type('form').send(params))
.then(() =>
request
.post(orgUrl)
.type('form')
.send(params)
)
.then(handleResponse)
.then((invitation) => {
.then(invitation => {
debug('complete', invitation)

bot.reply(message, invitation.reply)
Expand All @@ -46,10 +51,12 @@ function invite (bot, message) {
})
.catch(err => {
debug('catch')
let serverError = (err.res && err.res.statusCode !== 200)
const serverError = err.res && err.res.statusCode !== 200

if (serverError) {
err.message = `El servidor respondió de mala gana con estatus ${err.res.statusCode}`
err.message = `El servidor respondió de mala gana con estatus ${
err.res.statusCode
}`
}

if (err.message) {
Expand All @@ -58,36 +65,48 @@ function invite (bot, message) {
}

logError('caught', err)
return bot.reply(message, '😱 ¡Tu invitación no funcionó! Reporta el error en https://github.com/colombia-dev/leonel/issues/new')
return bot.reply(
message,
'😱 ¡Tu invitación no funcionó! Reporta el error en https://github.com/colombia-dev/leonel/issues/new'
)
})
}

function validatePermissions (data) {
const {BOT_ENV} = require('process').env
const stagingBot = (BOT_ENV && BOT_ENV === 'staging')
const isMaintainer = (data && data.is_maintainer === true)
const { BOT_ENV } = require('process').env
const stagingBot = BOT_ENV && BOT_ENV === 'staging'
const isMaintainer = data && data.is_maintainer === true

if (stagingBot && !isMaintainer) {
return Promise.reject(new Error('Solo los maintainers pueden enviar invitaciones con @leonel-test'))
return Promise.reject(
new Error(
'Solo los maintainers pueden enviar invitaciones con @leonel-test'
)
)
}

debug('validatePermissions')
return new Promise((resolve, reject) => {
if (!data) {
return reject(new Error('Creo que mi base de datos tiene un error, podés reportar esto en https://github.com/colombia-dev/leonel/issues/new ?'))
return reject(
new Error(
'Creo que mi base de datos tiene un error, podés reportar esto en https://github.com/colombia-dev/leonel/issues/new ?'
)
)
}

let validAge = moment().subtract(30, 'days')
let accountAge = moment(data.createdAt)
const validAge = moment().subtract(30, 'days')
const accountAge = moment(data.createdAt)

if (!accountAge.isSameOrBefore(validAge)) {
let days = accountAge.diff(validAge, 'days')
let message = `Debes ser miembro :coldev: por ${days} días más pa poder invitar gente.`
const days = accountAge.diff(validAge, 'days')
const message = `Debes ser miembro :coldev: por ${days} días más pa poder invitar gente.`
return reject(new Error(message))
}

if (data.invites <= 0) {
let message = 'Has agotado tus invitaciones mensuales. El 1ro de cada mes repartimos 1 invitación por usuario.'
const message =
'Has agotado tus invitaciones mensuales. El 1ro de cada mes repartimos 1 invitación por usuario.'
return reject(new Error(message))
}

Expand All @@ -99,9 +118,9 @@ function handleResponse (res) {
debug('handleResponse', res.body)
return new Promise((resolve, reject) => {
let reply = '¡Invitación etsitosa!'
let guest = res.request._data.email
let log
const guest = res.request._data.email
let result = 'ok'
const log = { guest, result }

if (res.body.error) {
result = res.body.error
Expand All @@ -114,7 +133,6 @@ function handleResponse (res) {
}
}

log = { guest, result }
resolve({ reply, log })
})
}
Expand Down
10 changes: 5 additions & 5 deletions lib/onboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const config = require('./config')
function onboard (bot, message) {
debug('begin %s', message.user.name, message.user.profile.email)

let introChannel = config.CHANNEL_INTROS
let jobsChannel = config.CHANNEL_JOBS
let user = message.user
let saveData = Promise.promisify(bot.botkit.storage.users.save)
let introText = [
const introChannel = config.CHANNEL_INTROS
const jobsChannel = config.CHANNEL_JOBS
const user = message.user
const saveData = Promise.promisify(bot.botkit.storage.users.save)
const introText = [
`Ole <@${message.user.id}|message.user.name>, que bueno tenerte por estas tierras.`,
'Pa romper el hielo, cuéntanos... ¿Dónde vives y a qué te dedicas?'
].join(' ')
Expand Down
Loading