diff --git a/app/models/comment.rb b/app/models/comment.rb index dcb08dd4e..8f3d2761a 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -92,4 +92,8 @@ def no_grandparents end # PostFollow.create(user: user, post: post) end + + after_commit if: :saved_change_to_content? do + SpamfilterWorker.perform(self, :content) + end end diff --git a/app/models/media_reaction.rb b/app/models/media_reaction.rb index dd354dc5d..2123431ba 100644 --- a/app/models/media_reaction.rb +++ b/app/models/media_reaction.rb @@ -31,6 +31,10 @@ class MediaReaction < ApplicationRecord votes.destroy_all if reaction_changed? end + after_commit if: :saved_change_to_reaction? do + SpamfilterWorker.perform(self, :reaction) + end + def stream_activity user.profile_feed.activities.new( progress:, diff --git a/app/models/post.rb b/app/models/post.rb index c24459c4a..67dc9de90 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -141,6 +141,10 @@ def locked? end end + after_commit if: :saved_change_to_content? do + SpamfilterWorker.perform(self, :content) + end + before_destroy do deletions = reposts.pluck(:user_id, :id).map do |user_id, repost_id| [['user', user_id], { foreign_id: "repost:#{repost_id}" }] diff --git a/app/workers/spamfilter_worker.rb b/app/workers/spamfilter_worker.rb new file mode 100644 index 000000000..2f2f614ec --- /dev/null +++ b/app/workers/spamfilter_worker.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class SpamfilterWorker + include Sidekiq::Worker + + def self.perform_async(record, content_field:) + super( + record.to_global_id.to_s, + content_field.to_s + ) + end + + def perform(record, content_field) + record = GlobalID::Locator.locate(record) + + spamfilter = SpamfilterService.call( + account_age_hours: (record.created_at - record.user.created_at) / 1.hour, + content: record.public_send(content_field) + ) + + record.update_moderation_scores( + scores: { nyckel_spamminess: spamfilter.spamminess }, + held_reason: spamfilter.spam? ? :spamfilter : nil + ) + end +end