summaryrefslogtreecommitdiff
path: root/app/controllers/lists_controller.rb
blob: 73c25e73c37e55d1fe6684bcd2ae1ab16d237384 (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
# frozen_string_literal: true

class ListsController < ApplicationController
  include TimePeriod
  before_action :setup_pagination_and_tags
  before_action :set_image_filter
  before_action :get_time_period, except: [:my_comments, :recent_comments]
  skip_authorization_check

  def index
    @title = 'Rankings'
    # TODO: msearch
    @top_scoring_images, @all_time_top_scoring_images, @top_commented_images = [
      get_top_scoring(3),
      get_top_scoring(3, all_time: true),
      get_top_commented(3)
    ].map { |s| s.records } #(includes: :tags) }

    @interactions = PostQuery.interactions((@top_commented_images.map(&:id) + @top_scoring_images.map(&:id) + @all_time_top_scoring_images.map(&:id)).compact.uniq, current_user.id) if current_user

    respond_to do |format|
      format.html
      format.json { render json: { top_scoring: @top_scoring_images, top_commented: @top_commented_images, all_time_top_scoring: @all_time_top_scoring_images, interactions: (@interactions || []) } }
    end
  end

  def recent_comments
    @title = 'Recent Comments - Lists'
    @hidden_tags = @current_filter.hidden_tag_ids
    @recent_comments = get_recent_comments(nil, nil, current_user).records(includes: { post: :tags })
    respond_to do |format|
      format.html
      format.json { render json: @recent_comments }
    end
  end

  def my_comments
    my_id = current_user&.id
    user_id = params[:user_id]&.to_i|| my_id
    is_self = user_id == my_id

    unless user_id
      redirect_to '/', error: 'You must be logged in to view your comments'
      return
    end

    @user = User.find_by_slug_or_id(user_id)

    unless @user
      redirect_to '/', error: 'No such user exits'
      return
    end

    unless is_self
      authorize! :edit, @user
    end

    show_name = !force_anonymous? || is_self || current_user&.staff?

    @title = "#{is_self ? 'Your' : (show_name ? @user.name + "'s" : 'Anonymous\'')} Comments - Lists"
    @hidden_tags = @current_filter.hidden_tag_ids
    @my_comments = get_recent_comments(nil, user_id, current_user).records(includes: { post: :tags })

    respond_to do |format|
      format.html
      format.json { render json: @my_comments }
    end
  end

  private

  def get_top_commented(limit)
    options = default_image_filter_options
    options.delete :per_page
    Post.fancy_search(options.merge(size: limit, last_first_seen: @time_period)) do |s|
      s.add_sort comment_count: :desc
      s.add_sort id: :desc
    end
  end
end