Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,29 @@ def filter_notification_recipient(users_scope, activity:, time: nil)
def generate_commands(recipient:, activity:, time: nil)
notification = activity.item

data = Rails.cache.fetch("campaigns/inappropriate_content_flagged/#{notification.inappropriate_content_flag_id}", expires_in: 5.minutes) do
Rails.cache.fetch("campaigns/inappropriate_content_flagged/#{notification.inappropriate_content_flag_id}", expires_in: 5.minutes) do
flag = notification.inappropriate_content_flag
flaggable = flag.flaggable

{
flaggable_author: flaggable.author,
payload = {
flaggable_author_name: UserDisplayNameService.new(AppConfiguration.instance, recipient).display_name!(flaggable.author),
flaggable_type: flag.flaggable_type,
flag_automatically_detected: flag.automatically_detected?
flag_automatically_detected: flag.automatically_detected?,
flaggable_url: Frontend::UrlService.new.model_to_url(flaggable, locale: Locale.new(recipient.locale))
}
end

payload = {
flaggable_type: data[:flaggable_type],
flag_automatically_detected: data[:flag_automatically_detected],
flaggable_author_name: UserDisplayNameService.new(AppConfiguration.instance, recipient).display_name!(data[:flaggable_author]),
flaggable_url: Frontend::UrlService.new.model_to_url(data[:flaggable], locale: Locale.new(recipient.locale))
}

case data[:flaggable_type]
when Idea.name
payload[:flaggable_title_multiloc] = data[:flaggable].title_multiloc
payload[:flaggable_body_multiloc] = data[:flaggable].body_multiloc
when Comment.name
payload[:flaggable_body_multiloc] = data[:flaggable].body_multiloc
else
raise "Unsupported flaggable type: #{data[:flaggable_type]}"
case flag.flaggable_type
when Idea.name
payload[:flaggable_title_multiloc] = flaggable.title_multiloc
payload[:flaggable_body_multiloc] = flaggable.body_multiloc
when Comment.name
payload[:flaggable_body_multiloc] = flaggable.body_multiloc
else
raise "Unsupported flaggable type: #{flag.flaggable_type}"
end

[{ event_payload: payload }]
end

[{ event_payload: payload }]
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@
end
end

describe '#generate_commands' do
let(:author) { create(:user, first_name: 'Biggus', last_name: 'Dickus') }
let(:idea) { create(:idea, title_multiloc: { 'en' => 'Flagged idea' }, body_multiloc: { 'en' => 'This is a flagged idea.' }, author:, slug: 'flagged-idea') }
let(:flag) { create(:inappropriate_content_flag, flaggable: idea) }
let(:campaign) { create(:inappropriate_content_flagged_campaign) }
let(:notification) { create(:inappropriate_content_flagged, inappropriate_content_flag: flag) }
let(:notification_activity) { create(:activity, item: notification, action: 'created') }

it 'generates a command with the desired payload and tracked content' do
command = campaign.generate_commands(
recipient: notification_activity.item.recipient,
activity: notification_activity
).first

expect(command).to match(
event_payload: a_hash_including(
flaggable_author_name: 'Biggus Dickus',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

flaggable_type: 'Idea',
flag_automatically_detected: true,
flaggable_url: 'http://example.org/en/ideas/flagged-idea',
flaggable_title_multiloc: { 'en' => 'Flagged idea' },
flaggable_body_multiloc: { 'en' => 'This is a flagged idea.' }
)
)
end
end

describe 'reason_code' do
it 'is inappropriate when toxicity was detected' do
flag = create(:inappropriate_content_flag, toxicity_label: 'insult')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,11 @@ def filter_recipient(users_scope, activity:, time: nil)
comment = activity.item
initiator = comment.author

recipient_ids = []
unless initiator&.admin?
recipients = User.admin
if !initiator.project_moderator?(comment.idea.project_id)
recipient_ids = recipients.or(User.project_moderator(comment.idea.project_id)).ids
end
if initiator && UserRoleService.new.can_moderate?(comment, initiator)
return users_scope.none
end

users_scope.where(id: recipient_ids)
UserRoleService.new.moderators_for(comment, users_scope)
end

def self.recipient_role_multiloc_key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,62 @@
end
end

describe '#generate_commands' do
let(:recipient) { create(:user) }
let(:author) { create(:user, first_name: 'Rea', last_name: 'Xion') }
let(:comment) { create(:comment, body_multiloc: { 'en' => 'Example comment.' }, author:) }
let(:campaign) { create(:new_comment_for_admin_campaign) }
let(:activity) { create(:activity, item: comment, action: 'created') }

it 'generates a command with the desired payload and tracked content' do
command = campaign.generate_commands(recipient:, activity:).first

expect(command).to match(
event_payload: a_hash_including(
initiating_user_first_name: 'Rea',
initiating_user_last_name: 'Xion',
comment_author_name: 'Rea Xion',
comment_body_multiloc: { 'en' => 'Example comment.' },
comment_url: "http://example.org/en/ideas/#{comment.idea.slug}",
idea_published_at: comment.idea.published_at.iso8601,
idea_title_multiloc: comment.idea.title_multiloc,
idea_author_name: comment.idea.author_name
)
)
end
end

describe 'filter_recipient' do
let(:idea) { create(:idea) }
let(:author) { create(:user, first_name: 'Rea', last_name: 'Xion') }
let(:comment) { create(:comment, body_multiloc: { 'en' => 'Example comment.' }, author:, idea:) }
let(:campaign) { create(:new_comment_for_admin_campaign) }
let(:activity) { create(:activity, item: comment, action: 'created') }
let!(:resident) { create(:user) }
let!(:admin) { create(:admin) }
let!(:moderator) { create(:project_moderator, projects: [idea.project]) }

it 'returns moderators only' do
expect(campaign.filter_recipient(User.all, activity:).ids).to match_array([admin.id, moderator.id])
end

context 'when the author is a moderator' do
let(:author) { create(:project_moderator, projects: [idea.project]) }

it 'returns no one (empty array)' do
expect(campaign.filter_recipient(User.all, activity:).ids).to eq []
end
end

context 'when there is no author' do
let(:author) { nil }

it 'returns moderators only' do
expect(campaign.filter_recipient(User.all, activity:).ids).to match_array([admin.id, moderator.id])
end
end
end

describe 'apply_recipient_filters' do
let(:campaign) { build(:new_comment_for_admin_campaign) }

Expand Down