mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-11-05 12:42:48 -05:00
- 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>
46 lines
1.1 KiB
Ruby
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
|