comparison app/models/tweet.rb @ 158:74422bae017d

Always use canonical id and turn cache time generator a class method
author nanaya <me@nanaya.pro>
date Fri, 03 Aug 2018 01:38:53 +0900
parents 6e6051cd9cca
children 4e4195e60c2b
comparison
equal deleted inserted replaced
157:6e6051cd9cca 158:74422bae017d
4 :exclude_replies => false, 4 :exclude_replies => false,
5 :include_rts => true, 5 :include_rts => true,
6 :tweet_mode => :extended, 6 :tweet_mode => :extended,
7 } 7 }
8 8
9 def self.cache_expires_time
10 (15 + rand(15)).minutes
11 end
12
9 def initialize(twitter_id) 13 def initialize(twitter_id)
10 @clients = {} 14 @clients = {}
11 @twitter_id = twitter_id 15 @twitter_id = twitter_id
12 end 16 end
13 17
14 def cache_expires_time 18 def id
15 (15 + rand(15)).minutes 19 user.id
16 end
17
18 def cache_key
19 "timeline:v2:#{@twitter_id}/#{Base64.urlsafe_encode64 @twitter_id.to_s}"
20 end 20 end
21 21
22 def timeline 22 def timeline
23 if @timeline.nil? 23 if @timeline.nil?
24 raw = Rails.cache.fetch(cache_key, :expires_in => cache_expires_time) do 24 cache_key = "timeline:v2:#{id}/#{Base64.urlsafe_encode64 id.to_s}"
25 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data| 25 raw = Rails.cache.fetch(cache_key, :expires_in => self.class.cache_expires_time) do
26 client_try(:user_timeline, id, TIMELINE_OPTIONS).tap do |data|
26 if data[:result] == :ok 27 if data[:result] == :ok
27 if data[:data].any? && data[:data].first.user.id != @twitter_id 28 if data[:data].any? && data[:data].first.user.id != id
28 wrong_user = data[:data].first.user 29 wrong_user = data[:data].first.user
29 Rails.logger.warn "Wrong timeline data. Requested: #{@twitter_id}, got: #{wrong_user.id} (#{wrong_user.name.printable})" 30 throw "Wrong timeline data. Requested: #{id}, got: #{wrong_user.id} (#{wrong_user.name.printable})"
30 end 31 end
31 32
32 data[:data] = data[:data].select do |tweet| 33 data[:data] = data[:data].select do |tweet|
33 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id 34 tweet.retweeted_status.nil? || tweet.user.id != tweet.retweeted_status.user.id
34 end.map { |tweet| tweet.to_h } 35 end.map { |tweet| tweet.to_h }
44 @timeline 45 @timeline
45 end 46 end
46 47
47 def user 48 def user
48 if @user.nil? 49 if @user.nil?
49 return timeline.first.user if timeline.any? 50 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => self.class.cache_expires_time) do
50
51 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => cache_expires_time) do
52 client_try :user, @twitter_id 51 client_try :user, @twitter_id
53 end 52 end
54 53
55 raise Twitter::Error::NotFound if raw[:result] == :not_found 54 raise Twitter::Error::NotFound if raw[:result] == :not_found
56 55
88 87
89 { :result => :ok, :data => data } 88 { :result => :ok, :data => data }
90 end 89 end
91 90
92 def client_config_id 91 def client_config_id
93 @client_config_id ||= 0 92 @client_config_count ||= $cfg[:twitter].size
93 @client_config_id ||= rand(@client_config_count)
94 94
95 @client_config_id %= $cfg[:twitter].size 95 @client_config_id %= @client_config_count
96 end 96 end
97 end 97 end