Skip to content

A platform for community discussion. Free, open, simple.

FIX: Show the voting notification prompt when vote limits are disabled (#41731)

Régis Hanola9d2e8272aba
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
index 963dff45acc..8aa7a11ba73 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-box.gjs
@@ -9,10 +9,10 @@ import VoteCount from "./vote-count";
export default class VoteBox extends Component {
@service currentUser;
- @action
- addVote() {
- let topic = this.args.topic;
- return ajax("/voting/vote", {
+ #sendVote(url, userVoted) {
+ const topic = this.args.topic;
+
+ return ajax(url, {
type: "POST",
data: {
topic_id: topic.id,
@@ -20,7 +20,7 @@ export default class VoteBox extends Component {
})
.then((result) => {
topic.vote_count = result.vote_count;
- topic.user_voted = true;
+ topic.user_voted = userVoted;
this.currentUser.votes_exceeded = !result.can_vote;
this.currentUser.vote_limit = result.vote_limit;
this.currentUser.votes_left = result.votes_left;
@@ -29,23 +29,13 @@ export default class VoteBox extends Component {
}
@action
- removeVote() {
- const topic = this.args.topic;
+ addVote() {
+ return this.#sendVote("/voting/vote", true);
+ }
- return ajax("/voting/unvote", {
- type: "POST",
- data: {
- topic_id: topic.id,
- },
- })
- .then((result) => {
- topic.vote_count = result.vote_count;
- topic.user_voted = false;
- this.currentUser.votes_exceeded = !result.can_vote;
- this.currentUser.vote_limit = result.vote_limit;
- this.currentUser.votes_left = result.votes_left;
- })
- .catch(popupAjaxError);
+ @action
+ removeVote() {
+ return this.#sendVote("/voting/unvote", false);
}
<template>
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
index a6ccf6e9e43..c11269b9480 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/components/vote-button.gjs
@@ -7,13 +7,13 @@ import DTooltip from "discourse/float-kit/components/d-tooltip";
import { deferAnonymousAction } from "discourse/lib/anonymous-action";
import { NotificationLevels } from "discourse/lib/notification-levels";
import { applyBehaviorTransformer } from "discourse/lib/transformer";
-import { and, eq, not } from "discourse/truth-helpers";
+import { eq } from "discourse/truth-helpers";
import DButton from "discourse/ui-kit/d-button";
import DDropdownMenu from "discourse/ui-kit/d-dropdown-menu";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
-export default class VoteBox extends Component {
+export default class VoteButton extends Component {
@service currentUser;
@tracked hasVoted = false;
@@ -41,6 +41,10 @@ export default class VoteBox extends Component {
return this.hasVoted && !this.hasSeenSuccessMenu;
}
+ get showVotedActions() {
+ return this.showVotedMenu || this.topic.user_voted;
+ }
+
get buttonClasses() {
if (this.currentUser?.vote_limit === 0) {
return "btn-default btn-small voting-wrapper__button";
@@ -84,44 +88,17 @@ export default class VoteBox extends Component {
return;
}
- // When limits are disabled and user has voted, toggle off
- if (!this.limitsEnabled && this.topic.user_voted) {
- this.args.removeVote();
- this.hasVoted = false;
- this.hasSeenSuccessMenu = false;
- return;
- }
-
- // If user has already voted and seen the success menu, don't do anything
- // The menu will show with the "remove vote" option
- if (this.topic.user_voted && this.hasSeenSuccessMenu) {
- return;
- }
-
if (
!this.topic.closed &&
!this.topic.user_voted &&
!this.currentUser.votes_exceeded
) {
this.args.addVote();
-
- // When limits are disabled, no menu needed
- if (!this.limitsEnabled) {
- return;
- }
-
this.hasVoted = true;
- // Don't set hasSeenSuccessMenu yet - it will be set when menu closes
}
});
}
- @action
- addVote() {
- this.args.addVote();
- this.hasVoted = true;
- }
-
@action
removeVote() {
this.args.removeVote();
@@ -165,7 +142,7 @@ export default class VoteBox extends Component {
{{i18n "topic_voting.voting_closed_description"}}
</:content>
</DTooltip>
- {{else if this.limitsEnabled}}
+ {{else if this.currentUser}}
<DMenu
@identifier="topic-voting-menu"
@icon={{this.buttonIcon}}
@@ -179,69 +156,27 @@ export default class VoteBox extends Component {
>
<:content>
<DDropdownMenu as |dropdown|>
- {{#if this.showVotedMenu}}
- <dropdown.item class="topic-voting-menu__votes-left">
- <DButton
- @translatedLabel={{i18n
- "topic_voting.see_votes"
- count=this.currentUser.votes_left
- max=this.currentUser.vote_limit
- }}
- @href="/my/activity/votes"
- @icon="check-to-slot"
- class="btn-transparent see-votes topic-voting-menu__row-btn"
- />
- </dropdown.item>
- <dropdown.item class="topic-voting-menu__row">
- <DButton
- @translatedLabel={{i18n "topic_voting.remove_vote"}}
- @action={{this.removeVote}}
- @icon="arrow-rotate-left"
- class="btn-transparent remove-vote topic-voting-menu__row-btn"
- />
- </dropdown.item>
- <dropdown.item class="topic-voting-menu__watch-toggle">
- <DButton
- @translatedLabel={{i18n "topic_voting.watch_topic"}}
- @action={{this.toggleWatching}}
- @icon={{if this.isWatching "toggle-on" "toggle-off"}}
- class="btn-transparent topic-voting-menu__row-btn"
- />
- </dropdown.item>
- {{else if (eq this.currentUser.vote_limit 0)}}
+ {{#if (eq this.currentUser.vote_limit 0)}}
<dropdown.item class="topic-voting-menu__title --locked">
{{dIcon "lock"}}
<span>{{i18n "topic_voting.locked_description"}}</span>
</dropdown.item>
- {{else if
- (and this.currentUser.votes_exceeded (not this.topic.user_voted))
- }}
- <dropdown.item class="topic-voting-menu__row">
- <DButton
- @translatedLabel={{i18n
- "topic_voting.see_votes"
- count=this.currentUser.votes_left
- max=this.currentUser.vote_limit
- }}
- @href="/my/activity/votes"
- @icon="check-to-slot"
- class="btn-transparent see-votes topic-voting-menu__row-btn"
- />
- </dropdown.item>
{{else}}
- <dropdown.item class="topic-voting-menu__row">
- <DButton
- @translatedLabel={{i18n
- "topic_voting.see_votes"
- count=this.currentUser.votes_left
- max=this.currentUser.vote_limit
- }}
- @href="/my/activity/votes"
- @icon="check-to-slot"
- class="btn-transparent see-votes topic-voting-menu__row-btn"
- />
- </dropdown.item>
- {{#if this.topic.user_voted}}
+ {{#if this.limitsEnabled}}
+ <dropdown.item class="topic-voting-menu__votes-left">
+ <DButton
+ @translatedLabel={{i18n
+ "topic_voting.see_votes"
+ count=this.currentUser.votes_left
+ max=this.currentUser.vote_limit
+ }}
+ @href="/my/activity/votes"
+ @icon="check-to-slot"
+ class="btn-transparent see-votes topic-voting-menu__row-btn"
+ />
+ </dropdown.item>
+ {{/if}}
+ {{#if this.showVotedActions}}
<dropdown.item class="topic-voting-menu__row">
<DButton
@translatedLabel={{i18n "topic_voting.remove_vote"}}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/discourse-topic-voting.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/discourse-topic-voting.js
index d0a14a919b5..053d8496ae6 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/discourse-topic-voting.js
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/initializers/discourse-topic-voting.js
@@ -66,9 +66,7 @@ export default {
addVotingNavItem("my_votes", "state=my_votes", {
requiresUser: true,
});
- }
- if (siteSettings.topic_voting_enabled) {
api.addSearchSuggestion("order:votes");
}
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
index 8243fc224ec..1ea05df800b 100644
--- a/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
+++ b/plugins/discourse-topic-voting/assets/javascripts/discourse/pre-initializers/extend-category-for-voting.js
@@ -4,7 +4,7 @@ import { withPluginApi } from "discourse/lib/plugin-api";
import Category from "discourse/models/category";
import { i18n } from "discourse-i18n";
-function initialize(api) {
+function extendCategory(api) {
Category.reopen({
enable_topic_voting: computed("custom_fields.enable_topic_voting", {
get() {
@@ -41,9 +41,7 @@ function initialize(api) {
buffer.push(i18n("topic_voting.votes", { count: topic.vote_count }));
buffer.push("</a>");
- if (buffer.length > 0) {
- return buffer.join("");
- }
+ return buffer.join("");
},
{ priority: -100 }
);
@@ -73,7 +71,9 @@ export default {
before: "inject-discourse-objects",
initialize() {
- withPluginApi((api) => initialize(api));
- withPluginApi((api) => api.addCategorySortCriteria("votes"));
+ withPluginApi((api) => {
+ extendCategory(api);
+ api.addCategorySortCriteria("votes");
+ });
},
};
diff --git a/plugins/discourse-topic-voting/assets/javascripts/discourse/feature-voting-route-map.js b/plugins/discourse-topic-voting/assets/javascripts/discourse/topic-voting-route-map.js
similarity index 100%
rename from plugins/discourse-topic-voting/assets/javascripts/discourse/feature-voting-route-map.js
rename to plugins/discourse-topic-voting/assets/javascripts/discourse/topic-voting-route-map.js
diff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.yml
index f3094ecb0ed..5e035ce0bb3 100755
--- a/plugins/discourse-topic-voting/config/locales/client.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml
@@ -9,29 +9,18 @@ en:
sort_options:
votes: "Votes"
topic_voting:
- category_type_ideas:
- title: "Ideas"
notification_label:
vote_released: "Vote returned"
- title: "Topic voting"
- reached_limit: "You are out of votes, remove an existing vote!"
- list_votes: "List your votes"
- votes_nav_help: "topics with the all time most votes"
hot_nav_help: "top recent topics, including those with recent votes"
voted: "You voted on this topic"
- allow_topic_voting: "Allow users to vote on topics in this category"
vote_title: "Vote"
vote_title_plural: "Votes"
voting_closed_description: "Voting is closed for this topic"
see_votes: "You have %{count}/%{max} votes left"
- locked: "Locked"
locked_description: "Increase your trust level to vote"
votes:
one: "%{count} vote"
other: "%{count} votes"
- anonymous_button:
- one: "Vote"
- other: "Votes"
remove_vote: "Remove vote"
watch_topic: "Notify me about new posts"
no_votes_yet: "No one has voted for this yet"
@@ -49,10 +38,6 @@ en:
help: "topics you voted on"
search:
most_votes: "Most Votes"
- advanced:
- vote:
- count:
- label: "Minimum Vote Count"
admin:
web_hooks:
voting_event:
diff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb
index 00f601a0f4f..4015931f2c3 100644
--- a/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb
+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/user_extension.rb
@@ -7,7 +7,7 @@ module DiscourseTopicVoting
prepended { has_many :votes, class_name: "DiscourseTopicVoting::Vote", dependent: :destroy }
def vote_count
- topics_with_vote.length
+ topics_with_vote.count
end
def alert_low_votes?
@@ -41,10 +41,5 @@ module DiscourseTopicVoting
return nil unless SiteSetting.topic_voting_enable_vote_limits
[vote_limit - vote_count, 0].max
end
-
- def vote_limit_0?
- return false unless SiteSetting.topic_voting_enable_vote_limits
- vote_limit == 0
- end
end
end
diff --git a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
index d0df51a94b9..1d85c0581f2 100644
--- a/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
+++ b/plugins/discourse-topic-voting/spec/system/page_objects/pages/topic.rb
@@ -17,12 +17,8 @@ module TopicVotingTopic
end
def remove_vote
- if SiteSetting.topic_voting_enable_vote_limits
- vote
- find("button.remove-vote").click
- else
- find(".title-voting button.voting-wrapper__button").click
- end
+ vote
+ find("button.remove-vote").click
self
end
diff --git a/plugins/discourse-topic-voting/spec/system/voting_spec.rb b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
index a653c25961d..2db761f1e94 100644
--- a/plugins/discourse-topic-voting/spec/system/voting_spec.rb
+++ b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
@@ -280,5 +280,20 @@ RSpec.describe "Topic voting" do
topic_page.remove_vote
expect(topic_page.vote_count).to have_text("0")
end
+
+ it "still offers the watch notification prompt after voting" do
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ topic_page.vote
+ expect(topic_page.vote_count).to have_text("1")
+ expect(topic_page).to have_watch_toggle_off
+ expect(page).to have_no_css(".see-votes")
+
+ topic_page.click_watch_toggle
+ expect(topic_page).to have_watch_toggle_on
+ expect(TopicUser.find_by(user: admin, topic: voting_topic1).notification_level).to eq(
+ TopicUser.notification_levels[:watching],
+ )
+ end
end
end
diff --git a/plugins/discourse-topic-voting/spec/voting_spec.rb b/plugins/discourse-topic-voting/spec/voting_spec.rb
index 7a64d178f19..edf41d36d0c 100644
--- a/plugins/discourse-topic-voting/spec/voting_spec.rb
+++ b/plugins/discourse-topic-voting/spec/voting_spec.rb
@@ -38,14 +38,12 @@ describe DiscourseTopicVoting do
expect(user0.can_vote?).to eq(true)
expect(user0.reached_voting_limit?).to eq(false)
expect(user0.alert_low_votes?).to eq(false)
- expect(user0.vote_limit_0?).to eq(false)
end
it "doesn't allow users to vote if their trust level vote limit is 0" do
SiteSetting.topic_voting_tl1_vote_limit = 0
user0.update!(trust_level: 1)
- expect(user0.vote_limit_0?).to eq(true)
expect(user0.reached_voting_limit?).to eq(true)
end