Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
oma-s committed Apr 10, 2024
1 parent fd8e527 commit 21dd9c4
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/controllers/daily_nerd_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DailyNerdMessagesController < ApplicationController
def create
@daily_nerd_message = authorize DailyNerdMessage.new(daily_nerd_message_attributes.merge(sprint_feedback:))
if @daily_nerd_message.save
@daily_nerd_message.push_to_slack
SlackPostDailyNerdJob.perform_later(daily_nerd_message: @daily_nerd_message)
sprint_feedback.add_daily_nerd_entry(@daily_nerd_message.created_at)
redirect_to sprints_path
else
Expand Down
10 changes: 10 additions & 0 deletions app/jobs/slack_post_daily_nerd_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class SlackPostDailyNerdJob < ApplicationJob
queue_as :notification
sidekiq_options retry: 0

def perform(daily_nerd_message:)
daily_nerd_message.post_to_slack
end
end
2 changes: 1 addition & 1 deletion app/models/daily_nerd_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DailyNerdMessage < ApplicationRecord

validates :message, presence: true

def push_to_slack
def post_to_slack
User::SlackNotification.new(sprint_feedback.user).post_daily_nerd_message(message)
end
end
12 changes: 10 additions & 2 deletions app/models/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def notify(channel:, text:)
request http_method: :post, slack_method: "chat.postMessage", body: {channel:, text:}.to_json
end

def push_personalized_message_to_daily_nerd_channel(body:)
def post_personalized_message_to_daily_nerd_channel(user:, message:)
# This is the old way of posting to slack on behalf of a user using a webhook.
# https://api.slack.com/legacy/custom-integrations/messaging/webhooks
# It might be necessary to change this in the future to use the new Slack API.
request_hook url: Config.slack_webhook_url!, body: body.to_json
request_hook url: Config.slack_webhook_url!, body: personalized_webhook_body(user:, message:).to_json
end

def retrieve_users_slack_id_by_email(email)
Expand Down Expand Up @@ -56,4 +56,12 @@ def request_hook(url:, body:)

response
end

def personalized_webhook_body(user:, message:)
{
username: user.display_name,
icon_url: user.slack_profile.image_url,
text: message
}
end
end
8 changes: 1 addition & 7 deletions app/models/user/slack_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ def send_message(message)
end

def post_daily_nerd_message(message)
body = {
username: user.display_name,
icon_url: user.slack_profile.image_url,
text: message
}

Slack.instance.push_personalized_message_to_daily_nerd_channel(body:)
Slack.instance.post_personalized_message_to_daily_nerd_channel(user:, message:)
end

private
Expand Down
4 changes: 2 additions & 2 deletions spec/models/daily_nerd_message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
let(:user) { users(:john) }
let(:daily_nerd_message) { user.sprint_feedbacks.take.daily_nerd_messages.create(message: "I'm a daily nerd") }

describe "#push_to_slack" do
describe "#post_to_slack" do
it "posts the daily nerd message to Slack" do
slack_notification = instance_double("User::SlackNotification")
allow(User::SlackNotification).to receive(:new).with(user).and_return(slack_notification)
expect(slack_notification).to receive(:post_daily_nerd_message).with(daily_nerd_message.message)

daily_nerd_message.push_to_slack
daily_nerd_message.post_to_slack
end
end
end
2 changes: 1 addition & 1 deletion spec/system/daily_nerd_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

before do
allow(Slack.instance).to receive(:retrieve_users_profile_image_url_by_email).and_return("https://example.com/image.jpg")
allow(Slack.instance).to receive(:push_personalized_message_to_daily_nerd_channel)
allow(Slack.instance).to receive(:post_personalized_message_to_daily_nerd_channel)
end

def login_and_create_daily_nerd_message
Expand Down

0 comments on commit 21dd9c4

Please sign in to comment.