|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class RelationshipsController < ApplicationController |
| 4 | + layout 'admin' |
| 5 | + |
| 6 | + before_action :authenticate_user! |
| 7 | + before_action :set_accounts, only: :show |
| 8 | + before_action :set_body_classes |
| 9 | + |
| 10 | + helper_method :following_relationship?, :followed_by_relationship?, :mutual_relationship? |
| 11 | + |
| 12 | + def show |
| 13 | + @form = Form::AccountBatch.new |
| 14 | + end |
| 15 | + |
| 16 | + def update |
| 17 | + @form = Form::AccountBatch.new(form_account_batch_params.merge(current_account: current_account, action: action_from_button)) |
| 18 | + @form.save |
| 19 | + rescue ActionController::ParameterMissing |
| 20 | + # Do nothing |
| 21 | + ensure |
| 22 | + redirect_to relationships_path(current_params) |
| 23 | + end |
| 24 | + |
| 25 | + private |
| 26 | + |
| 27 | + def set_accounts |
| 28 | + @accounts = relationships_scope.page(params[:page]).per(40) |
| 29 | + end |
| 30 | + |
| 31 | + def relationships_scope |
| 32 | + scope = begin |
| 33 | + if following_relationship? |
| 34 | + current_account.following.includes(:account_stat) |
| 35 | + else |
| 36 | + current_account.followers.includes(:account_stat) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + scope.merge!(Follow.recent) |
| 41 | + scope.merge!(mutual_relationship_scope) if mutual_relationship? |
| 42 | + scope.merge!(abandoned_account_scope) if params[:status] == 'abandoned' |
| 43 | + scope.merge!(active_account_scope) if params[:status] == 'active' |
| 44 | + scope.merge!(by_domain_scope) if params[:by_domain].present? |
| 45 | + |
| 46 | + scope |
| 47 | + end |
| 48 | + |
| 49 | + def mutual_relationship_scope |
| 50 | + Account.where(id: current_account.following) |
| 51 | + end |
| 52 | + |
| 53 | + def abandoned_account_scope |
| 54 | + Account.where.not(moved_to_account_id: nil) |
| 55 | + end |
| 56 | + |
| 57 | + def active_account_scope |
| 58 | + Account.where(moved_to_account_id: nil) |
| 59 | + end |
| 60 | + |
| 61 | + def by_domain_scope |
| 62 | + Account.where(domain: params[:by_domain]) |
| 63 | + end |
| 64 | + |
| 65 | + def form_account_batch_params |
| 66 | + params.require(:form_account_batch).permit(:action, account_ids: []) |
| 67 | + end |
| 68 | + |
| 69 | + def following_relationship? |
| 70 | + params[:relationship].blank? || params[:relationship] == 'following' |
| 71 | + end |
| 72 | + |
| 73 | + def mutual_relationship? |
| 74 | + params[:relationship] == 'mutual' |
| 75 | + end |
| 76 | + |
| 77 | + def followed_by_relationship? |
| 78 | + params[:relationship] == 'followed_by' |
| 79 | + end |
| 80 | + |
| 81 | + def current_params |
| 82 | + params.slice(:page, :status, :relationship, :by_domain).permit(:page, :status, :relationship, :by_domain) |
| 83 | + end |
| 84 | + |
| 85 | + def action_from_button |
| 86 | + if params[:unfollow] |
| 87 | + 'unfollow' |
| 88 | + elsif params[:remove_from_followers] |
| 89 | + 'remove_from_followers' |
| 90 | + elsif params[:block_domains] |
| 91 | + 'block_domains' |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def set_body_classes |
| 96 | + @body_classes = 'admin' |
| 97 | + end |
| 98 | +end |
0 commit comments