Files
growstuff/app/models/comment.rb
T
Daniel O'Connorandgoogle-labs-jules[bot] d387bfbaac Ban users with maximum spam score (#5190)
- Define `MAX_SCORE = 100` constant on `UserBehaviourDetectionService`
- Add `Member#ban!` and `Member#banned?` methods
- Automatically ban (discard) member in `Member#update_spam_score!` when score reaches `MAX_SCORE`
- Trigger `update_author_spam_score` in `after_save` callbacks on `Post` and `Comment`
- Add unit specs for MAX_SCORE, member banning, and model spam score triggers

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-10-31 23:41:44 +10:30

46 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class Comment < ApplicationRecord
belongs_to :author, class_name: 'Member', inverse_of: :comments
belongs_to :commentable, polymorphic: true, counter_cache: true
# validates :body, presence: true
validate :author_is_not_blocked
scope :post_order, -> { order(created_at: :asc) } # for display on post page
after_create do
recipient = commentable.author.id
sender = author.id
# don't send notifications to yourself
if recipient != sender
Notification.create(
recipient_id: recipient,
sender_id: sender,
subject: "#{author} commented on #{commentable.subject}",
body:,
notifiable: commentable
)
end
end
after_save :update_author_spam_score
def to_s
"#{author.login_name} commented on #{commentable.subject}"
end
private
def update_author_spam_score
author&.update_spam_score!
end
def author_is_not_blocked
return unless author
return unless commentable.author.already_blocking?(author)
errors.add(:base, "You cannot comment on a post of a member who has blocked you.")
end
end