Add slack notifications to notes for reports (#660)

* Remove extraneous string conversion

* Add slack message for new notes

* Move status and minimize buttons to the bottom

* Add note model spec

* Remove extraneous param for reported links
This commit is contained in:
Andy Zhao 2018-09-25 14:36:54 -04:00 committed by Ben Halpern
parent e72b6f0ab6
commit 5814d0a263
7 changed files with 77 additions and 29 deletions

View file

@ -30,7 +30,7 @@ class FeedbackMessagesController < ApplicationController
def send_slack_message
SlackBot.ping(
generate_message,
channel: feedback_message_params[:feedback_type].to_s,
channel: feedback_message_params[:feedback_type],
username: "#{feedback_message_params[:feedback_type]}_bot",
icon_emoji: ":#{emoji_for_feedback(feedback_message_params[:feedback_type])}:",
)

View file

@ -41,10 +41,14 @@ class Internal::FeedbackMessagesController < Internal::ApplicationController
reason: params["reason"],
)
if note.save
params["author_name"] = note.author.name
params["feedback_message_status"] = note.noteable.status
params["feedback_type"] = note.noteable.feedback_type
send_slack_message(params)
render json: {
outcome: "Success",
content: params["content"],
author_name: note.author.name,
author_name: note.author.name
}
else
render json: { outcome: note.errors.full_messages }
@ -53,6 +57,25 @@ class Internal::FeedbackMessagesController < Internal::ApplicationController
private
def send_slack_message(params)
SlackBot.ping(
generate_message(params),
channel: params["feedback_type"],
username: "new_note_bot",
icon_emoji: ":memo:",
)
end
def generate_message(params)
<<~HEREDOC
*New note from #{params['author_name']}:*
*Report status: #{params['feedback_message_status']}*
Report page: https://dev.to/internal/reports/#{params['noteable_id']}
--------
Message: #{params['content']}
HEREDOC
end
def feedback_message_params
params[:feedback_message].permit(
:id, :status, :reviewer_id,

View file

@ -26,8 +26,9 @@ class PagesController < ApplicationController
end
def report_abuse
reported_url = params[:reported_url] || params[:url] || request.referrer
@feedback_message = FeedbackMessage.new(
reported_url: params[:reported_url] || params[:url] || request.referrer,
reported_url: reported_url&.chomp("?i=i"),
)
render "pages/report-abuse"
end

View file

@ -51,7 +51,7 @@
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading<%= feedback_message.id %>">
<h2 class="panel-title">
<a href="#collapse<%= feedback_message.id %>" role="button" data-toggle="collapse" data-parent="#accordion" aria-expanded="true" aria-controls="collapse<%= feedback_message.id %>">
<a id="collapse__header__link-<%= feedback_message.id %>" href="#collapse<%= feedback_message.id %>" role="button" data-toggle="collapse" data-parent="#accordion" aria-expanded="true" aria-controls="collapse<%= feedback_message.id %>">
Report #<%= feedback_message.id %> - Submitted: <%= time_ago_in_words feedback_message.created_at %> ago
</a>
<div class="panel-right-elements">
@ -87,10 +87,6 @@
<%= feedback_message.message %>
<% end %>
</p>
<h3>
Status: <%= f.select :status, ['Open', 'Invalid', 'Resolved'], {}, id: "status__#{feedback_message.id}" %>
<button class="btn btn-primary" role="button" id="save__status__<%= feedback_message.id %>">SAVE STATUS</button>
</h3>
<hr>
<h3>Previous Emails:</h3>
<div class="previous__emails__container">
@ -150,7 +146,10 @@
<button class="btn btn-primary" type="submit" id="send__email__btn__<%= feedback_message.id %>">SEND EMAIL ✉️ </button>
<div class="email__alert alert fade in" id="email__alert__<%= feedback_message.id %>">
</div>
<h3>
Status: <%= f.select :status, ['Open', 'Invalid', 'Resolved'], {}, id: "status__#{feedback_message.id}" %>
<button class="btn btn-primary" role="button" id="save__status__<%= feedback_message.id %>">SAVE STATUS</button>
</h3>
<h3>Notes:</h3>
<div class="notes__container" id="notes__<%= feedback_message.id %>">
<% feedback_message.notes&.order("created_at").each do |note| %>
@ -177,8 +176,12 @@
required
>
<br>
<button class="btn btn-primary" type="submit" id="note__submit__<%= feedback_message.id %>">SUBMIT NOTE 📝 </button>
<button class="btn btn-primary" type="submit" id="note__submit__<%= feedback_message.id %>">SUBMIT NOTE 📝 </button>
<br>
<br>
<button class="btn btn-primary" type="button" id="minimize__report__button__<%= feedback_message.id %>">
Minimize Report
</button>
</div>
</div>
</div>
@ -354,4 +357,8 @@
var reportId = <%= feedback_message.id %>;
submitNote(reportId);
});
document.getElementById('minimize__report__button__<%= feedback_message.id %>').addEventListener('click', function() {
document.getElementById('collapse__header__link-<%= feedback_message.id %>').click();
});
</script>

8
spec/models/note_spec.rb Normal file
View file

@ -0,0 +1,8 @@
require "rails_helper"
RSpec.describe Note, type: :model do
it { is_expected.to validate_presence_of(:reason) }
it { is_expected.to validate_presence_of(:content) }
it { is_expected.to belong_to(:noteable) }
it { is_expected.to belong_to(:author).class_name("User") }
end

View file

@ -6,7 +6,7 @@ RSpec.describe "feedback_messages", type: :request do
before do
allow_any_instance_of(FeedbackMessagesController).
to receive(:recaptcha_verified?).and_return(true)
allow_any_instance_of(Slack::Notifier).to receive(:ping).and_return(true)
allow(SlackBot).to receive(:ping).and_return(true)
end
# rubocop:enable RSpec/AnyInstance
@ -15,8 +15,8 @@ RSpec.describe "feedback_messages", type: :request do
feedback_type: "abuse-reports",
category: "rude or vulgar",
reported_url: "https://dev.to",
message: "this was vulgar",
},
message: "this was vulgar"
}
}
context "with valid params" do
@ -30,27 +30,37 @@ RSpec.describe "feedback_messages", type: :request do
)
end
xit "redirects to the ticket page" do
expect(response).to redirect_to(FeedbackMessage.last.path)
it "send a Slack message when completed" do
expect(SlackBot).to have_received(:ping)
end
end
context "when a logged in user submits report" do
let(:user) { create(:user) }
let(:mail_message) { instance_double(Mail::Message, deliver: true) }
context "when a logged in user submits a report" do
let(:user) { create(:user) }
before do
allow(NotifyMailer).to receive(:new_report_email).and_return(mail_message)
login_as(user)
post "/feedback_messages", params: valid_abuse_report_params
end
it "adds the logged in user as as the reporter" do
it "adds the logged in user as the reporter" do
expect(FeedbackMessage.find_by(reporter_id: user.id)).not_to eq(nil)
end
xit "sends an email to the reporter" do
expect(NotifyMailer).to have_received(:new_report_email)
it "send a Slack message when completed" do
expect(SlackBot).to have_received(:ping)
end
end
context "when a signed out users submits a report" do
before { post "/feedback_messages", params: valid_abuse_report_params }
it "does not add any user as the reporter" do
expect(FeedbackMessage.last.reporter_id).to eq(nil)
end
it "send a Slack message when completed" do
expect(SlackBot).to have_received(:ping)
end
end
end

View file

@ -7,8 +7,6 @@ RSpec.describe "/internal/reports", type: :request do
let(:user) { create(:user) }
let(:admin) { create(:user, :super_admin) }
describe "GET "
describe "POST /save_status" do
context "when a valid request is made" do
before do
@ -35,13 +33,13 @@ RSpec.describe "/internal/reports", type: :request do
"feedback_message_id" => 1,
"email_subject" => "Thank you for your report",
"email_body" => "Thanks for your report and being an awesome member!",
"email_type" => "reporter",
"email_type" => "reporter"
}
email_message_attributes = {
"feedback_message_id" => 1,
"subject" => "Thank you for your report",
"utm_campaign" => "reporter",
"utm_campaign" => "reporter"
}
before do
@ -68,15 +66,16 @@ RSpec.describe "/internal/reports", type: :request do
note_params = {
"content" => "test note",
"reason" => "abuse-reports",
"noteable_type" => "FeedbackMessage",
"noteable_type" => "FeedbackMessage"
}
json_response = {
outcome: "Success",
content: "test note",
content: "test note"
}
before do
allow(SlackBot).to receive(:ping).and_return(true)
feedback_message
login_as admin
note_params["noteable_id"] = feedback_message.id