Skip to content

Commit

Permalink
AO3-6544 Send comment_type and user_role to spam checker for tickets …
Browse files Browse the repository at this point in the history
…and comments (#5084)

* AO3-6544 Add comment_type and user_role to data sent to spam checker for comments and tickets

* AO3-6544 Add comment_type for comments
  • Loading branch information
sarken authored Mar 11, 2025
1 parent 436ffc0 commit be7d221
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/abuse_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ def logged_in_with_matching_email?

def akismet_attributes
name = username ? username : ""
# If the user is logged in and we're sending info to Akismet, we can assume
# the email does not match.
role = User.current_user.present? ? "user-with-nonmatching-email" : "guest"
{
comment_type: "contact-form",
key: ArchiveConfig.AKISMET_KEY,
blog: ArchiveConfig.AKISMET_NAME,
user_ip: ip_address,
user_role: role,
comment_author: name,
comment_author_email: email,
comment_content: comment
Expand Down
6 changes: 6 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,17 @@ def check_for_spam
has_comment_methods

def akismet_attributes
# While we do have tag comments, those are from logged-in users with special
# access granted by admins, so we never spam check them, unlike comments on
# works or admin posts.
comment_type = ultimate_parent.is_a?(Work) ? "fanwork-comment" : "comment"
{
comment_type: comment_type,
key: ArchiveConfig.AKISMET_KEY,
blog: ArchiveConfig.AKISMET_NAME,
user_ip: ip_address,
user_agent: user_agent,
user_role: "guest",
comment_author: name,
comment_author_email: email,
comment_content: comment_content
Expand Down
5 changes: 5 additions & 0 deletions app/models/feedback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ def logged_in_with_matching_email?
end

def akismet_attributes
# If the user is logged in and we're sending info to Akismet, we can assume
# the email does not match.
role = User.current_user.present? ? "user-with-nonmatching-email" : "guest"
{
comment_type: "contact-form",
key: ArchiveConfig.AKISMET_KEY,
blog: ArchiveConfig.AKISMET_NAME,
user_ip: ip_address,
user_agent: user_agent,
user_role: role,
comment_author_email: email,
comment_content: comment
}
Expand Down
1 change: 1 addition & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ def akismet_attributes
key: ArchiveConfig.AKISMET_KEY,
blog: ArchiveConfig.AKISMET_NAME,
user_ip: ip_address,
user_role: "user",
comment_date_gmt: created_at.to_time.iso8601,
blog_lang: language.short,
comment_author: user.login,
Expand Down
17 changes: 17 additions & 0 deletions spec/models/abuse_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@
end
end

context "when report is submitted to Akismet" do
let(:report) { build(:abuse_report) }

it "has comment_type \"contact-form\"" do
expect(report.akismet_attributes[:comment_type]).to eq("contact-form")
end

it "has user_role \"user-with-nonmatching-email\" when reporter is logged in" do
User.current_user = create(:user)
expect(report.akismet_attributes[:user_role]).to eq("user-with-nonmatching-email")
end

it "has user_role \"guest\" when reporter is logged out" do
expect(report.akismet_attributes[:user_role]).to eq("guest")
end
end

describe "#attach_work_download" do
include ActiveJob::TestHelper

Expand Down
40 changes: 40 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@
.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when submitting comment to Akismet" do
subject { create(:comment) }

it "has user_role \"guest\"" do
expect(subject.akismet_attributes[:user_role]).to eq("guest")
end

context "when the commentable is a chapter" do
it "has comment_type \"fanwork-comment\"" do
expect(subject.akismet_attributes[:comment_type]).to eq("fanwork-comment")
end
end

context "when the commentable is an admin post" do
subject { create(:comment, :on_admin_post) }

it "has comment_type \"comment\"" do
expect(subject.akismet_attributes[:comment_type]).to eq("comment")
end
end

context "when the commentable is a comment" do
context "when the comment is on a chapter" do
subject { create(:comment, commentable: create(:comment)) }

it "has comment_type \"fanwork-comment\"" do
expect(subject.akismet_attributes[:comment_type]).to eq("fanwork-comment")
end
end

context "when the comment is on an admin post" do
subject { create(:comment, commentable: create(:comment, :on_admin_post)) }

it "has comment_type \"comment\"" do
expect(subject.akismet_attributes[:comment_type]).to eq("comment")
end
end
end
end
end

context "with an existing comment from the same user" do
Expand Down
17 changes: 17 additions & 0 deletions spec/models/feedback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,21 @@
expect(safe_report.save).to be_truthy
end
end

context "when report is submitted to Akismet" do
let(:report) { build(:feedback) }

it "has comment_type \"contact-form\"" do
expect(report.akismet_attributes[:comment_type]).to eq("contact-form")
end

it "has user_role \"user-with-nonmatching-email\" when reporter is logged in" do
User.current_user = create(:user)
expect(report.akismet_attributes[:user_role]).to eq("user-with-nonmatching-email")
end

it "has user_role \"guest\" when reporter is logged out" do
expect(report.akismet_attributes[:user_role]).to eq("guest")
end
end
end

0 comments on commit be7d221

Please sign in to comment.