2022-07-30 18:29:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-05 07:02:23 +01:00
|
|
|
class AnswerController < ApplicationController
|
2023-02-07 22:59:48 +01:00
|
|
|
before_action :authenticate_user!, only: %i[pin unpin]
|
2023-02-07 08:32:25 +01:00
|
|
|
|
2023-02-12 20:29:36 +01:00
|
|
|
include TurboStreamable
|
|
|
|
|
|
|
|
turbo_stream_actions :pin, :unpin
|
|
|
|
|
2014-12-05 07:02:23 +01:00
|
|
|
def show
|
2023-11-26 19:32:50 +01:00
|
|
|
@answer = Answer.for_user(current_user).includes(question: [:user], smiles: [:user]).find(params[:id])
|
2014-12-12 18:54:17 +01:00
|
|
|
@display_all = true
|
2023-02-19 15:51:19 +01:00
|
|
|
|
|
|
|
return unless user_signed_in?
|
|
|
|
|
|
|
|
mark_notifications_as_read
|
2014-12-05 07:02:23 +01:00
|
|
|
end
|
2023-02-07 08:32:25 +01:00
|
|
|
|
|
|
|
def pin
|
|
|
|
answer = Answer.includes(:user).find(params[:id])
|
|
|
|
UseCase::Answer::Pin.call(user: current_user, answer:)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to(user_path(username: current_user.screen_name)) }
|
2023-02-12 20:29:36 +01:00
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.update("ab-pin-#{answer.id}", partial: "actions/pin", locals: { answer: }),
|
|
|
|
render_toast(t(".success"))
|
|
|
|
]
|
|
|
|
end
|
2023-02-07 08:32:25 +01:00
|
|
|
end
|
|
|
|
end
|
2023-02-07 08:36:29 +01:00
|
|
|
|
|
|
|
def unpin
|
|
|
|
answer = Answer.includes(:user).find(params[:id])
|
|
|
|
UseCase::Answer::Unpin.call(user: current_user, answer:)
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to(user_path(username: current_user.screen_name)) }
|
2023-02-12 20:29:36 +01:00
|
|
|
format.turbo_stream do
|
|
|
|
render turbo_stream: [
|
|
|
|
turbo_stream.update("ab-pin-#{answer.id}", partial: "actions/pin", locals: { answer: }),
|
|
|
|
render_toast(t(".success"))
|
|
|
|
]
|
|
|
|
end
|
2023-02-07 08:36:29 +01:00
|
|
|
end
|
|
|
|
end
|
2023-02-17 01:18:33 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def mark_notifications_as_read
|
2023-06-16 18:20:21 +02:00
|
|
|
updated = Notification.where(recipient_id: current_user.id, new: true)
|
2023-02-17 01:18:33 +01:00
|
|
|
.and(Notification.where(type: "Notification::QuestionAnswered", target_id: @answer.id)
|
|
|
|
.or(Notification.where(type: "Notification::Commented", target_id: @answer.comments.pluck(:id)))
|
|
|
|
.or(Notification.where(type: "Notification::Smiled", target_id: @answer.smiles.pluck(:id)))
|
|
|
|
.or(Notification.where(type: "Notification::CommentSmiled", target_id: @answer.comment_smiles.pluck(:id))))
|
|
|
|
.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
|
2023-06-16 18:20:21 +02:00
|
|
|
current_user.touch(:notifications_updated_at) if updated.positive?
|
2023-02-17 01:18:33 +01:00
|
|
|
end
|
2014-12-05 07:02:23 +01:00
|
|
|
end
|