view app/controllers/tweets_controller.rb @ 203:af84c9f23263

Fix redirect for twitter user with nil screen name Everything else is still kind of broken but it's kind of beyond help anyway
author nanaya <me@nanaya.pro>
date Mon, 19 Oct 2020 03:42:22 +0900
parents 469df6354341
children 8190fa511e35
line wrap: on
line source

class TweetsController < ApplicationController
  def index
    return redirect if params[:id].present? || params[:name].present?
  end

  def show
    return redirect if params[:id][/D/].present?

    client = Tweet.new(params[:id].to_i)
    @user = client.user

    return redirect if normalized_screen_name != params[:name]

    @tweets = client.timeline
  rescue Twitter::Error::NotFound
    head :not_found
  rescue Twitter::Error::Unauthorized
    head :forbidden
  end

  def redirect
    @user ||= Tweet.new(params[:id].presence || params[:name]).user
    redirect_to tweet_path(@user.id, normalized_screen_name)
  rescue Twitter::Error::NotFound
    head :not_found
  rescue Twitter::Error::Unauthorized
    head :forbidden
  end

  private
  def normalized_screen_name
    @user.screen_name.presence || '_'
  end
end