diff app/models/tweet.rb @ 137:00dc9346dfaa

Skip self retweets
author nanaya <me@nanaya.pro>
date Wed, 13 Dec 2017 03:44:43 +0900
parents 59991d10f8a3
children 7907fe886675
line wrap: on
line diff
--- a/app/models/tweet.rb	Wed Dec 13 02:52:22 2017 +0900
+++ b/app/models/tweet.rb	Wed Dec 13 03:44:43 2017 +0900
@@ -1,4 +1,11 @@
 class Tweet
+  TIMELINE_OPTIONS = {
+    :count => 100,
+    :exclude_replies => false,
+    :include_rts => true,
+    :tweet_mode => :extended,
+  }
+
   def initialize(twitter_id)
     @clients = {}
     @twitter_id = twitter_id
@@ -14,7 +21,13 @@
     @timeline ||=
       Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
         begin
-          client.user_timeline(@twitter_id, :count => 100, :exclude_replies => false, :include_rts => true, :tweet_mode => :extended)
+          client.user_timeline(@twitter_id, TIMELINE_OPTIONS).select do |tweet|
+            tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id
+          end.map do |tweet|
+            # Fails when there's Twitter::NullObject initiated somewhere in previous select
+            # Reference: https://github.com/sferik/twitter/issues/892
+            tweet.to_h
+          end
         rescue Twitter::Error::TooManyRequests
           @client_config_id += 1
 
@@ -24,6 +37,8 @@
             retry
           end
         end
+      end.map do |tweet_hash|
+        Twitter::Tweet.new(tweet_hash)
       end
   end