From be947bf4e2181c001cabce84f9befc541fa660fc Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Thu, 26 Oct 2023 08:16:18 +0200 Subject: [PATCH] Add `RelationshipsController` --- app/controllers/relationships_controller.rb | 47 +++++++++++++++++++++ app/views/relationships/_create.html.haml | 13 ++++++ app/views/relationships/_destroy.html.haml | 13 ++++++ config/routes.rb | 1 + 4 files changed, 74 insertions(+) create mode 100644 app/controllers/relationships_controller.rb create mode 100644 app/views/relationships/_create.html.haml create mode 100644 app/views/relationships/_destroy.html.haml diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb new file mode 100644 index 00000000..d5c134ae --- /dev/null +++ b/app/controllers/relationships_controller.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +class RelationshipsController < ApplicationController + include TurboStreamable + + before_action :authenticate_user! + + turbo_stream_actions :create, :destroy + + def create + UseCase::Relationship::Create.call( + source_user: current_user, + target_user: ::User.find_by!(screen_name: params[:screen_name]), + type: params[:type] + ) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: [ + turbo_stream.replace("#{params[:type]}-#{params[:screen_name]}", partial: "relationships/destroy", locals: { type: params[:type], screen_name: params[:screen_name] }), + render_toast(t(".#{params[:type]}.success")) + ] + end + + format.html { redirect_back(fallback_location: user_path(username: params[:screen_name])) } + end + end + + def destroy + UseCase::Relationship::Destroy.call( + source_user: current_user, + target_user: ::User.find_by!(screen_name: params[:screen_name]), + type: params[:type] + ) + + respond_to do |format| + format.turbo_stream do + render turbo_stream: [ + turbo_stream.replace("#{params[:type]}-#{params[:screen_name]}", partial: "relationships/create", locals: { type: params[:type], screen_name: params[:screen_name] }), + render_toast(t(".#{params[:type]}.success")) + ] + end + + format.html { redirect_back(fallback_location: user_path(username: params[:screen_name])) } + end + end +end diff --git a/app/views/relationships/_create.html.haml b/app/views/relationships/_create.html.haml new file mode 100644 index 00000000..23c91782 --- /dev/null +++ b/app/views/relationships/_create.html.haml @@ -0,0 +1,13 @@ +- if type == "follow" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", form_class: "d-grid" do + = t("voc.follow") + +- if type == "block" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-minus-circle.fa-fw + = t("voc.block") + +- if type == "mute" + = button_to relationships_path(screen_name:, type:), form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-volume-off.fa-fw + = t("voc.mute") diff --git a/app/views/relationships/_destroy.html.haml b/app/views/relationships/_destroy.html.haml new file mode 100644 index 00000000..00b81b93 --- /dev/null +++ b/app/views/relationships/_destroy.html.haml @@ -0,0 +1,13 @@ +- if type == "follow" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "btn btn-primary", form_class: "d-grid" do + = t("voc.unfollow") + +- if type == "block" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-minus-circle.fa-fw + = t("voc.unblock") + +- if type == "mute" + = button_to relationships_path(screen_name:, type:), method: :delete, form: { id: "#{type}-#{screen_name}" }, class: "dropdown-item" do + %i.fa.fa-volume-off.fa-fw + = t("voc.unmute") diff --git a/config/routes.rb b/config/routes.rb index 46db96a8..7e5e259c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -147,6 +147,7 @@ Rails.application.routes.draw do get "/inbox", to: "inbox#show", as: :inbox resource :subscriptions, controller: :subscriptions, only: %i[create destroy] + resource :relationships, only: %i[create destroy] get "/user/:username", to: "user#show" get "/@:username", to: "user#show", as: :user