diff app/models/tweet.rb @ 165:5af9b537db86

Unbreak \everything/ - simplify routing, no more split hackery - fix user lookup and differentiate between id and name lookup
author nanaya <me@nanaya.pro>
date Fri, 03 Aug 2018 02:31:25 +0900
parents ed73d92c7113
children 08cc4a4d8a5f
line wrap: on
line diff
--- a/app/models/tweet.rb	Fri Aug 03 02:12:54 2018 +0900
+++ b/app/models/tweet.rb	Fri Aug 03 02:31:25 2018 +0900
@@ -12,7 +12,7 @@
 
   def initialize(twitter_id)
     @clients = {}
-    @twitter_id = twitter_id.to_s
+    @twitter_id = twitter_id
   end
 
   def id
@@ -47,12 +47,13 @@
 
   def user
     if @user.nil?
-      raw = Rails.cache.fetch("user:v1:#{@twitter_id}", :expires_in => self.class.cache_expires_time) do
+      cache_key = "user:v1:#{@twitter_id.is_a?(Integer) ? 'id' : 'lookup'}:#{@twitter_id}"
+      raw = Rails.cache.fetch(cache_key, :expires_in => self.class.cache_expires_time) do
         client_try(:user, @twitter_id).tap do |data|
           if data[:result] == :ok
             user = data[:data]
 
-            if user.id.to_s != @twitter_id && user.screen_name != @twitter_id
+            if user.id != @twitter_id && user.screen_name.downcase != @twitter_id
               throw "Wrong user data. Requested: #{@twitter_id}, got: #{user.id} (#{user.screen_name.printable})"
             end
           end