Added StatusSimilarityService to check against spamming same statuses over and over

• Added:
- StatusSimilarityService to check against spamming same statuses over and over
- StatusSimilarityService check to PostStatusService and EditStatusService
This commit is contained in:
Developer 2021-02-18 20:12:40 -05:00
parent a955c51eb6
commit 5149c613f2
3 changed files with 47 additions and 0 deletions

View File

@ -25,6 +25,7 @@ class EditStatusService < BaseService
return idempotency_duplicate if idempotency_given? && idempotency_duplicate? return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
validate_similarity!
validate_links! validate_links!
validate_media! validate_media!
preprocess_attributes! preprocess_attributes!
@ -92,6 +93,10 @@ class EditStatusService < BaseService
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
end end
def validate_similarity!
raise GabSocial::NotPermittedError if StatusSimilarityService.new.call?(@text, @account.id)
end
def validate_links! def validate_links!
raise GabSocial::LinkBlockedError if LinkBlock.block?(@text) raise GabSocial::LinkBlockedError if LinkBlock.block?(@text)
end end

View File

@ -34,6 +34,7 @@ class PostStatusService < BaseService
return idempotency_duplicate if idempotency_given? && idempotency_duplicate? return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
validate_similarity!
validate_links! validate_links!
validate_media! validate_media!
validate_group! validate_group!
@ -159,6 +160,10 @@ class PostStatusService < BaseService
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
end end
def validate_similarity!
raise GabSocial::NotPermittedError if StatusSimilarityService.new.call?(@text, @account.id)
end
def validate_links! def validate_links!
raise GabSocial::NotPermittedError if LinkBlock.block?(@text) raise GabSocial::NotPermittedError if LinkBlock.block?(@text)
end end

View File

@ -0,0 +1,37 @@
# frozen_string_literal: true
require 'similar_text'
class StatusSimilarityService < BaseService
def call?(status_text = "", account_id = nil)
@status_text = status_text
@account_id = account_id
# Not alike if no status_text or no account
# : todo : come up with solution for same image spamming
return false if @status_text.length == 0 || @account_id.nil?
alike?
end
private
def alike?
last_status_text = nil
key = "last_status_from_account:#{@account_id}"
Redis.current.with do |conn|
last_status_text = conn.get(key) || ""
conn.setex(key, 300, @status_text)
end
if last_status_text.nil? || last_status_text.empty? || last_status_text.length == 0
return false
end
likeness = last_status_text.similar(@status_text)
likeness > 85
end
end