From b571fa5b460d21695ffe1e92d8d417505abbfd85 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:27:00 -0500 Subject: [PATCH 01/16] Allow permissioned ambassadors to add leaders to clubs --- src/interactions/leaderAdd.js | 2 +- src/utils/index.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 46ef523d..d25274e4 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!commandUser.club) { + if (!(commandUser.club && commandUser.ambassador)) { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return diff --git a/src/utils/index.js b/src/utils/index.js index 694ae1e9..b71ab9a9 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -42,7 +42,7 @@ export const airPatch = (baseName, recordID, values, options = {}) => } console.log( `Airtable updated my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -69,7 +69,7 @@ export const airCreate = (baseName, fields, options = {}) => } console.log( `Airtable saved my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -134,7 +134,7 @@ export const airGet = ( } console.log( `AirTable got back to me from my question at ${timestamp} with ${ - data.length + data.length } records. The query took ${Date.now() - timestamp}ms` ) resolve(data) @@ -251,6 +251,15 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), + airFind('Ambassadors', 'Slack ID', user).then( + () => { + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador + } + ) + } + ) ]) .then(async () => { if (!results.person && results.slackUser) { @@ -316,7 +325,7 @@ export const getInfoForUser = user => .then(() => { console.log( `Finished pulling up the info about user '${user}' from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(results) }) @@ -504,7 +513,7 @@ export const transcript = (search, vars) => { return evalTranscript(recurseTranscript(searchArr, transcriptObj), vars) } const evalTranscript = (target, vars = {}) => - function() { + function () { return eval('`' + target + '`') }.call({ ...vars, From 316985377cd1e3dc0184efd7fe47f62445653e31 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:35:32 -0500 Subject: [PATCH 02/16] Fix name issue --- src/interactions/leaderAdd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index d25274e4..8e687a1a 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!(commandUser.club && commandUser.ambassador)) { + if (!(commandUser.club && commandUser.permissionedAmbassador)) { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return From 871ed13579140b381d5ba362f31cd5fb193873ba Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:37:51 -0500 Subject: [PATCH 03/16] Simplify --- src/utils/index.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index b71ab9a9..ec9ecbc5 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,13 +251,9 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airFind('Ambassadors', 'Slack ID', user).then( - () => { - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador - } - ) + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador } ) ]) From 2b88cd955d17fb0ec2f0dd8a58cc9d171aa2f5ab Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:40:14 -0500 Subject: [PATCH 04/16] Make it work actually --- src/interactions/leaderAdd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 8e687a1a..f68aaf7e 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!(commandUser.club && commandUser.permissionedAmbassador)) { + if (!commandUser.club && commandUser.permissionedAmbassador === "false") { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return From 2168e68b5efacb3158ad6865e0b75d5c13d7c502 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:41:59 -0500 Subject: [PATCH 05/16] Add missing parenthesis --- src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.js b/src/utils/index.js index ec9ecbc5..6b1d54f2 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,7 +251,7 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( permissionedAmbassador => { results.permissionedAmbassador = permissionedAmbassador } From a6970bfceb41911424e7c05bf400cad2381a6351 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 22:00:30 -0500 Subject: [PATCH 06/16] Filter to record of user first --- src/utils/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 6b1d54f2..3cc7b258 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,9 +251,13 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador + airGet('Ambassadors', `FIND('${user}', {Slack ID})`).then( + () => { + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador + } + ) } ) ]) From a576a4cd5cb308a9a9d08871b2dec27359b4dcec Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 22:01:42 -0500 Subject: [PATCH 07/16] Simplify filtering with airFind? Not toally sure how this works, but I think this should do the same thing --- src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.js b/src/utils/index.js index 3cc7b258..a0c78819 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,7 +251,7 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', `FIND('${user}', {Slack ID})`).then( + airFind('Ambassadors', 'Slack ID', user).then( () => { airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( permissionedAmbassador => { From 464191e911d679c6c8e21632cb10e6fd428b1407 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Mon, 13 Jan 2020 19:32:53 -0800 Subject: [PATCH 08/16] Rewrite leader add to async/await --- src/interactions/leaderAdd.js | 115 ++++++++-------------------------- src/utils/index.js | 23 +++---- src/utils/transcript.yml | 5 +- 3 files changed, 39 insertions(+), 104 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index f68aaf7e..1ca85ca6 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -1,101 +1,40 @@ -import { getInfoForUser, airCreate, airPatch, transcript } from '../utils' +import { getInfoForUser, airPatch, transcript, airFind } from '../utils' -const interactionLeaderAdd = (bot, message) => { +const interactionLeaderAdd = async (bot, message) => { const { user, text, channel } = message - if (text === '' || text === 'help') { + const taggedUserID = (text.match(/\<@(.*)\|/) || [])[1] + if (!taggedUserID) { bot.replyPrivateDelayed(message, transcript('leaderAdd.help')) - return } - return getInfoForUser(user) - .then(commandUser => { - if (!commandUser.leader) { - console.log( - `${commandUser.user} isn't a leader, so I told them this was restricted` - ) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidUser')) - return - } + const taggedUser = await getInfoForUser(taggedUserID) + const commandUser = await getInfoForUser(user) + const recipientClub = await airFind('Clubs', 'Slack Channel ID', channel) - if (!commandUser.club && commandUser.permissionedAmbassador === "false") { - console.log(`${commandUser.user} doesn't have a club`) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) - return - } + if (!commandUser.club && !commandUser.permissionedAmbassador) { + throw transcript('leaderAdd.invalidClub') + } + + if (commandUser.club && commandUser.club.id != recipientClub.id) { + // A leader is trying to permission someone to a channel that's not their + // club channel + throw transcript('leaderAdd.invalidChannel') + } - if (commandUser.club.fields['Slack Channel ID'] != channel) { - console.log(`${user} doesn't own channel ${channel}`) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidChannel')) - return - } + const taggedUserClubs = taggedUser.fields['Clubs'] || [] + if (taggedUserClubs.includes(recipientClub)) { + throw transcript('leaderAdd.alreadyLeader') + } - const taggedUserID = (message.text.match(/\<@(.*)\|/) || [])[1] - if (!taggedUserID) { - throw new Error('Invalid Slack user') - } + await airPatch('People', taggedUser.id, { + Clubs: [...taggedUserClubs, recipientClub], + }) - return getInfoForUser(taggedUserID) - .then(taggedUser => { - console.log('found tagged user') - if (taggedUser.slackUser.is_bot) { - throw new Error('bots cannot be club leaders') - } - if (!taggedUser.person) { - // if user doesn't exist in Airtable - const profile = taggedUser.slackUser.profile - const fields = { - Email: taggedUser.slackUser.profile.email, - 'Slack ID': taggedUser.slackUser.id, - 'Full Name': profile.real_name || profile.display_name, - } - console.log(fields) - return airCreate('Person', fields) - .then(taggedPerson => { - return taggedPerson - }) - .catch(err => { - console.error( - 'Ran into issue creating new Person record in Airtable' - ) - throw err - }) - } else { - return taggedUser.person - } - }) - .then(taggedPerson => { - // ensure we can assign the person as a leader to this club - const clubs = taggedPerson.fields['Clubs'] || [] - if (clubs.includes(commandUser.club.id)) { - bot.replyPrivateDelayed( - message, - transcript('leaderAdd.alreadyLeader') - ) - return - } - clubs.push(commandUser.club.id) - return airPatch('People', taggedPerson.id, { - Clubs: clubs, - }) - .then(() => { - bot.replyPrivateDelayed( - message, - transcript('leaderAdd.success', { taggedUserID, channel }) - ) - }) - .catch(err => { - throw err - }) - }) - .catch(err => { - throw err - }) - }) - .catch(err => { - console.error(err) - bot.replyPrivateDelayed(message, transcript('errors.general', { err })) - }) + bot.replyPrivateDelayed( + message, + transcript('leaderAdd.success', { taggedUserID, channel }) + ) } export default interactionLeaderAdd diff --git a/src/utils/index.js b/src/utils/index.js index a0c78819..430bca44 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -42,7 +42,7 @@ export const airPatch = (baseName, recordID, values, options = {}) => } console.log( `Airtable updated my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -69,7 +69,7 @@ export const airCreate = (baseName, fields, options = {}) => } console.log( `Airtable saved my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -134,7 +134,7 @@ export const airGet = ( } console.log( `AirTable got back to me from my question at ${timestamp} with ${ - data.length + data.length } records. The query took ${Date.now() - timestamp}ms` ) resolve(data) @@ -251,15 +251,12 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airFind('Ambassadors', 'Slack ID', user).then( - () => { - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador - } - ) + airFind('Ambassadors', 'Slack ID', user).then(ambassador => { + results.ambassador = ambassador + if (ambassador) { + results.permissionedAmbassador = ambassador.fields['Permissioned'] } - ) + }), ]) .then(async () => { if (!results.person && results.slackUser) { @@ -325,7 +322,7 @@ export const getInfoForUser = user => .then(() => { console.log( `Finished pulling up the info about user '${user}' from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(results) }) @@ -513,7 +510,7 @@ export const transcript = (search, vars) => { return evalTranscript(recurseTranscript(searchArr, transcriptObj), vars) } const evalTranscript = (target, vars = {}) => - function () { + function() { return eval('`' + target + '`') }.call({ ...vars, diff --git a/src/utils/transcript.yml b/src/utils/transcript.yml index e81ed2d3..eae54bef 100644 --- a/src/utils/transcript.yml +++ b/src/utils/transcript.yml @@ -338,11 +338,10 @@ leaderAdd: Registers a new leader to your club. Ex. \`/leader-add @orpheus\` _Needs to be run by a club leader in their own club channel_ Additionally, you can see a list of who is already a leader with \`/leader-list\` - invalidUser: Only club leaders can use this command. - invalidClub: I couldn't find your club. + invalidAuth: I couldn't find your club. Only registered leaders or operations team members can run this command. invalidChannel: This isn't your club channel. Please run this in a channel you lead. - success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>. alreadyLeader: They're already a leader in your club! + success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>. leaderList: invalidUser: Only club leaders can use this command. invalidClub: I couldn't find your club. From 18cd324ffcb6a54dd01d4d7628e3002328f5c14c Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:27:00 -0500 Subject: [PATCH 09/16] Allow permissioned ambassadors to add leaders to clubs --- src/interactions/leaderAdd.js | 2 +- src/utils/index.js | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 46ef523d..d25274e4 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!commandUser.club) { + if (!(commandUser.club && commandUser.ambassador)) { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return diff --git a/src/utils/index.js b/src/utils/index.js index 694ae1e9..b71ab9a9 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -42,7 +42,7 @@ export const airPatch = (baseName, recordID, values, options = {}) => } console.log( `Airtable updated my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -69,7 +69,7 @@ export const airCreate = (baseName, fields, options = {}) => } console.log( `Airtable saved my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -134,7 +134,7 @@ export const airGet = ( } console.log( `AirTable got back to me from my question at ${timestamp} with ${ - data.length + data.length } records. The query took ${Date.now() - timestamp}ms` ) resolve(data) @@ -251,6 +251,15 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), + airFind('Ambassadors', 'Slack ID', user).then( + () => { + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador + } + ) + } + ) ]) .then(async () => { if (!results.person && results.slackUser) { @@ -316,7 +325,7 @@ export const getInfoForUser = user => .then(() => { console.log( `Finished pulling up the info about user '${user}' from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(results) }) @@ -504,7 +513,7 @@ export const transcript = (search, vars) => { return evalTranscript(recurseTranscript(searchArr, transcriptObj), vars) } const evalTranscript = (target, vars = {}) => - function() { + function () { return eval('`' + target + '`') }.call({ ...vars, From b8ee75fc98cb639014c53a8c038b40814c30f844 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:35:32 -0500 Subject: [PATCH 10/16] Fix name issue --- src/interactions/leaderAdd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index d25274e4..8e687a1a 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!(commandUser.club && commandUser.ambassador)) { + if (!(commandUser.club && commandUser.permissionedAmbassador)) { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return From e593a6cc6c09fa7c0d823cc7ea10c8432cb662d6 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:37:51 -0500 Subject: [PATCH 11/16] Simplify --- src/utils/index.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index b71ab9a9..ec9ecbc5 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,13 +251,9 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airFind('Ambassadors', 'Slack ID', user).then( - () => { - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador - } - ) + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador } ) ]) From b33038dab197b70551285de9b63d59a3661a4ecd Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:40:14 -0500 Subject: [PATCH 12/16] Make it work actually --- src/interactions/leaderAdd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 8e687a1a..f68aaf7e 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -18,7 +18,7 @@ const interactionLeaderAdd = (bot, message) => { return } - if (!(commandUser.club && commandUser.permissionedAmbassador)) { + if (!commandUser.club && commandUser.permissionedAmbassador === "false") { console.log(`${commandUser.user} doesn't have a club`) bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) return From 9b837fef12f7ccbb00afed74a99f34fbaffd7067 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 21:41:59 -0500 Subject: [PATCH 13/16] Add missing parenthesis --- src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.js b/src/utils/index.js index ec9ecbc5..6b1d54f2 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,7 +251,7 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false"').then( + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( permissionedAmbassador => { results.permissionedAmbassador = permissionedAmbassador } From 92f7b7fec18363b8498e4c21930e6b6dbf195863 Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 22:00:30 -0500 Subject: [PATCH 14/16] Filter to record of user first --- src/utils/index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 6b1d54f2..3cc7b258 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,9 +251,13 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador + airGet('Ambassadors', `FIND('${user}', {Slack ID})`).then( + () => { + airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( + permissionedAmbassador => { + results.permissionedAmbassador = permissionedAmbassador + } + ) } ) ]) From 942d28d79557b7ebfe435c76b10a78d9310b725a Mon Sep 17 00:00:00 2001 From: Matthew Stanciu Date: Sat, 11 Jan 2020 22:01:42 -0500 Subject: [PATCH 15/16] Simplify filtering with airFind? Not toally sure how this works, but I think this should do the same thing --- src/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/index.js b/src/utils/index.js index 3cc7b258..a0c78819 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -251,7 +251,7 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airGet('Ambassadors', `FIND('${user}', {Slack ID})`).then( + airFind('Ambassadors', 'Slack ID', user).then( () => { airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( permissionedAmbassador => { From d4a474ec63c24153347a2723a4a8dd0a8405ada3 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Mon, 13 Jan 2020 19:32:53 -0800 Subject: [PATCH 16/16] Rewrite leader add to async/await --- src/interactions/leaderAdd.js | 115 ++++++++-------------------------- src/utils/index.js | 23 +++---- src/utils/transcript.yml | 5 +- 3 files changed, 39 insertions(+), 104 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index f68aaf7e..1ca85ca6 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -1,101 +1,40 @@ -import { getInfoForUser, airCreate, airPatch, transcript } from '../utils' +import { getInfoForUser, airPatch, transcript, airFind } from '../utils' -const interactionLeaderAdd = (bot, message) => { +const interactionLeaderAdd = async (bot, message) => { const { user, text, channel } = message - if (text === '' || text === 'help') { + const taggedUserID = (text.match(/\<@(.*)\|/) || [])[1] + if (!taggedUserID) { bot.replyPrivateDelayed(message, transcript('leaderAdd.help')) - return } - return getInfoForUser(user) - .then(commandUser => { - if (!commandUser.leader) { - console.log( - `${commandUser.user} isn't a leader, so I told them this was restricted` - ) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidUser')) - return - } + const taggedUser = await getInfoForUser(taggedUserID) + const commandUser = await getInfoForUser(user) + const recipientClub = await airFind('Clubs', 'Slack Channel ID', channel) - if (!commandUser.club && commandUser.permissionedAmbassador === "false") { - console.log(`${commandUser.user} doesn't have a club`) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidClub')) - return - } + if (!commandUser.club && !commandUser.permissionedAmbassador) { + throw transcript('leaderAdd.invalidClub') + } + + if (commandUser.club && commandUser.club.id != recipientClub.id) { + // A leader is trying to permission someone to a channel that's not their + // club channel + throw transcript('leaderAdd.invalidChannel') + } - if (commandUser.club.fields['Slack Channel ID'] != channel) { - console.log(`${user} doesn't own channel ${channel}`) - bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidChannel')) - return - } + const taggedUserClubs = taggedUser.fields['Clubs'] || [] + if (taggedUserClubs.includes(recipientClub)) { + throw transcript('leaderAdd.alreadyLeader') + } - const taggedUserID = (message.text.match(/\<@(.*)\|/) || [])[1] - if (!taggedUserID) { - throw new Error('Invalid Slack user') - } + await airPatch('People', taggedUser.id, { + Clubs: [...taggedUserClubs, recipientClub], + }) - return getInfoForUser(taggedUserID) - .then(taggedUser => { - console.log('found tagged user') - if (taggedUser.slackUser.is_bot) { - throw new Error('bots cannot be club leaders') - } - if (!taggedUser.person) { - // if user doesn't exist in Airtable - const profile = taggedUser.slackUser.profile - const fields = { - Email: taggedUser.slackUser.profile.email, - 'Slack ID': taggedUser.slackUser.id, - 'Full Name': profile.real_name || profile.display_name, - } - console.log(fields) - return airCreate('Person', fields) - .then(taggedPerson => { - return taggedPerson - }) - .catch(err => { - console.error( - 'Ran into issue creating new Person record in Airtable' - ) - throw err - }) - } else { - return taggedUser.person - } - }) - .then(taggedPerson => { - // ensure we can assign the person as a leader to this club - const clubs = taggedPerson.fields['Clubs'] || [] - if (clubs.includes(commandUser.club.id)) { - bot.replyPrivateDelayed( - message, - transcript('leaderAdd.alreadyLeader') - ) - return - } - clubs.push(commandUser.club.id) - return airPatch('People', taggedPerson.id, { - Clubs: clubs, - }) - .then(() => { - bot.replyPrivateDelayed( - message, - transcript('leaderAdd.success', { taggedUserID, channel }) - ) - }) - .catch(err => { - throw err - }) - }) - .catch(err => { - throw err - }) - }) - .catch(err => { - console.error(err) - bot.replyPrivateDelayed(message, transcript('errors.general', { err })) - }) + bot.replyPrivateDelayed( + message, + transcript('leaderAdd.success', { taggedUserID, channel }) + ) } export default interactionLeaderAdd diff --git a/src/utils/index.js b/src/utils/index.js index a0c78819..430bca44 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -42,7 +42,7 @@ export const airPatch = (baseName, recordID, values, options = {}) => } console.log( `Airtable updated my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -69,7 +69,7 @@ export const airCreate = (baseName, fields, options = {}) => } console.log( `Airtable saved my ${baseName} record from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(record) }) @@ -134,7 +134,7 @@ export const airGet = ( } console.log( `AirTable got back to me from my question at ${timestamp} with ${ - data.length + data.length } records. The query took ${Date.now() - timestamp}ms` ) resolve(data) @@ -251,15 +251,12 @@ export const getInfoForUser = user => airFind('People', 'Slack ID', user).then( person => (results.person = person) ), - airFind('Ambassadors', 'Slack ID', user).then( - () => { - airGet('Ambassadors', 'IF(Permissioned = 1, "true", "false")').then( - permissionedAmbassador => { - results.permissionedAmbassador = permissionedAmbassador - } - ) + airFind('Ambassadors', 'Slack ID', user).then(ambassador => { + results.ambassador = ambassador + if (ambassador) { + results.permissionedAmbassador = ambassador.fields['Permissioned'] } - ) + }), ]) .then(async () => { if (!results.person && results.slackUser) { @@ -325,7 +322,7 @@ export const getInfoForUser = user => .then(() => { console.log( `Finished pulling up the info about user '${user}' from ${timestamp} in ${Date.now() - - timestamp}ms` + timestamp}ms` ) resolve(results) }) @@ -513,7 +510,7 @@ export const transcript = (search, vars) => { return evalTranscript(recurseTranscript(searchArr, transcriptObj), vars) } const evalTranscript = (target, vars = {}) => - function () { + function() { return eval('`' + target + '`') }.call({ ...vars, diff --git a/src/utils/transcript.yml b/src/utils/transcript.yml index 83f810ff..1570fb54 100644 --- a/src/utils/transcript.yml +++ b/src/utils/transcript.yml @@ -338,11 +338,10 @@ leaderAdd: Registers a new leader to your club. Ex. \`/leader-add @orpheus\` _Needs to be run by a club leader in their own club channel_ Additionally, you can see a list of who is already a leader with \`/leader-list\` - invalidUser: Only club leaders can use this command. - invalidClub: I couldn't find your club. + invalidAuth: I couldn't find your club. Only registered leaders or operations team members can run this command. invalidChannel: This isn't your club channel. Please run this in a channel you lead. - success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>. alreadyLeader: They're already a leader in your club! + success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>. leaderList: invalidUser: Only club leaders can use this command. invalidClub: I couldn't find your club.