Add the ability to block/unblock users from profile pages

This commit is contained in:
Karina Kwiatek 2022-06-09 23:08:38 +02:00 committed by Karina Kwiatek
parent 56799f60c7
commit c173c4a17d
10 changed files with 59 additions and 12 deletions

View File

@ -3,11 +3,31 @@ import { showNotification, showErrorNotification } from 'utilities/notifications
import I18n from 'retrospring/i18n';
export function userActionHandler(event: Event): void {
event.preventDefault();
const button: HTMLButtonElement = event.target as HTMLButtonElement;
const target = button.dataset.target;
const action = button.dataset.action;
const targetURL = action === 'follow' ? '/ajax/create_relationship' : '/ajax/destroy_relationship';
let targetURL, relationshipType;
switch (action) {
case 'follow':
targetURL = '/ajax/create_relationship';
relationshipType = 'follow';
break;
case 'unfollow':
targetURL = '/ajax/destroy_relationship';
relationshipType = 'follow';
break;
case 'block':
targetURL = '/ajax/create_relationship';
relationshipType = 'block';
break;
case 'unblock':
targetURL = '/ajax/destroy_relationship';
relationshipType = 'block';
break;
}
let success = false;
Rails.ajax({
@ -15,7 +35,7 @@ export function userActionHandler(event: Event): void {
type: 'POST',
data: new URLSearchParams({
screen_name: target,
type: "follow"
type: relationshipType,
}).toString(),
success: (data) => {
success = data.success;
@ -41,6 +61,18 @@ export function userActionHandler(event: Event): void {
button.classList.remove('btn-default');
button.classList.add('btn-primary');
break;
case 'block':
button.dataset.action = 'unblock';
button.querySelector('span').innerText = I18n.translate('views.actions.unblock');
button.classList.remove('btn-primary');
button.classList.add('btn-default');
break;
case 'unblock':
button.dataset.action = 'block';
button.querySelector('span').innerText = I18n.translate('views.actions.block');
button.classList.remove('btn-default');
button.classList.add('btn-primary');
break;
}
}
});

View File

@ -5,6 +5,7 @@ import registerEvents from 'retrospring/utilities/registerEvents';
export default (): void => {
registerEvents([
{ type: 'click', target: 'button[name=user-action]', handler: userActionHandler, global: true },
{ type: 'click', target: 'a[data-action=block], a[data-action=unblock]', handler: userActionHandler, global: true },
{ type: 'click', target: 'a[data-action=report-user]', handler: userReportHandler, global: true }
]);
}

View File

@ -5,3 +5,7 @@
.fs-10 {
font-size: 10em;
}
.pe-none {
pointer-events: none;
}

View File

@ -24,9 +24,9 @@ class User
unfollow(target_user) if following?(target_user)
target_user.unfollow(self) if target_user.following?(self)
target_user.inboxes.where(question: { user_id: id }).destroy_all
inboxes.where(question: { user_id: target_user.id, author_is_anonymous: false }).destroy_all
ListMember.where(list: { user_id: target_user.id }, user_id: id).destroy_all
target_user.inboxes.joins(:question).where(question: { user_id: id }).destroy_all
inboxes.joins(:question).where(questions: { user_id: target_user.id, author_is_anonymous: false }).destroy_all
ListMember.joins(:list).where(list: { user_id: target_user.id }, user_id: id).destroy_all
create_relationship(active_block_relationships, target_user)
end

View File

@ -21,8 +21,8 @@ class User
# Follow an user
def follow(target_user)
raise Errors::FollowingSelf if target_user == self
raise Errors::FollowingOtherBlockedSelf if target_user.blocked_by?(self)
raise Errors::FollowingSelfBlockedOther if blocked_by?(target_user)
raise Errors::FollowingOtherBlockedSelf if self.blocking?(target_user)
raise Errors::FollowingSelfBlockedOther if target_user.blocking?(self)
create_relationship(active_follow_relationships, target_user)
end

View File

@ -17,6 +17,14 @@
%a.dropdown-item.d-block.d-sm-none{ href: '#', data: { target: '#modal-list-memberships', toggle: :modal } }
%i.fa.fa-list
= t 'views.actions.list'
- if current_user.blocking?(user)
%a.dropdown-item{ href: '#', data: { action: :unblock, target: user.screen_name } }
%i.fa.fa-minus-circle
%span.pe-none= t 'views.actions.unblock'
- else
%a.dropdown-item{ href: '#', data: { action: :block, target: user.screen_name } }
%i.fa.fa-minus-circle
%span.pe-none= t 'views.actions.block'
%a.dropdown-item{ href: '#', data: { action: 'report-user', target: user.screen_name } }
%i.fa.fa-exclamation-triangle
= t 'views.actions.report'

View File

@ -227,6 +227,8 @@ en:
y: "Yes"
n: "No"
remove: "Remove"
block: "Block"
unblock: "Unblock"
sessions:
destroy: "Logout"
create: "Sign in"

View File

@ -5,5 +5,5 @@ require "dry-types"
module Types
include Dry.Types()
RelationshipTypes = Types::String.enum("follow")
RelationshipTypes = Types::String.enum("follow", "block")
end

View File

@ -6,8 +6,8 @@ require "errors"
module UseCase
module Relationship
class Create < UseCase::Base
option :source_user, type: Types.Instance(::User) | Types::Coercible::String
option :target_user, type: Types.Instance(::User) | Types::Coercible::String
option :source_user, type: Types::Strict::String | Types.Instance(::User)
option :target_user, type: Types::Strict::String | Types.Instance(::User)
option :type, type: Types::RelationshipTypes
def call

View File

@ -6,8 +6,8 @@ require "errors"
module UseCase
module Relationship
class Destroy < UseCase::Base
option :source_user, type: Types.Instance(::User) | Types::Coercible::String
option :target_user, type: Types.Instance(::User) | Types::Coercible::String
option :source_user, type: Types::Strict::String | Types.Instance(::User)
option :target_user, type: Types::Strict::String | Types.Instance(::User)
option :type, type: Types::RelationshipTypes
def call