8000 Move comment checker to worker by benhalpern · Pull Request #22034 · forem/forem · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Move comment checker to worker #22034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2025
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
13 changes: 8 additions & 5 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,22 @@ def synchronous_bust
8000 expire_root_fragment
end

def create_conditional_autovomits
# return if nothing has changed in body markdown
return unless saved_change_to_body_markdown?

Comments::HandleSpamWorker.perform_async(id)
end

def send_email_notification
Comments::SendEmailNotificationWorker.perform_async(id)
Comments::SendEmailNotificationWorker.perform_in(120.seconds, id)
end

def synchronous_spam_score_check
self.score = -3 if user.registered_at > 48.hours.ago && body_markdown.include?("http")
self.score = -5 if Settings::RateLimit.trigger_spam_for?(text: [title, body_markdown].join("\n"))
end

def create_conditional_autovomits
Spam::Handler.handle_comment!(comment: self)
end

def should_send_email_notification?
parent_exists? &&
parent_user.class.name != "Podcast" &&
Expand Down
7 changes: 4 additions & 3 deletions app/services/ai/comment_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def spam?
response = @ai_client.call(prompt)
parse_response(response)
rescue StandardError => e
Rails.logger.error(e)
false
end

Expand All @@ -43,17 +44,17 @@ def build_prompt
- Gibberish or irrelevant text.
- Out-of-context replies used for promotion.
- Insertion of link in otherwise in-context reply.
- Repetitive, spam-likely messages posted across different content.
- Repetitive, promotional messages posted across different content.

Here is the context:

1. **The Parent Content Post** (The post the comment was left in reply to):
---
Title: #{@comment.commentable.title}
Body: #{@comment.commentable.body_markdown.first(5_000)}
Body#{ "(truncated)" if @comment.commentable.body_markdown.size > 1500}: #{@comment.commentable.body_markdown.first(1_500)}
---

2. **The User's Recent Comment History** (Their last 10 comments):
2. **The User's Recent Comment History**:
---
#{user_history.empty? ? 'No comment history available.' : user_history}
---
Expand Down
12 changes: 12 additions & 0 deletions app/workers/comments/handle_spam_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Comments
class HandleSpamWorker
include Sidekiq::Job

sidekiq_options queue: :high_priority

def perform(comment_id)
comment = Comment.find_by(id: comment_id)
Spam::Handler.handle_comment!(comment: comment) if comment
end
end
end
2 changes: 1 addition & 1 deletion app/workers/comments/send_email_notification_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SendEmailNotificationWorker

def perform(comment_id)
comment = Comment.find_by(id: comment_id)
NotifyMailer.with(comment: comment).new_reply_email.deliver_now if comment
NotifyMailer.with(comment: comment).new_reply_email.deliver_now if comment && comment.score > -1
end
end
end
12 changes: 9 additions & 3 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,16 @@
end

describe "spam" do
it "delegates spam handling to Spam::Handler.handle_comment!" do
allow(Spam::Handler).to receive(:handle_comment!).with(comment: comment).and_call_original
it "delegates spam handling to HandleSpamWorker" do
allow(Comments::HandleSpamWorker).to receive(:perform_async)
comment.save
expect(Spam::Handler).to have_received(:handle_comment!).with(comment: comment)
expect(Comments::HandleSpamWorker).to have_received(:perform_async).with(comment.id).once
end

it "delegates to spam handling again if comment is updated" do
allow(Comments::HandleSpamWorker).to receive(:perform_async)
comment.update(body_markdown: "Updated comment body!")
expect(Comments::HandleSpamWorker).to have_received(:perform_async).with(comment.id).twice
end

it "marks score as negative 3 if new user and comment includes htttp" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

before do
allow(Comment).to receive(:find_by).with(id: 1).and_return(comment)
allow(comment).to receive(:score).and_return(1)
end

it "sends reply email" do
Expand Down
Loading
0