Commit 009c4f7f authored by Chantal Rollison's avatar Chantal Rollison Committed by Sean McGivern

Allows author to vote on their own issue

parent f4d18201
...@@ -109,8 +109,6 @@ export class AwardsHandler { ...@@ -109,8 +109,6 @@ export class AwardsHandler {
} }
const $menu = $(`.${this.menuClass}`); const $menu = $(`.${this.menuClass}`);
const $thumbsBtn = $menu.find('[data-name="thumbsup"], [data-name="thumbsdown"]').parent();
const $userAuthored = this.isUserAuthored($addBtn);
if ($menu.length) { if ($menu.length) {
if ($menu.is('.is-visible')) { if ($menu.is('.is-visible')) {
$addBtn.removeClass('is-active'); $addBtn.removeClass('is-active');
...@@ -134,9 +132,6 @@ export class AwardsHandler { ...@@ -134,9 +132,6 @@ export class AwardsHandler {
}, 200); }, 200);
}); });
} }
$thumbsBtn.toggleClass('disabled', $userAuthored);
$thumbsBtn.prop('disabled', $userAuthored);
} }
// Create the emoji menu with the first category of emojis. // Create the emoji menu with the first category of emojis.
...@@ -364,10 +359,6 @@ export class AwardsHandler { ...@@ -364,10 +359,6 @@ export class AwardsHandler {
return $emojiButton.hasClass('active'); return $emojiButton.hasClass('active');
} }
isUserAuthored($button) {
return $button.hasClass('js-user-authored');
}
decrementCounter($emojiButton, emoji) { decrementCounter($emojiButton, emoji) {
const counter = $('.js-counter', $emojiButton); const counter = $('.js-counter', $emojiButton);
const counterNumber = parseInt(counter.text(), 10); const counterNumber = parseInt(counter.text(), 10);
...@@ -474,20 +465,16 @@ export class AwardsHandler { ...@@ -474,20 +465,16 @@ export class AwardsHandler {
} }
postEmoji($emojiButton, awardUrl, emoji, callback) { postEmoji($emojiButton, awardUrl, emoji, callback) {
if (this.isUserAuthored($emojiButton)) { axios
this.userAuthored($emojiButton); .post(awardUrl, {
} else { name: emoji,
axios })
.post(awardUrl, { .then(({ data }) => {
name: emoji, if (data.ok) {
}) callback();
.then(({ data }) => { }
if (data.ok) { })
callback(); .catch(() => flash(__('Something went wrong on our end.')));
}
})
.catch(() => flash(__('Something went wrong on our end.')));
}
} }
findEmojiIcon(votesBlock, emoji) { findEmojiIcon(votesBlock, emoji) {
......
...@@ -82,29 +82,17 @@ export default { ...@@ -82,29 +82,17 @@ export default {
getAwardHTML(name) { getAwardHTML(name) {
return glEmojiTag(name); return glEmojiTag(name);
}, },
getAwardClassBindings(awardList, awardName) { getAwardClassBindings(awardList) {
return { return {
active: this.hasReactionByCurrentUser(awardList), active: this.hasReactionByCurrentUser(awardList),
disabled: !this.canInteractWithEmoji(awardList, awardName), disabled: !this.canInteractWithEmoji(),
}; };
}, },
canInteractWithEmoji(awardList, awardName) { canInteractWithEmoji() {
let isAllowed = true; return this.getUserData.id;
const restrictedEmojis = ['thumbsup', 'thumbsdown'];
// Users can not add :+1: and :-1: to their own notes
if (
this.getUserData.id === this.noteAuthorId &&
restrictedEmojis.indexOf(awardName) > -1
) {
isAllowed = false;
}
return this.getUserData.id && isAllowed;
}, },
hasReactionByCurrentUser(awardList) { hasReactionByCurrentUser(awardList) {
return awardList.filter(award => award.user.id === this.getUserData.id) return awardList.filter(award => award.user.id === this.getUserData.id).length;
.length;
}, },
awardTitle(awardsList) { awardTitle(awardsList) {
const hasReactionByCurrentUser = this.hasReactionByCurrentUser( const hasReactionByCurrentUser = this.hasReactionByCurrentUser(
...@@ -197,7 +185,7 @@ export default { ...@@ -197,7 +185,7 @@ export default {
v-tooltip v-tooltip
v-for="(awardList, awardName, index) in groupedAwards" v-for="(awardList, awardName, index) in groupedAwards"
:key="index" :key="index"
:class="getAwardClassBindings(awardList, awardName)" :class="getAwardClassBindings(awardList)"
:title="awardTitle(awardList)" :title="awardTitle(awardList)"
class="btn award-control" class="btn award-control"
data-boundary="viewport" data-boundary="viewport"
......
...@@ -5,7 +5,7 @@ module ToggleAwardEmoji ...@@ -5,7 +5,7 @@ module ToggleAwardEmoji
authenticate_user! authenticate_user!
name = params.require(:name) name = params.require(:name)
if awardable.user_can_award?(current_user, name) if awardable.user_can_award?(current_user)
awardable.toggle_award_emoji(name, current_user) awardable.toggle_award_emoji(name, current_user)
todoable = to_todoable(awardable) todoable = to_todoable(awardable)
......
...@@ -92,14 +92,6 @@ module IssuesHelper ...@@ -92,14 +92,6 @@ module IssuesHelper
end end
end end
def award_user_authored_class(award)
if award == 'thumbsdown' || award == 'thumbsup'
'user-authored js-user-authored'
else
''
end
end
def awards_sort(awards) def awards_sort(awards)
awards.sort_by do |award, award_emojis| awards.sort_by do |award, award_emojis|
if award == "thumbsup" if award == "thumbsup"
......
...@@ -76,12 +76,8 @@ module Awardable ...@@ -76,12 +76,8 @@ module Awardable
true true
end end
def awardable_votes?(name) def user_can_award?(current_user)
AwardEmoji::UPVOTE_NAME == name || AwardEmoji::DOWNVOTE_NAME == name Ability.allowed?(current_user, :award_emoji, self)
end
def user_can_award?(current_user, name)
awardable_by_user?(current_user, name) && Ability.allowed?(current_user, :award_emoji, self)
end end
def user_authored?(current_user) def user_authored?(current_user)
...@@ -117,12 +113,4 @@ module Awardable ...@@ -117,12 +113,4 @@ module Awardable
def normalize_name(name) def normalize_name(name)
Gitlab::Emoji.normalize_emoji_name(name) Gitlab::Emoji.normalize_emoji_name(name)
end end
def awardable_by_user?(current_user, name)
if user_authored?(current_user)
!awardable_votes?(normalize_name(name))
else
true
end
end
end end
...@@ -402,7 +402,7 @@ module QuickActions ...@@ -402,7 +402,7 @@ module QuickActions
match[1] if match match[1] if match
end end
command :award do |name| command :award do |name|
if name && issuable.user_can_award?(current_user, name) if name && issuable.user_can_award?(current_user)
@updates[:emoji_award] = name @updates[:emoji_award] = name
end end
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
.awards.js-awards-block{ class: ("hidden" if !inline && grouped_emojis.empty?), data: { award_url: toggle_award_url(awardable) } } .awards.js-awards-block{ class: ("hidden" if !inline && grouped_emojis.empty?), data: { award_url: toggle_award_url(awardable) } }
- awards_sort(grouped_emojis).each do |emoji, awards| - awards_sort(grouped_emojis).each do |emoji, awards|
%button.btn.award-control.js-emoji-btn.has-tooltip{ type: "button", %button.btn.award-control.js-emoji-btn.has-tooltip{ type: "button",
class: [(award_state_class(awardable, awards, current_user)), (award_user_authored_class(emoji) if user_authored)], class: [(award_state_class(awardable, awards, current_user))],
data: { placement: "bottom", title: award_user_list(awards, current_user) } } data: { placement: "bottom", title: award_user_list(awards, current_user) } }
= emoji_icon(emoji) = emoji_icon(emoji)
%span.award-control-text.js-counter %span.award-control-text.js-counter
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
.award-menu-holder.js-award-holder .award-menu-holder.js-award-holder
%button.btn.award-control.has-tooltip.js-add-award{ type: 'button', %button.btn.award-control.has-tooltip.js-add-award{ type: 'button',
'aria-label': _('Add reaction'), 'aria-label': _('Add reaction'),
class: ("js-user-authored" if user_authored),
data: { title: _('Add reaction'), placement: "bottom" } } data: { title: _('Add reaction'), placement: "bottom" } }
%span{ class: "award-control-icon award-control-icon-neutral" }= custom_icon('emoji_slightly_smiling_face') %span{ class: "award-control-icon award-control-icon-neutral" }= custom_icon('emoji_slightly_smiling_face')
%span{ class: "award-control-icon award-control-icon-positive" }= custom_icon('emoji_smiley') %span{ class: "award-control-icon award-control-icon-positive" }= custom_icon('emoji_smiley')
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
- if note.emoji_awardable? - if note.emoji_awardable?
- user_authored = note.user_authored?(current_user) - user_authored = note.user_authored?(current_user)
.note-actions-item .note-actions-item
= button_tag title: 'Add reaction', class: "note-action-button note-emoji-button js-add-award js-note-emoji #{'js-user-authored' if user_authored} has-tooltip btn btn-transparent", data: { position: 'right', container: 'body' } do = button_tag title: 'Add reaction', class: "note-action-button note-emoji-button js-add-award js-note-emoji} has-tooltip btn btn-transparent", data: { position: 'right', container: 'body' } do
= icon('spinner spin') = icon('spinner spin')
%span{ class: 'link-highlight award-control-icon-neutral' }= custom_icon('emoji_slightly_smiling_face') %span{ class: 'link-highlight award-control-icon-neutral' }= custom_icon('emoji_slightly_smiling_face')
%span{ class: 'link-highlight award-control-icon-positive' }= custom_icon('emoji_smiley') %span{ class: 'link-highlight award-control-icon-positive' }= custom_icon('emoji_smiley')
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
- if note.emoji_awardable? - if note.emoji_awardable?
- user_authored = note.user_authored?(current_user) - user_authored = note.user_authored?(current_user)
.note-actions-item .note-actions-item
= link_to '#', title: 'Add reaction', class: "note-action-button note-emoji-button js-add-award js-note-emoji #{'js-user-authored' if user_authored} has-tooltip", data: { position: 'right' } do = link_to '#', title: 'Add reaction', class: "note-action-button note-emoji-button js-add-award js-note-emoji has-tooltip", data: { position: 'right' } do
= icon('spinner spin') = icon('spinner spin')
%span{ class: 'link-highlight award-control-icon-neutral' }= custom_icon('emoji_slightly_smiling_face') %span{ class: 'link-highlight award-control-icon-neutral' }= custom_icon('emoji_slightly_smiling_face')
%span{ class: 'link-highlight award-control-icon-positive' }= custom_icon('emoji_smiley') %span{ class: 'link-highlight award-control-icon-positive' }= custom_icon('emoji_smiley')
......
---
title: Allow author to vote on their own issue and MRs
merge_request: 21203
author:
type: changed
...@@ -100,7 +100,7 @@ module API ...@@ -100,7 +100,7 @@ module API
end end
def can_award_awardable? def can_award_awardable?
awardable.user_can_award?(current_user, params[:name]) awardable.user_can_award?(current_user)
end end
def awardable def awardable
......
...@@ -53,21 +53,14 @@ describe Awardable do ...@@ -53,21 +53,14 @@ describe Awardable do
issue.project.add_guest(user) issue.project.add_guest(user)
end end
it 'does not allow upvoting or downvoting your own issue' do
issue.update!(author: user)
expect(issue.user_can_award?(user, AwardEmoji::DOWNVOTE_NAME)).to be_falsy
expect(issue.user_can_award?(user, AwardEmoji::UPVOTE_NAME)).to be_falsy
end
it 'is truthy when the user is allowed to award emoji' do it 'is truthy when the user is allowed to award emoji' do
expect(issue.user_can_award?(user, AwardEmoji::UPVOTE_NAME)).to be_truthy expect(issue.user_can_award?(user)).to be_truthy
end end
it 'is falsy when the project is archived' do it 'is falsy when the project is archived' do
issue.project.update!(archived: true) issue.project.update!(archived: true)
expect(issue.user_can_award?(user, AwardEmoji::UPVOTE_NAME)).to be_falsy expect(issue.user_can_award?(user)).to be_falsy
end end
end end
......
...@@ -167,12 +167,6 @@ describe API::AwardEmoji do ...@@ -167,12 +167,6 @@ describe API::AwardEmoji do
expect(response).to have_gitlab_http_status(401) expect(response).to have_gitlab_http_status(401)
end end
it "returns a 404 error if the user authored issue" do
post api("/projects/#{project.id}/issues/#{issue2.id}/award_emoji", user), name: 'thumbsup'
expect(response).to have_gitlab_http_status(404)
end
it "normalizes +1 as thumbsup award" do it "normalizes +1 as thumbsup award" do
post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: '+1' post api("/projects/#{project.id}/issues/#{issue.iid}/award_emoji", user), name: '+1'
...@@ -215,12 +209,6 @@ describe API::AwardEmoji do ...@@ -215,12 +209,6 @@ describe API::AwardEmoji do
expect(json_response['user']['username']).to eq(user.username) expect(json_response['user']['username']).to eq(user.username)
end end
it "it returns 404 error when user authored note" do
post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note2.id}/award_emoji", user), name: 'thumbsup'
expect(response).to have_gitlab_http_status(404)
end
it "normalizes +1 as thumbsup award" do it "normalizes +1 as thumbsup award" do
post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: '+1' post api("/projects/#{project.id}/issues/#{issue.iid}/notes/#{note.id}/award_emoji", user), name: '+1'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment