From ae5d8931afdef3a3827ddd2b3bd0293dae397c2c Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Sat, 13 Aug 2022 20:01:13 +0200 Subject: [PATCH] Implement frontend for blocking users site-wide --- .../features/moderation/blockAnon.ts | 44 +++++++++++++++++++ .../retrospring/features/moderation/index.ts | 6 ++- app/models/question.rb | 4 ++ app/views/answerbox/_header.haml | 4 ++ app/views/inbox/_entry.haml | 11 +++-- app/views/navigation/dropdown/_profile.haml | 3 ++ config/locales/frontend.en.yml | 4 ++ 7 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 app/javascript/retrospring/features/moderation/blockAnon.ts diff --git a/app/javascript/retrospring/features/moderation/blockAnon.ts b/app/javascript/retrospring/features/moderation/blockAnon.ts new file mode 100644 index 00000000..5d7abdbd --- /dev/null +++ b/app/javascript/retrospring/features/moderation/blockAnon.ts @@ -0,0 +1,44 @@ +import Rails from '@rails/ujs'; +import swal from 'sweetalert'; + +import { showErrorNotification, showNotification } from "utilities/notifications"; +import I18n from "retrospring/i18n"; + +export function blockAnonEventHandler(event: Event): void { + swal({ + title: I18n.translate('frontend.mod_mute.confirm.title'), + text: I18n.translate('frontend.mod_mute.confirm.text'), + type: 'warning', + showCancelButton: true, + confirmButtonColor: "#DD6B55", + confirmButtonText: I18n.translate('voc.y'), + cancelButtonText: I18n.translate('voc.n'), + closeOnConfirm: true, + }, (dialogResult) => { + if (!dialogResult) { + return; + } + + const sender: HTMLAnchorElement = event.target as HTMLAnchorElement; + + const data = { + question: sender.getAttribute('data-q-id'), + global: 'true' + }; + + Rails.ajax({ + url: '/ajax/block_anon', + type: 'POST', + data: new URLSearchParams(data).toString(), + success: (data) => { + if (!data.success) return false; + + showNotification(data.message); + }, + error: (data, status, xhr) => { + console.log(data, status, xhr); + showErrorNotification(I18n.translate('frontend.error.message')); + } + }); + }); +} \ No newline at end of file diff --git a/app/javascript/retrospring/features/moderation/index.ts b/app/javascript/retrospring/features/moderation/index.ts index 6c93249b..7d0e639a 100644 --- a/app/javascript/retrospring/features/moderation/index.ts +++ b/app/javascript/retrospring/features/moderation/index.ts @@ -2,11 +2,13 @@ import registerEvents from 'utilities/registerEvents'; import { banCheckboxHandler, banFormHandler, permanentBanCheckboxHandler } from './ban'; import { destroyReportHandler } from './destroy'; import { privilegeCheckHandler } from './privilege'; +import { blockAnonEventHandler } from './blockAnon'; export default (): void => { registerEvents([ - { type: 'click', target: '[type=checkbox][name=check-your-privileges]', handler: privilegeCheckHandler, global: true }, - { type: 'click', target: '[name=mod-delete-report]', handler: destroyReportHandler, global: true }, + { type: 'click', target: '[type="checkbox"][name="check-your-privileges"]', handler: privilegeCheckHandler, global: true }, + { type: 'click', target: '[name="mod-delete-report"]', handler: destroyReportHandler, global: true }, + { type: 'click', target: '[name="mod-block-anon"]', handler: blockAnonEventHandler, global: true }, { type: 'change', target: '[name="ban"][type="checkbox"]', handler: banCheckboxHandler, global: true }, { type: 'change', target: '[name="permaban"][type="checkbox"]', handler: permanentBanCheckboxHandler, global: true }, { type: 'submit', target: '#modal-ban form', handler: banFormHandler, global: true } diff --git a/app/models/question.rb b/app/models/question.rb index f9b8e4c3..88b5565c 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -26,4 +26,8 @@ class Question < ApplicationRecord return false if Inbox.where(question: self).count > 1 true end + + def generated? = %w[justask retrospring_exporter].include?(author_identifier) + + def anonymous? = author_is_anonymous && author_identifier.present? end diff --git a/app/views/answerbox/_header.haml b/app/views/answerbox/_header.haml index 8b9277a1..e65eb88e 100644 --- a/app/views/answerbox/_header.haml +++ b/app/views/answerbox/_header.haml @@ -18,6 +18,10 @@ %a.dropdown-item{ href: "#", tabindex: -1, data: { action: "ab-question-report", q_id: a.question.id } } %i.fa.fa-exclamation-triangle = t("voc.report") + - if current_user.has_role?(:moderator) && a.question.anonymous? && !a.question.generated? + %a.dropdown-item{ name: "mod-block-anon", data: { q_id: a.question.id } } + %i.fa.fa-volume-off + = t("voc.block_site_wide") - if current_user.has_role? :administrator %a.dropdown-item{ href: rails_admin_path_for_resource(a.question), target: "_blank" } %i.fa.fa-gears diff --git a/app/views/inbox/_entry.haml b/app/views/inbox/_entry.haml index 0bc31cde..8e788047 100644 --- a/app/views/inbox/_entry.haml +++ b/app/views/inbox/_entry.haml @@ -22,15 +22,18 @@ .dropdown-menu.dropdown-menu-right{ role: :menu } - if i.question.user_id != current_user.id %a.dropdown-item{ name: "ib-report", data: { q_id: i.question.id } } - %i.fa.fa-warning + %i.fa.fa-fw.fa-warning = t("voc.report") - - if i.question.author_is_anonymous && i.question.author_identifier.present? + - if current_user.has_role?(:moderator) && i.question.anonymous? && !i.question.generated? + %a.dropdown-item{ name: "mod-block-anon", data: { q_id: i.question.id } } + %i.fa.fa-fw.fa-volume-off + = t("voc.block_site_wide") %a.dropdown-item{ name: "ib-block-anon", data: { q_id: i.question.id } } - %i.fa.fa-minus-circle + %i.fa.fa-fw.fa-minus-circle = t("voc.block") - if current_user.has_role? :administrator %a.dropdown-item{ href: rails_admin_path_for_resource(i) } - %i.fa.fa-gears + %i.fa.fa-fw.fa-gears = t("voc.view_in_rails_admin") - if current_user == i.user .card-body diff --git a/app/views/navigation/dropdown/_profile.haml b/app/views/navigation/dropdown/_profile.haml index 20c52295..3388da00 100644 --- a/app/views/navigation/dropdown/_profile.haml +++ b/app/views/navigation/dropdown/_profile.haml @@ -30,6 +30,9 @@ = link_to moderation_toggle_unmask_path, method: :post, class: "dropdown-item" do %i.fa.fa-toggle-off = t(".unmask.enable") + %a.dropdown-item{ href: mod_anon_block_index_path } + %i.fa.fa-fw.fa-volume-off + = t("views.navigation.blocks") %a.dropdown-item{ href: moderation_path } %i.fa.fa-fw.fa-gavel = t(".moderation") diff --git a/config/locales/frontend.en.yml b/config/locales/frontend.en.yml index fd1e59ee..a0677e2d 100644 --- a/config/locales/frontend.en.yml +++ b/config/locales/frontend.en.yml @@ -45,6 +45,10 @@ en: confirm: title: "Are you sure?" text: "This question will be gone forever." + mod_mute: + confirm: + title: "Are you sure?" + text: "This will block this user for everyone." report: confirm: title: "Are you sure you want to report this %{type}?"