Add frontend for blocking anonymous users

This commit is contained in:
Karina Kwiatek 2022-06-14 23:16:55 +02:00 committed by Karina Kwiatek
parent 4e80b4f9ab
commit f379845615
5 changed files with 61 additions and 0 deletions

View file

@ -0,0 +1,24 @@
class Ajax::AnonymousBlockController < AjaxController
def create
params.require :question
question = Question.find(params[:question])
AnonymousBlock.create!(
user: current_user,
identifier: AnonymousBlock.get_identifier(question.author_identifier),
question: question,
)
question.inboxes.first.destroy
@response[:status] = :okay
@response[:message] = I18n.t('messages.block.create.okay')
@response[:success] = true
rescue Errors::Base => e
@response[:status] = e.code
@response[:message] = I18n.t(e.locale_tag)
@response[:success] = false
end
end

View file

@ -0,0 +1,30 @@
import Rails from '@rails/ujs';
import { showErrorNotification, showNotification } from "utilities/notifications";
import I18n from "retrospring/i18n";
export function blockAnonEventHandler(event: Event): void {
const element: HTMLAnchorElement = event.target as HTMLAnchorElement;
const data = {
question: element.getAttribute('data-q-id'),
};
Rails.ajax({
url: '/ajax/block_anon',
type: 'POST',
data: new URLSearchParams(data).toString(),
success: (data) => {
if (!data.success) return false;
const inboxEntry: Node = element.closest('.inbox-entry');
showNotification(data.message);
(inboxEntry as HTMLElement).remove();
},
error: (data, status, xhr) => {
console.log(data, status, xhr);
showErrorNotification(I18n.translate('frontend.error.message'));
}
});
}

View file

@ -3,12 +3,14 @@ import { answerEntryHandler, answerEntryInputHandler } from './answer';
import { deleteEntryHandler } from './delete';
import optionsEntryHandler from './options';
import { reportEventHandler } from './report';
import { blockAnonEventHandler } from "retrospring/features/inbox/entry/blockAnon";
export default (): void => {
registerEvents([
{ type: 'click', target: 'button[name="ib-answer"]', handler: answerEntryHandler, global: true },
{ type: 'click', target: '[name="ib-destroy"]', handler: deleteEntryHandler, global: true },
{ type: 'click', target: '[name=ib-report]', handler: reportEventHandler, global: true },
{ type: 'click', target: '[name=ib-block-anon]', handler: blockAnonEventHandler, global: true },
{ type: 'click', target: 'button[name=ib-options]', handler: optionsEntryHandler, global: true },
{ type: 'keydown', target: 'textarea[name=ib-answer]', handler: answerEntryInputHandler, global: true }
]);

View file

@ -22,6 +22,10 @@
%a.dropdown-item{ name: "ib-report", data: { q_id: i.question.id } }
%i.fa.fa-warning
= t("voc.report")
- if i.question.author_is_anonymous && i.question.author_identifier.present?
%a.dropdown-item{ name: "ib-block-anon", data: { q_id: i.question.id } }
%i.fa.fa-minus-circle
= t("views.actions.block")
- if current_user.has_role? :administrator
%a.dropdown-item{ href: rails_admin_path_for_resource(i) }
%i.fa.fa-gears

View file

@ -118,6 +118,7 @@ Rails.application.routes.draw do
match '/mute', to: 'mute_rule#create', via: :post, as: :create_mute_rule
match '/mute/:id', to: 'mute_rule#update', via: :post, as: :update_mute_rule
match '/mute/:id', to: 'mute_rule#destroy', via: :delete, as: :delete_mute_rule
match '/block_anon', to: 'anonymous_block#create', via: :post, as: :block_anon
end
match '/discover', to: 'discover#index', via: :get, as: :discover