diff app/controllers/tweets_controller.rb @ 234:7a773720d81f legit-client

Totally legit client
author nanaya <me@nanaya.net>
date Fri, 14 Jul 2023 22:42:20 +0900
parents 0f0cc55ff11b
children 4222343d9433
line wrap: on
line diff
--- a/app/controllers/tweets_controller.rb	Fri Jul 14 01:45:40 2023 +0900
+++ b/app/controllers/tweets_controller.rb	Fri Jul 14 22:42:20 2023 +0900
@@ -1,39 +1,38 @@
 class TweetsController < ApplicationController
   def index
-    return redirect if params[:id].present? || params[:name].present?
+    return redirect if params[:name].present?
   end
 
   def show
     return redirect if params[:id][/\D/].present?
 
-    client = Tweet.new(params[:id].to_i)
-    @user = client.user
+    @user = CachedFetch.user_by_id params[:id]
+
+    if @user.nil?
+      head :not_found
+      return
+    end
 
     return redirect if normalized_screen_name != params[:name]
 
-    @tweets = client.timeline
-  rescue Twitter::Error::Forbidden
-    head :forbidden
-  rescue Twitter::Error::NotFound
-    head :not_found
-  rescue Twitter::Error::Unauthorized
-    head :forbidden
+    @tweets = CachedFetch.timeline params[:id]
+
+    head :not_found if @tweets.nil?
   end
 
   def redirect
-    @user ||= Tweet.new(params[:id].presence || params[:name]).user
-    redirect_to tweet_path(@user.id, normalized_screen_name)
-  rescue Twitter::Error::Forbidden
-    head :forbidden
-  rescue Twitter::Error::NotFound
-    head :not_found
-  rescue Twitter::Error::Unauthorized
-    head :forbidden
+    @user ||= CachedFetch.user_by_username(params[:name])
+
+    if @user.nil?
+      head :not_found
+    else
+      redirect_to tweet_path(@user[:id], normalized_screen_name)
+    end
   end
 
   private
 
   def normalized_screen_name
-    @user.screen_name.presence || '_'
+    @user[:username].presence || '_'
   end
 end