Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move relationship functionality to Turbo Streams #1421

Merged
merged 9 commits into from
Oct 28, 2023
35 changes: 0 additions & 35 deletions app/controllers/ajax/relationship_controller.rb

This file was deleted.

49 changes: 49 additions & 0 deletions app/controllers/relationships_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

class RelationshipsController < ApplicationController
include TurboStreamable

before_action :authenticate_user!

turbo_stream_actions :create, :destroy

def create
params.require :screen_name

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
115 changes: 0 additions & 115 deletions app/javascript/retrospring/features/user/action.ts

This file was deleted.

4 changes: 0 additions & 4 deletions app/javascript/retrospring/features/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { userActionHandler } from './action';
import { userReportHandler } from './report';
import registerEvents from 'retrospring/utilities/registerEvents';

export default (): void => {
registerEvents([
{ type: 'click', target: 'button[name=user-action]', handler: userActionHandler, global: true },
{ type: 'click', target: '[data-action=block], [data-action=unblock]', handler: userActionHandler, global: true },
{ type: 'click', target: '[data-action=mute], [data-action=unmute]', handler: userActionHandler, global: true },
{ type: 'click', target: 'a[data-action=report-user]', handler: userReportHandler, global: true }
]);
}
13 changes: 13 additions & 0 deletions app/views/relationships/_create.html.haml
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions app/views/relationships/_destroy.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- 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")
22 changes: 6 additions & 16 deletions app/views/user/_actions.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
- elsif user_signed_in?
.d-grid.gap-2
- if own_followings&.include?(user.id) || current_user.following?(user)
%button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :unfollow, type: type, target: user.screen_name } }
= t("voc.unfollow")
= render "relationships/destroy", type: "follow", screen_name: user.screen_name
- else
%button.btn.btn-primary{ type: :button, name: 'user-action', data: { action: :follow, type: type, target: user.screen_name } }
= t("voc.follow")
= render "relationships/create", type: "follow", screen_name: user.screen_name
.btn-group
%button.btn.btn-light.btn-sm.dropdown-toggle{ data: { bs_toggle: :dropdown }, aria: { expanded: false } }
= t(".title")
Expand All @@ -23,21 +21,13 @@
%i.fa.fa-list.fa-fw
= t(".list")
- if own_blocks&.include?(user.id) || current_user.blocking?(user)
%a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } }
%i.fa.fa-minus-circle.fa-fw
%span.pe-none= t("voc.unblock")
= render "relationships/destroy", type: "block", screen_name: user.screen_name
- else
%a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } }
%i.fa.fa-minus-circle.fa-fw
%span.pe-none= t("voc.block")
= render "relationships/create", type: "block", screen_name: user.screen_name
- if own_mutes&.include?(user.id) || current_user.muting?(user)
%a.dropdown-item{ href: '#', data: { action: :unmute, target: user.screen_name } }
%i.fa.fa-volume-off.fa-fw
%span.pe-none= t("voc.unmute")
= render "relationships/destroy", type: "mute", screen_name: user.screen_name
- else
%a.dropdown-item{ href: '#', data: { action: :mute, target: user.screen_name } }
%i.fa.fa-volume-off.fa-fw
%span.pe-none= t("voc.mute")
= render "relationships/create", type: "mute", screen_name: user.screen_name
%a.dropdown-item{ href: '#', data: { action: 'report-user', target: user.screen_name } }
%i.fa.fa-exclamation-triangle.fa-fw
= t("voc.report")
Expand Down
42 changes: 21 additions & 21 deletions config/locales/controllers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,6 @@ en:
notfound: "Question does not exist."
noauth: "You are not allowed to delete this question."
success: "Successfully deleted question."
relationship:
create:
block:
success: "Successfully blocked user."
error: "You are already blocking that user."
follow:
success: "Successfully followed user."
error: "You are already following that user."
mute:
success: "Successfully muted user."
error: "You are already muting that user."
destroy:
block:
success: "Successfully unblocked user."
error: "You are not blocking that user."
follow:
success: "Successfully unfollowed user."
error: "You are not following that user."
mute:
success: "Successfully unmuted user."
error: "You are not muting that user."
report:
create:
noauth: :ajax.noauth
Expand Down Expand Up @@ -216,6 +195,27 @@ en:
registrations:
destroy:
export_pending: "You may not delete your account while account data is currently being exported."
relationships:
create:
block:
success: "Successfully blocked user."
error: "You are already blocking that user."
follow:
success: "Successfully followed user."
error: "You are already following that user."
mute:
success: "Successfully muted user."
error: "You are already muting that user."
destroy:
block:
success: "Successfully unblocked user."
error: "You are not blocking that user."
follow:
success: "Successfully unfollowed user."
error: "You are not following that user."
mute:
success: "Successfully unmuted user."
error: "You are not muting that user."
timeline:
public:
title: "Public Timeline"
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
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
Expand Down
Loading