Skip to content

Commit

Permalink
Connect spamfilter to models
Browse files Browse the repository at this point in the history
  • Loading branch information
NuckChorris committed Dec 8, 2024
1 parent 85d2150 commit 05fd662
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions app/models/media_reaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:,
Expand Down
4 changes: 4 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }]
Expand Down
26 changes: 26 additions & 0 deletions app/workers/spamfilter_worker.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 05fd662

Please sign in to comment.