diff --git a/app/models/pantograph.rb b/app/models/pantograph.rb index 16490e8f..98664539 100644 --- a/app/models/pantograph.rb +++ b/app/models/pantograph.rb @@ -50,14 +50,18 @@ def self.total_duration (total_texts / (((60 * 60) / Constant.pantography.frequency) * 24 * Constant.pantography.sidereal_year_in_days)); end - def self.sanitize(text) - text.truncate(Constant.pantography.max_length, omission: '') - .gsub(/"|“|”|\‘|\’/, "'") - .gsub(/\&/, '+') - .gsub(/\[\{/, '(') - .gsub(/\]\}/, ')') - .downcase - .gsub(/[^#{ Constant.pantography.alphabet_escaped }]/, '') + def self.sanitize(text, pantographer_id = nil) + text = text.truncate(Constant.pantography.max_length, omission: '') + .gsub(/"|“|”|\‘|\’/, "'") + .gsub(/\&/, '+') + .gsub(/\[\{/, '(') + .gsub(/\]\}/, ')') + .downcase + .gsub(/[^#{ Constant.pantography.alphabet_escaped }]/, '') + if !pantography_twitter_user.blank? && pantographer_id == pantography_twitter_user.id + text = self.spamify(text) + end + text end def self.publish_next @@ -72,7 +76,7 @@ def self.publish_next def self.tweet(text) # Replace @ and # characters before tweeting to avoid spamming other users # These characters are unchanged as far as Pantography itself goes; they are stored intact. - text = self.unspam(text) + text = self.unspamify(text) tweet = authenticated_twitter_client.update(text) if tweet.text == text @@ -91,10 +95,14 @@ def self.tweet(text) end end - def self.unspam(text) + def self.unspamify(text) text.gsub(/@/, 'A').gsub(/#/, 'H') end + def self.spamify(text) + text.gsub(/A/, '@').gsub(/H/, '#') + end + def self.report_error(error_message) PANTOGRAPHY_LOG.error error_message PantographMailer.tweet_failed(error_message).deliver @@ -245,7 +253,7 @@ def self.get_timeline(min_id) authenticated_twitter_client.home_timeline(trim_user: true, min_id: min_id).each do |tweet| user = Pantographer.where(twitter_user_id: tweet.user.id).first_or_create create( - text: sanitize(tweet.text), + text: sanitize(tweet.text, user.id), external_created_at: tweet.created_at, tweet_id: tweet.id, pantographer_id: user.id diff --git a/spec/models/pantograph_spec.rb b/spec/models/pantograph_spec.rb index 39e07318..048ff9d5 100644 --- a/spec/models/pantograph_spec.rb +++ b/spec/models/pantograph_spec.rb @@ -62,8 +62,12 @@ specify { Pantograph.sanitize(Constant.pantography.alphabet_escaped).should eq(Pantograph.alphabet) } end - describe '.unspam' do - Pantograph.unspam('#hashtag @mention #more @more').should == 'Hhashtag Amention Hmore Amore' + describe '.unspamify' do + Pantograph.unspamify('#hashtag @mention #more @more').should == 'Hhashtag Amention Hmore Amore' + end + + describe '.spamify' do + Pantograph.spamify('Hhashtag Amention Hmore Amore').should == '#hashtag @mention #more @more' end describe '.previous_pantograph' do