Skip to content

Commit

Permalink
Email for Appeal
Browse files Browse the repository at this point in the history
  • Loading branch information
ncounter committed Dec 20, 2023
1 parent ae35e3f commit 0be73f3
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/api/app/models/appeal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def create_event
end

def event_parameters
{ id: id, appellant_id: appellant.id, decision_id: decision.id, reason: reason }
{ id: id, appellant_id: appellant.id, decision_id: decision.id, reason: reason,
report_last_id: decision.reports.last.id, reportable_type: decision.reports.first.reportable.class.name }
end
end

Expand Down
7 changes: 6 additions & 1 deletion src/api/app/models/event/appeal_created.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ class AppealCreated < Base

self.description = 'A user has appealed the decision of a moderator'

payload_keys :id, :appellant_id, :decision_id, :reason
payload_keys :id, :appellant_id, :decision_id, :reason, :report_last_id, :reportable_type

def subject
appeal = Appeal.find(payload['id'])
"Appeal to #{appeal.decision.reports.first.reportable&.class&.name} decision".squish
end
end
end

Expand Down
4 changes: 1 addition & 3 deletions src/api/app/models/event_subscription/for_channel_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class ForChannelForm
'Event::ReportForRequest',
'Event::WorkflowRunFail', 'Event::AppealCreated',
'Event::FavoredDecision', 'Event::ClearedDecision'].freeze
DISABLE_EMAIL_FOR_EVENTS = ['Event::AppealCreated'].freeze

attr_reader :name, :subscription

Expand All @@ -25,8 +24,7 @@ def subscription_params(index)

def disabled_checkbox?
(DISABLE_FOR_EVENTS.include?(@event.to_s) && (name == 'web' || name == 'rss')) ||
(DISABLE_RSS_FOR_EVENTS.include?(@event.to_s) && name == 'rss') ||
(DISABLE_EMAIL_FOR_EVENTS.include?(@event.to_s) && name == 'instant_email')
(DISABLE_RSS_FOR_EVENTS.include?(@event.to_s) && name == 'rss')

Check warning on line 27 in src/api/app/models/event_subscription/for_channel_form.rb

View check run for this annotation

Codecov / codecov/patch

src/api/app/models/event_subscription/for_channel_form.rb#L27

Added line #L27 was not covered by tests
end

private
Expand Down
7 changes: 7 additions & 0 deletions src/api/app/views/event_mailer/appeal_created.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%p
- appellant = User.find(event['appellant_id'])
- decision = Decision.find(event['decision_id'])
'#{appellant}' decided to appeal to the decision '#{decision.reason}'. This is the reason:
= event['reason']
%p
= link_to_reportables(report_id: event['report_last_id'], reportable_type: event['reportable_type'], host: @host)
6 changes: 6 additions & 0 deletions src/api/app/views/event_mailer/appeal_created.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%- appellant = User.find(event['appellant_id']) %>
<%- decision = Decision.find(event['decision_id']) %>
'<%= appellant %>' decided to appeal to the decision '<%= decision.reason %>'. This is the reason:
<%= event['reason'] %>
<%= link_to_reportables(report_id: event['report_last_id'], reportable_type: event['reportable_type'], host: @host) %>
8 changes: 8 additions & 0 deletions src/api/spec/factories/event_subscriptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@
group { nil }
end

factory :event_subscription_appeal_created do
eventtype { 'Event::AppealCreated' }
receiver_role { 'moderator' }
channel { :instant_email }
user
group { nil }
end

factory :event_subscription_workflow_run_fail do
eventtype { 'Event::WorkflowRunFail' }
receiver_role { 'token_executor' }
Expand Down
44 changes: 44 additions & 0 deletions src/api/spec/mailers/event_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,49 @@
expect(mail.body.encoded).to include("<a href=\"https://build.example.com/project/show/#{project}#comments-list\">#{project}</a>")
end
end

context 'for an event of type Event::AppealCreated' do
let(:moderator) { create(:admin_user) }
let(:offender) { create(:confirmed_user, login: 'offender') } # user who wrote the offensive comment
let(:reporter) { create(:confirmed_user, login: 'reporter') }
let(:appellant) { create(:confirmed_user, login: 'appellant') }

let(:comment) { create(:comment_project, user: offender) }
let(:project) { comment.commentable }
let(:report) { create(:report, user: reporter, reportable: comment) }

let!(:moderator_subscription) { create(:event_subscription_appeal_created, user: moderator) }

let(:decision) { create(:decision, :favor, moderator: moderator, reason: 'This is spam for sure.', reports: [report]) }
let(:appeal) { create(:appeal, appellant: appellant, decision: decision, reason: 'I strongly disagree!') }
let(:event) { Event::AppealCreated.last }
let(:mail) { EventMailer.with(subscribers: event.subscribers, event: event).notification_email.deliver_now }

before do
login(moderator)
appeal
end

it 'gets delivered' do
expect(ActionMailer::Base.deliveries).to include(mail)
end

it 'is sent to the moderator' do
expect(mail.to).to contain_exactly(decision.moderator.email)
end

it 'has a subject' do
expect(mail.subject).to eq("Appeal to #{decision.reports.first.reportable&.class&.name} decision")
end

it 'contains the correct text' do
expect(mail.body.encoded).to include("'#{appellant}' decided to appeal to the decision '#{decision.reason}'. This is the reason:")
expect(mail.body.encoded).to include('I strongly disagree!')
end

it 'renders link to the page of the comment' do
expect(mail.body.encoded).to include("<a href=\"https://build.example.com/project/show/#{project}#comments-list\">#{project}</a>")
end
end
end
end

0 comments on commit 0be73f3

Please sign in to comment.