Class: ComplaintsController

Inherits:
ApplicationController show all
Defined in:
/var/apps/qpixel/app/controllers/complaints_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#change_content_typeObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 166

def change_content_type
  unless current_user&.same_as?(@complaint.assignee)
    flash[:danger] = 'You are not assigned to this report. Assign yourself before changing the content type.'
    redirect_back fallback_location: complaint_path(@complaint.access_token) and return
  end

  new_content_type = helpers.content_type(params[:new_content_type])
  if new_content_type.nil?
    flash[:danger] = 'Invalid content type.'
    redirect_back fallback_location: complaint_path(@complaint.access_token) and return
  end

  update_params = { content_type: params[:new_content_type] }
  message = "Content type changed to #{new_content_type['name']} by #{current_user.username}."
  @comment = @complaint.comments.new(user: helpers.system_user, internal: true, content: message)

  begin
    Complaint.transaction do
      @complaint.update!(update_params)
      @comment.save!
    end
  rescue ActiveRecord::RecordInvalid
    errors = @complaint.errors.full_messages + @comment.errors.full_messages
    flash[:danger] = "Couldn't change content type (#{errors.join(', ')})"
  end

  redirect_to complaint_path(@complaint.access_token)
end

#commentObject



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
101
102
103
104
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 61

def comment
  permitted = [:content]
  if user_signed_in? && current_user.staff?
    permitted << :internal
  end

  default_params = { user: current_user, internal: false, complaint: @complaint }
  comment_params = default_params.merge(params.permit(*permitted).to_h)

  @comment = ComplaintComment.new(comment_params)
  if @comment.save
    if @comment.user&.staff? && !@comment.internal? && @complaint.user_wants_updates?
      ComplaintsMailer.with(complaint: @complaint, comment: @comment).staff_reply.deliver_later
    end

    if @comment.user.nil? || @comment.user.same_as?(@complaint.user)
      @complaint.update_status 'responded'
    end

    respond_to do |format|
      format.json do
        render json: { status: 'success',
                       comment: render_to_string(partial: 'comment', locals: { comment: @comment },
                                                 formats: [:html]),
                       can_add_more: @complaint.can_add_more_comments?(current_user) }
      end
      format.html do
        flash[:success] = 'Your reply was saved.'
        redirect_to complaint_path(@complaint.access_token)
      end
    end
  else
    respond_to do |format|
      format.json do
        render json: { status: 'failed', message: "Couldn't save your reply", errors: @comment.errors.full_messages },
               status: :bad_request
      end
      format.html do
        flash[:danger] = "Couldn't save your reply: #{@comment.errors.full_messages.join(', ')}"
        redirect_to complaint_path(@complaint.access_token)
      end
    end
  end
end

#createObject



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
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 22

def create
  @report_types = helpers.enabled_report_types
  @content_types = helpers.content_types

  complaint_params = params.permit(:report_type, :reported_url, :content_type, :user_wants_updates)
  comment_params = params.permit(:content)

  if user_signed_in?
    complaint_params.merge!(user: current_user, email: current_user.email)
    comment_params.merge!(user: current_user)
  else
    complaint_params.merge!(email: params[:email])
  end

  @complaint = Complaint.new(complaint_params)
  @comment = ComplaintComment.new(comment_params.merge(internal: false))

  begin
    Complaint.transaction do
      @complaint.save!
      @comment.complaint = @complaint
      @comment.save!
    end

    redirect_to complaint_path(@complaint.access_token)
  rescue ActiveRecord::RecordInvalid
    @errors = @complaint.errors.full_messages + @comment.errors.full_messages
    render :report, status: :bad_request, layout: 'without_sidebar'
  end
end

#indexObject



8
9
10
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 8

def index
  render layout: 'without_sidebar'
end

#reportObject



12
13
14
15
16
17
18
19
20
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 12

def report
  @report_types = helpers.enabled_report_types
  @content_types = helpers.content_types

  @complaint = Complaint.new
  @errors = []

  render layout: 'without_sidebar'
end

#reportingObject



195
196
197
198
199
200
201
202
203
204
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 195

def reporting
  @total = Complaint.recent(12.months.ago).count
  @by_type = Complaint.recent(12.months.ago).group(:report_type).group_by_month(:created_at).count
  @by_content_type = Complaint.recent(12.months.ago).where(report_type: 'illegal')
                              .group(:content_type).group_by_month(:created_at).count
  @by_outcome = Complaint.recent(12.months.ago).where.not(outcome: nil)
                         .group(:outcome).group_by_month(:created_at).count
  @type_totals = Complaint.recent(12.months.ago).group(:report_type).count
  render layout: 'without_sidebar'
end

#reportsObject



106
107
108
109
110
111
112
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 106

def reports
  default_filters = { status: ['new', 'assigned', 'responded'] }
  filters = default_filters.merge(params.permit(:status, :report_type, :outcome).reject { |_k, v| v.blank? })
  @complaints = Complaint.includes(:comments, :user, :assignee).where(**filters).order(created_at: :desc)
                         .paginate(page: params[:page], per_page: 20)
  render layout: 'without_sidebar'
end

#self_assignObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 114

def self_assign
  update_params = { assignee: current_user }
  if @complaint.status == 'new'
    update_params.merge!(status: 'assigned', status_updated_at: DateTime.now)
  end

  @comment = @complaint.comments.new(user: helpers.system_user, internal: true,
                                     content: "Report assigned to #{current_user.username}.")

  begin
    Complaint.transaction do
      @complaint.update!(update_params)
      @comment.save!
    end
  rescue ActiveRecord::RecordInvalid
    errors = @complaint.errors.full_messages + @comment.errors.full_messages
    flash[:danger] = "Couldn't assign you to this report (#{errors.join(', ')})"
  end

  redirect_to complaint_path(@complaint.access_token)
end

#showObject



53
54
55
56
57
58
59
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 53

def show
  @report_type = helpers.report_type(@complaint.report_type)
  @content_type = helpers.content_type(@complaint.content_type)
  @status = helpers.status(@complaint.status)

  render layout: 'without_sidebar'
end

#trainingObject



206
207
208
209
210
211
212
213
214
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 206

def training
  pages = Dir.glob(Rails.root.join('app', 'views', 'complaints', 'training', '*.html.erb'))
             .map { |page| File.basename(page, '.html.erb') }
  if pages.include?(params[:page])
    render "complaints/training/#{params[:page]}", layout: 'osa_training'
  else
    not_found!
  end
end

#training_completeObject



216
217
218
219
220
221
222
223
224
225
226
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 216

def training_complete
  user_update = current_user.update(osa_training: DateTime.now)
  audit_log = AuditLog.moderator_audit(event_type: 'osa_training_completed', user: current_user,
                                       comment: 'OSA training completed.')
  if user_update && audit_log
    flash[:success] = I18n.t('safety_center.training_complete')
  else
    flash[:danger] = I18n.t('safety_center.training_complete_failed')
  end
  redirect_to safety_center_path
end

#update_statusObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File '/var/apps/qpixel/app/controllers/complaints_controller.rb', line 136

def update_status
  unless current_user&.same_as?(@complaint.assignee)
    flash[:danger] = 'You are not assigned to this report. Assign yourself before changing its status.'
    redirect_back fallback_location: complaint_path(@complaint.access_token) and return
  end

  new_status = helpers.status(params[:new_status])
  if new_status.nil?
    flash[:danger] = 'Invalid status.'
    redirect_back fallback_location: complaint_path(@complaint.access_token) and return
  end

  update_params = { status: params[:new_status], status_updated_at: DateTime.now }
  update_params.merge!(outcome: params[:outcome]) unless params[:outcome].nil?
  @comment = @complaint.comments.new(user: helpers.system_user, internal: true,
                                     content: "Status changed to #{new_status['name']} by #{current_user.username}.")

  begin
    Complaint.transaction do
      @complaint.update!(update_params)
      @comment.save!
    end
  rescue ActiveRecord::RecordInvalid
    errors = @complaint.errors.full_messages + @comment.errors.full_messages
    flash[:danger] = "Couldn't change status of this report (#{errors.join(', ')})"
  end

  redirect_to complaint_path(@complaint.access_token)
end