Skip to content

Commit

Permalink
So, this is one of those non-magic places in Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
makyen committed Apr 1, 2023
1 parent 09eac98 commit 630a8b0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/models/spam_wave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ def post_matches?(post, site_ids = nil)
# We only care about everything matching.
# Returning here saves testing the longer strings if a shorter one doesn't match.
post_text = post.send(f.to_sym)
post_text = '' if post_text.nil?
# UTF-8 -> UTF-16 -> UTF-8 idea and code from [answer to: "ruby 1.9: invalid byte sequence in UTF-8"](https://stackoverflow.com/a/8873922)
# by [RubenLaguna](https://stackoverflow.com/users/90580/rubenlaguna), which is under a CC BY-SA 3.0 license.
post_text = post_text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '')
post_text = post_text.encode!('UTF-8', 'UTF-16')
if post_text.nil?
post_text = ''
else
# UTF-8 -> UTF-16 -> UTF-8 idea and code from [answer to: "ruby 1.9: invalid byte sequence in UTF-8"](https://stackoverflow.com/a/8873922)
# by [RubenLaguna](https://stackoverflow.com/users/90580/rubenlaguna), which is under a CC BY-SA 3.0 license.
post_text.encode!('UTF-16', 'UTF-8', invalid: :replace, replace: '')
post_text.encode!('UTF-8', 'UTF-16')
end
Rails.logger.debug "[spam-wave] id: #{id}: #{name}:: post #{f}: encoding: #{post_text.encoding}"
return false unless regex.match?(post_text)
Rails.logger.debug "[spam-wave] id: #{id}: #{name}:: post_matches?: #{f}: MATCHES: post id: #{post.id}: #{post.title}"
Expand Down

0 comments on commit 630a8b0

Please sign in to comment.