comparison app/models/tweet.rb @ 160:4e4195e60c2b

Add check at user level as well
author nanaya <me@nanaya.pro>
date Fri, 03 Aug 2018 01:45:16 +0900
parents 74422bae017d
children c2e9d3a36bde
comparison
equal deleted inserted replaced
159:59ff3dabf070 160:4e4195e60c2b
25 raw = Rails.cache.fetch(cache_key, :expires_in => self.class.cache_expires_time) do 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 client_try(:user_timeline, id, TIMELINE_OPTIONS).tap do |data|
27 if data[:result] == :ok 27 if data[:result] == :ok
28 if data[:data].any? && data[:data].first.user.id != id 28 if data[:data].any? && data[:data].first.user.id != id
29 wrong_user = data[:data].first.user 29 wrong_user = data[:data].first.user
30 throw "Wrong timeline data. Requested: #{id}, got: #{wrong_user.id} (#{wrong_user.name.printable})" 30 throw "Wrong timeline data. Requested: #{id}, got: #{wrong_user.id} (#{wrong_user.screen_name.printable})"
31 end 31 end
32 32
33 data[:data] = data[:data].select do |tweet| 33 data[:data] = data[:data].select do |tweet|
34 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
35 end.map { |tweet| tweet.to_h } 35 end.map { |tweet| tweet.to_h }
46 end 46 end
47 47
48 def user 48 def user
49 if @user.nil? 49 if @user.nil?
50 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => self.class.cache_expires_time) do 50 raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => self.class.cache_expires_time) do
51 client_try :user, @twitter_id 51 client_try(:user, @twitter_id).tap do |data|
52 if data[:result] == :ok
53 user = data[:data]
54
55 if user.id != @twitter_id || user.screen_name != @twitter_id
56 throw "Wrong user data. Requested: #{@twitter_id}, got: #{user.id} (#{user.screen_name.printable})"
57 end
58 end
59 end
52 end 60 end
53 61
54 raise Twitter::Error::NotFound if raw[:result] == :not_found 62 raise Twitter::Error::NotFound if raw[:result] == :not_found
55 63
56 @user = raw[:data] 64 @user = raw[:data]