aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/tweets_controller.rb
blob: 8708898afdacad09693fcc78c5fa309c2f764df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class TweetsController < ApplicationController
  def show
    @tweet = Tweet.find(params[:id])
    authorize! @user = @tweet.user

    @sidebars = [:user]
    @title = "\"#{view_context.truncate(CGI.unescapeHTML(@tweet.text))}\" from #{@user.name} (@#{@user.screen_name})"
    @header = "@#{@user.screen_name}'s Tweet"
  rescue ActiveRecord::RecordNotFound
    import
  end

  def import
    tweet = Tweet.import_from_twitter(params[:id], current_user)
    redirect_to tweet
  end

  def i_responses
    authorize! @tweet = Tweet.find(params[:id])
  end

  def user_index
    authorize! @user = User.find(screen_name: params[:screen_name])

    if @user.registered?
      user_best
    else
      user_timeline
    end
  end

  def user_best
    authorize! @user ||= User.find(screen_name: params[:screen_name])
    @tweets = @user.tweets.reacted.parse_recent(params[:recent]).order_by_reactions.paginate(params)

    @sidebars = [:user, :recent_thresholds]
    @title = "@#{@user.screen_name}'s Best Tweets"
  end

  def user_timeline
    authorize! @user ||= User.find(screen_name: params[:screen_name])
    @tweets = @user.tweets.reacted(params[:reactions]).order_by_id.paginate(params)

    @sidebars = [:user, :reactions_thresholds]
    @title = "@#{@user.screen_name}'s Timeline"
  end

  def user_favorites
    authorize! @user = User.find(screen_name: params[:screen_name])
    @tweets = Tweet.reacted(params[:reactions]).favorited_by(@user).order("`favorites`.`id` DESC").eager_load(:user).paginate(params)

    @sidebars = [:user, :reactions_thresholds]
    @title = "@#{@user.screen_name}'s Favorites"
  end

  def user_favorited_by
    authorize! @user = User.find(screen_name: params[:screen_name])
    authorize! @source_user = User.find(screen_name: params[:source_screen_name])
    @tweets = @user.tweets.reacted(params[:reactions]).favorited_by(@source_user).order_by_id.eager_load(:user).paginate(params)

    @sidebars = [:user, :reactions_thresholds]
    @title = "@#{@user.screen_name}'s Tweets favorited by @#{@source_user.screen_name}"
  end

  def all_best
    @tweets = Tweet.reacted.parse_recent(params[:recent]).order_by_reactions.eager_load(:user).paginate(params)

    @sidebars = [:all, :recent_thresholds]
    @title = "Top Tweets"
  end

  def all_timeline
    @tweets = Tweet.reacted(params[:reactions]).order_by_id.eager_load(:user).paginate(params)

    @sidebars = [:all, :reactions_thresholds]
    @title = "Public Timeline"
  end

  def filter
    @tweets = Tweet.recent((params[:period] || 7).days).filter_by_query(params[:q].to_s).order_by_id.eager_load(:user).paginate(params)

    @sidebars = [:all]
    @title = "Filter"
  end

  private
  def render(*args)
    return super(*args) if args.size > 0

    if template_exists?(params[:action], _prefixes)
      super
    else
      if @tweets
        super("tweets")
      else
        # bug
      end
    end
  end
end