view app/controllers/tweets_controller.rb @ 227:e8eb5caae9c1

Use a different account
author nanaya <me@nanaya.pro>
date Sun, 03 Apr 2022 21:09:58 +0900
parents f9fd9d9cab97
children 0f0cc55ff11b
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::Forbidden
    head :forbidden
  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::Forbidden
    head :forbidden
  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