Remove AJAX Mute Rules code

This commit is contained in:
Andreas Nedbal 2022-11-18 21:48:50 +01:00 committed by Andreas Nedbal
parent c08ff04738
commit 9048a3e7d8
4 changed files with 1 additions and 234 deletions

View file

@ -1,67 +0,0 @@
class Ajax::MuteRuleController < AjaxController
def create
params.require :muted_phrase
unless user_signed_in?
@response[:status] = :noauth
@response[:message] = t(".noauth")
return
end
rule = MuteRule.create!(user: current_user, muted_phrase: params[:muted_phrase])
@response[:status] = :okay
@response[:success] = true
@response[:message] = t(".success")
@response[:id] = rule.id.to_s # Returning this as a string as JavaScript defines all numbers as floats
end
def update
params.require :id
params.require :muted_phrase
unless user_signed_in?
@response[:status] = :noauth
@response[:message] = t(".noauth")
return
end
rule = MuteRule.find(params[:id])
if rule.user_id != current_user.id
@response[:status] = :nopriv
@response[:message] = t(".nopriv")
return
end
rule.muted_phrase = params[:muted_phrase]
rule.save!
@response[:status] = :okay
@response[:message] = t(".success")
@response[:success] = true
end
def destroy
params.require :id
unless user_signed_in?
@response[:status] = :noauth
@response[:message] = t(".noauth")
return
end
rule = MuteRule.find(params[:id])
if rule.user_id != current_user.id
@response[:status] = :nopriv
@response[:message] = t(".nopriv")
return
end
rule.destroy!
@response[:status] = :okay
@response[:message] = t(".success")
@response[:success] = true
end
end

View file

@ -1,12 +1,10 @@
import registerEvents from "utilities/registerEvents";
import { muteDocumentHandler } from "./mute";
import { profileHeaderChangeHandler, profilePictureChangeHandler } from "./crop";
import { themeDocumentHandler, themeSubmitHandler } from "./theme";
import { userSubmitHandler } from "./password";
import { unblockAnonymousHandler } from "./block";
export default (): void => {
muteDocumentHandler();
themeDocumentHandler();
registerEvents([

View file

@ -1,76 +0,0 @@
import { destroy, post } from '@rails/request.js';
function createSubmitEvent(
submit: HTMLButtonElement,
rulesList: HTMLDivElement,
textEntry: HTMLButtonElement,
template: HTMLTemplateElement
): (event: Event) => void {
return (event) => {
event.preventDefault();
submit.disabled = true;
post('/ajax/mute', {
body: {
muted_phrase: textEntry.value
},
contentType: 'application/json'
})
.then(async response => {
const data = await response.json;
submit.disabled = false;
if (!data.success) return;
const newEntryFragment = template.content.cloneNode(true) as Element;
newEntryFragment.querySelector<HTMLInputElement>('input').value = textEntry.value;
const newDeleteButton = newEntryFragment.querySelector<HTMLButtonElement>('button');
newDeleteButton.dataset.id = String(data.id);
newDeleteButton.onclick = createDeleteEvent(
newEntryFragment.querySelector('.form-group'),
newDeleteButton
);
rulesList.appendChild(newEntryFragment);
textEntry.value = '';
});
};
}
function createDeleteEvent(
entry: HTMLDivElement,
deleteButton: HTMLButtonElement
): () => void {
return () => {
deleteButton.disabled = true;
destroy(`/ajax/mute/${deleteButton.dataset.id}`)
.then(async response => {
const data = await response.json;
if (data.success) {
entry.parentElement.removeChild(entry)
} else {
deleteButton.disabled = false;
}
});
}
}
export function muteDocumentHandler(): void {
const submit: HTMLButtonElement = document.getElementById('new-rule-submit') as HTMLButtonElement;
if (!submit || submit.classList.contains('js-initialized')) return;
const rulesList = document.querySelector<HTMLDivElement>('.js-rules-list');
rulesList.querySelectorAll<HTMLDivElement>('.form-group:not(.js-initalized)').forEach(entry => {
const button = entry.querySelector('button')
button.onclick = createDeleteEvent(entry, button)
});
const textEntry: HTMLButtonElement = document.getElementById('new-rule-text') as HTMLButtonElement;
const template: HTMLTemplateElement = document.getElementById('rule-template') as HTMLTemplateElement;
submit.form.onsubmit = createSubmitEvent(submit, rulesList, textEntry, template)
submit.classList.add('js-initialized');
}

View file

@ -1,88 +0,0 @@
# frozen_string_literal: true
require "rails_helper"
describe Ajax::MuteRuleController, :ajax_controller, type: :controller do
describe "#create" do
subject { post(:create, params:) }
context "when user is signed in" do
before(:each) { sign_in(user) }
let(:params) { { muted_phrase: "test" } }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"id" => MuteRule.last.id.to_s,
"message" => "Rule added successfully."
}
end
it "creates a mute rule" do
expect { subject }.to change { MuteRule.count }.by(1)
expect(response).to have_http_status(:success)
rule = MuteRule.first
expect(rule.user_id).to eq(user.id)
expect(rule.muted_phrase).to eq("test")
end
include_examples "returns the expected response"
end
end
describe "#update" do
subject { post(:update, params:) }
context "when user is signed in" do
before(:each) { sign_in(user) }
let(:rule) { MuteRule.create(user:, muted_phrase: "test") }
let(:params) { { id: rule.id, muted_phrase: "dogs" } }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => "Rule updated successfully."
}
end
it "updates a mute rule" do
subject
expect(response).to have_http_status(:success)
expect(rule.reload.muted_phrase).to eq("dogs")
end
include_examples "returns the expected response"
end
end
describe "#destroy" do
subject { delete(:destroy, params:) }
context "when user is signed in" do
before(:each) { sign_in(user) }
let(:rule) { MuteRule.create(user:, muted_phrase: "test") }
let(:params) { { id: rule.id } }
let(:expected_response) do
{
"success" => true,
"status" => "okay",
"message" => "Rule deleted successfully."
}
end
it "deletes a mute rule" do
subject
expect(response).to have_http_status(:success)
expect(MuteRule.exists?(rule.id)).to eq(false)
end
include_examples "returns the expected response"
end
end
end