Retrospring/app/controllers/answer_controller.rb

62 lines
2.1 KiB
Ruby
Raw Normal View History

2022-07-30 09:29:32 -07:00
# frozen_string_literal: true
2014-12-04 22:02:23 -08:00
class AnswerController < ApplicationController
before_action :authenticate_user!, only: %i[pin unpin]
2023-02-12 11:29:36 -08:00
include TurboStreamable
turbo_stream_actions :pin, :unpin
2014-12-04 22:02:23 -08:00
def show
2022-07-30 09:29:32 -07:00
@answer = Answer.includes(comments: %i[user smiles], question: [:user], smiles: [:user]).find(params[:id])
2014-12-12 09:54:17 -08:00
@display_all = true
2023-03-19 08:00:15 -07:00
@subscribed_answer_ids = []
return unless user_signed_in?
2023-03-19 08:00:15 -07:00
@subscribed_answer_ids = Subscription.where(user: current_user, answer: @answer).pluck(:answer_id)
mark_notifications_as_read
2014-12-04 22:02:23 -08:00
end
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 11:29:36 -08: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
end
end
2023-02-06 23:36:29 -08: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 11:29:36 -08: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-06 23:36:29 -08:00
end
end
private
def mark_notifications_as_read
Notification.where(recipient_id: current_user.id, new: true)
.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
end
2014-12-04 22:02:23 -08:00
end