Add `SubscriptionsController`

This commit is contained in:
Andreas Nedbal 2023-10-25 02:50:10 +02:00
parent 5b55f8e6ed
commit cefb805243
4 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,39 @@
class SubscriptionsController < ApplicationController
include TurboStreamable
before_action :authenticate_user!
turbo_stream_actions :create, :destroy
def create
answer = Answer.find(params[:answer])
result = Subscription.subscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/destroy", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
def destroy
answer = Answer.find(params[:answer])
result = Subscription.unsubscribe(current_user, answer)
respond_to do |format|
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("subscription-#{answer.id}", partial: "subscriptions/create", locals: { answer: }),
render_toast(t(result.present? ? ".success" : ".error"), result.present?)
]
end
format.html { redirect_to answer_path(username: answer.user.screen_name, id: answer.id) }
end
end
end

View File

@ -0,0 +1,3 @@
= button_to subscriptions_path(answer: answer.id), class: "dropdown-item", id: "subscription-#{answer.id}" do
%i.fa.fa-fw.fa-bell
= t("voc.subscribe")

View File

@ -0,0 +1,3 @@
= button_to subscriptions_path(answer: answer.id), method: :delete, class: "dropdown-item", id: "subscription-#{answer.id}" do
%i.fa.fa-fw.fa-bell-slash
= t("voc.unsubscribe")

View File

@ -148,6 +148,8 @@ Rails.application.routes.draw do
post "/inbox/create", to: "inbox#create", as: :inbox_create
get "/inbox", to: "inbox#show", as: :inbox
resource :subscriptions, controller: :subscriptions, only: %i[create destroy]
get "/user/:username", to: "user#show"
get "/@:username", to: "user#show", as: :user
get "/@:username/a/:id", to: "answer#show", as: :answer