Skip to content

Commit

Permalink
Merge pull request OperationCode#447 from OperationCode/password_change
Browse files Browse the repository at this point in the history
Add slack invites
  • Loading branch information
apex-omontgomery authored Nov 10, 2018
2 parents a2ad9a3 + 6cfebb6 commit 138feda
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/jobs/slack_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SlackJobs < ApplicationJob
def slack_client
@slack_client ||= Slack::Client.new(
subdomain: ENV.fetch('SLACK_SUBDOMAIN'),
token: ENV.fetch('SLACK_TOKEN')
token: ENV.fetch('SLACK_LEGACY_ADMIN_TOKEN') # admin token required to invite
)
end
end
14 changes: 12 additions & 2 deletions app/jobs/slack_jobs/inviter_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ class SlackJobs
class InviterJob < SlackJobs
include Sidekiq::Worker

def perform(_email)
logger.debug 'Deprecated pathway, trying to determine what is placing this on queue.'
def perform(email)
# Invites user to general channel
Raven.user_context email: email
Raven.tags_context slack_invite: 'yes'
slack_client.invite(
email: email,
channels: ['C03GSNF77'],
extra_message: 'Welcome to Operation Code!\n'\
'Once you log in, please take a moment to introduce yourself in the #general channel - a bit about who you are, where you\'re coming from, and where you\'re interested in going next.\n'\
'Thank you for joining us!\n'\
'-from the OC volunteer team\n'\
)
end
end
end
3 changes: 2 additions & 1 deletion app/lib/slack/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(subdomain:, token:)
@domain = "#{subdomain}.slack.com"
end

def invite(email:, channels: [])
def invite(extra_message:, email:, channels: [])
Rails.logger.info "Inviting user with email '#{email}'"
body = send_api_request(
to: INVITE_PATH,
Expand All @@ -29,6 +29,7 @@ def invite(email:, channels: [])
channels: channels.join(','),
token: @token,
set_active: 'true',
extra_message: extra_message,
_attempts: 1
}
)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/slack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def client
def set_client
Slack::Client.new(
subdomain: OperationCode.fetch_secret_with(name: :slack_domain),
token: OperationCode.fetch_secret_with(name: :slack_token)
token: OperationCode.fetch_secret_with(name: :slack_legacy_admin_token)
)
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def name

def welcome_user
add_to_send_grid
invite_to_slack
end

def invite_to_slack
SlackJobs::InviterJob.perform_async(self.email)
end

def add_to_send_grid
Expand Down
7 changes: 7 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def teardown
assert_equal [user.id], AddUserToSendGridJob.jobs.first['args']
end

test 'welcoming a user adds them to SlackInvite' do
user = create(:user, user_opts)
user.welcome_user
assert_equal 1, SlackJobs::InviterJob.jobs.length
assert_equal [user.email], SlackJobs::InviterJob.jobs.first['args']
end

test 'must have a valid email' do
refute User.new(email: 'bogusemail', password: 'password', zip: '97201').valid?
assert User.new(email: '[email protected]', password: 'password', zip: '97201').valid?
Expand Down

0 comments on commit 138feda

Please sign in to comment.