# HG changeset patch # User nanaya # Date 1513104283 -32400 # Node ID 00dc9346dfaa35d81e0becddb2d4e3c92b56de41 # Parent 59991d10f8a30778c08669700ec02b501da69dc5 Skip self retweets diff -r 59991d10f8a3 -r 00dc9346dfaa app/models/tweet.rb --- 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