diff --git a/app/javascript/retrospring/features/user/action.ts b/app/javascript/retrospring/features/user/action.ts index bbac9b08..3774091b 100644 --- a/app/javascript/retrospring/features/user/action.ts +++ b/app/javascript/retrospring/features/user/action.ts @@ -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; } } }); diff --git a/app/javascript/retrospring/features/user/index.ts b/app/javascript/retrospring/features/user/index.ts index 26894e3c..9045088e 100644 --- a/app/javascript/retrospring/features/user/index.ts +++ b/app/javascript/retrospring/features/user/index.ts @@ -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 } ]); } \ No newline at end of file diff --git a/app/javascript/styles/_utilities.scss b/app/javascript/styles/_utilities.scss index dff5ef14..e1a5cb5d 100644 --- a/app/javascript/styles/_utilities.scss +++ b/app/javascript/styles/_utilities.scss @@ -5,3 +5,7 @@ .fs-10 { font-size: 10em; } + +.pe-none { + pointer-events: none; +} diff --git a/app/models/user/relationship/block.rb b/app/models/user/relationship/block.rb index 7cb2cdd3..6ed16347 100644 --- a/app/models/user/relationship/block.rb +++ b/app/models/user/relationship/block.rb @@ -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 diff --git a/app/models/user/relationship/follow.rb b/app/models/user/relationship/follow.rb index fb30325b..4b2fc9d7 100644 --- a/app/models/user/relationship/follow.rb +++ b/app/models/user/relationship/follow.rb @@ -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 diff --git a/app/views/user/_actions.haml b/app/views/user/_actions.haml index 51411d9e..1dd4cc45 100644 --- a/app/views/user/_actions.haml +++ b/app/views/user/_actions.haml @@ -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' diff --git a/config/locales/en.yml b/config/locales/en.yml index 8aabd24e..e6624fc6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -227,6 +227,8 @@ en: y: "Yes" n: "No" remove: "Remove" + block: "Block" + unblock: "Unblock" sessions: destroy: "Logout" create: "Sign in" diff --git a/lib/types.rb b/lib/types.rb index 267760a2..fe869647 100644 --- a/lib/types.rb +++ b/lib/types.rb @@ -5,5 +5,5 @@ require "dry-types" module Types include Dry.Types() - RelationshipTypes = Types::String.enum("follow") + RelationshipTypes = Types::String.enum("follow", "block") end diff --git a/lib/use_case/relationship/create.rb b/lib/use_case/relationship/create.rb index 0918d226..9418d273 100644 --- a/lib/use_case/relationship/create.rb +++ b/lib/use_case/relationship/create.rb @@ -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 diff --git a/lib/use_case/relationship/destroy.rb b/lib/use_case/relationship/destroy.rb index 6e04e89a..c41c61f1 100644 --- a/lib/use_case/relationship/destroy.rb +++ b/lib/use_case/relationship/destroy.rb @@ -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