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:
parent
a955c51eb6
commit
5149c613f2
@ -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
|
||||||
|
@ -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
|
||||||
|
37
app/services/status_similarity_service.rb
Normal file
37
app/services/status_similarity_service.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user