Skip to content

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

FEATURE: Add setting to disable vote limits in topic voting (#38823)

Régis Hanol224ac3145fb6
diff --git a/plugins/discourse-topic-voting/app/controllers/discourse_topic_voting/votes_controller.rb b/plugins/discourse-topic-voting/app/controllers/discourse_topic_voting/votes_controller.rb
index 1ba02306fb3..808fe58a14a 100644
--- a/plugins/discourse-topic-voting/app/controllers/discourse_topic_voting/votes_controller.rb
+++ b/plugins/discourse-topic-voting/app/controllers/discourse_topic_voting/votes_controller.rb
@@ -23,21 +23,15 @@ module DiscourseTopicVoting
voted = false
- unless current_user.reached_voting_limit?
+ if current_user.can_vote?
DiscourseTopicVoting::Vote.find_or_create_by(user: current_user, topic_id: topic_id)
topic.update_vote_count
voted = true
end
- obj = {
- can_vote: !current_user.reached_voting_limit?,
- vote_limit: current_user.vote_limit,
- vote_count: topic.topic_vote_count&.votes_count&.to_i,
- who_voted: who_voted(topic),
- alert: current_user.alert_low_votes?,
- votes_left: [(current_user.vote_limit - current_user.vote_count), 0].max,
- }
+ obj = voting_response(topic)
+ obj[:alert] = current_user.alert_low_votes?
if WebHook.active_web_hooks(:topic_upvote).exists?
payload = {
@@ -62,13 +56,7 @@ module DiscourseTopicVoting
topic.update_vote_count
- obj = {
- can_vote: !current_user.reached_voting_limit?,
- vote_limit: current_user.vote_limit,
- vote_count: topic.topic_vote_count&.votes_count&.to_i,
- who_voted: who_voted(topic),
- votes_left: [(current_user.vote_limit - current_user.vote_count), 0].max,
- }
+ obj = voting_response(topic)
if WebHook.active_web_hooks(:topic_unvote).exists?
payload = {
@@ -85,6 +73,16 @@ module DiscourseTopicVoting
protected
+ def voting_response(topic)
+ {
+ can_vote: current_user.can_vote?,
+ vote_limit: current_user.vote_limit,
+ vote_count: topic.vote_count,
+ who_voted: who_voted(topic),
+ votes_left: current_user.votes_left,
+ }
+ end
+
def who_voted(topic)
return nil unless SiteSetting.topic_voting_show_who_voted
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 475b7f05f9d..38b82c03f7e 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
@@ -29,6 +29,7 @@ export default class VoteBox extends Component {
topic.vote_count = result.vote_count;
topic.user_voted = true;
this.currentUser.votes_exceeded = !result.can_vote;
+ this.currentUser.vote_limit = result.vote_limit;
this.currentUser.votes_left = result.votes_left;
this.votesAlert = result.alert;
this.allowClick = true;
@@ -50,6 +51,7 @@ export default class VoteBox extends Component {
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;
this.allowClick = true;
})
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 5357c91c389..99906876c20 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
@@ -47,6 +47,10 @@ export default class VoteBox extends Component {
return content;
}
+ get limitsEnabled() {
+ return this.currentUser?.vote_limit != null;
+ }
+
get showVotedMenu() {
return this.hasVoted && !this.hasSeenSuccessMenu;
}
@@ -137,10 +141,14 @@ export default class VoteBox extends Component {
</dropdown.item>
<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
+ @translatedLabel={{if
+ this.limitsEnabled
+ (i18n
+ "topic_voting.see_votes"
+ count=this.currentUser.votes_left
+ max=this.currentUser.vote_limit
+ )
+ (i18n "topic_voting.see_all_votes")
}}
@href="/my/activity/votes"
@icon="check-to-slot"
@@ -168,18 +176,20 @@ export default class VoteBox extends Component {
/>
</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.limitsEnabled}}
+ <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}}
{{#if this.topic.user_voted}}
<dropdown.item class="topic-voting-menu__row">
<DButton
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 959e8eb38ac..554a09dcbbe 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
@@ -59,6 +59,7 @@ function initialize(api) {
(Superclass) =>
class extends Superclass {
@tracked votes_exceeded;
+ @tracked vote_limit;
@tracked votes_left;
}
);
diff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.yml
index e0395e298fc..65ccc5e7eaf 100755
--- a/plugins/discourse-topic-voting/config/locales/client.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml
@@ -18,6 +18,7 @@ en:
voting_closed_title: "Closed"
voting_limit: "Limit"
see_votes: "You have %{count}/%{max} votes left"
+ see_all_votes: "See your votes"
locked: "Locked"
locked_description: "Increase your trust level to vote"
votes:
diff --git a/plugins/discourse-topic-voting/config/locales/server.en.yml b/plugins/discourse-topic-voting/config/locales/server.en.yml
index 158b6619f30..3aed76e0a8f 100755
--- a/plugins/discourse-topic-voting/config/locales/server.en.yml
+++ b/plugins/discourse-topic-voting/config/locales/server.en.yml
@@ -1,6 +1,7 @@
en:
site_settings:
topic_voting_enabled: 'Allow users to vote on topics?'
+ topic_voting_enable_vote_limits: 'Limit the number of votes per user based on trust level'
topic_voting_tl0_vote_limit: 'How many active votes are TL0 users allowed?'
topic_voting_tl1_vote_limit: 'How many active votes are TL1 users allowed?'
topic_voting_tl2_vote_limit: 'How many active votes are TL2 users allowed?'
diff --git a/plugins/discourse-topic-voting/config/settings.yml b/plugins/discourse-topic-voting/config/settings.yml
index 5fbee837ca4..36b2b31a9bc 100755
--- a/plugins/discourse-topic-voting/config/settings.yml
+++ b/plugins/discourse-topic-voting/config/settings.yml
@@ -1,22 +1,43 @@
plugins:
- topic_voting_alert_votes_left:
- default: 10
topic_voting_enabled:
default: true
client: true
+ topic_voting_enable_vote_limits:
+ default: true
+ client: true
topic_voting_show_who_voted:
default: true
client: true
topic_voting_show_votes_on_profile:
default: true
client: true
+ topic_voting_alert_votes_left:
+ default: 10
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
topic_voting_tl0_vote_limit:
default: 2
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
topic_voting_tl1_vote_limit:
default: 4
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
topic_voting_tl2_vote_limit:
default: 6
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
topic_voting_tl3_vote_limit:
default: 8
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
topic_voting_tl4_vote_limit:
default: 10
+ depends_on:
+ - "topic_voting_enable_vote_limits"
+ depends_behavior: "hidden"
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 7134243cfd7..c7727cfc9a4 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
@@ -11,6 +11,7 @@ module DiscourseTopicVoting
end
def alert_low_votes?
+ return false unless SiteSetting.topic_voting_enable_vote_limits
(vote_limit - vote_count) <= SiteSetting.topic_voting_alert_votes_left
end
@@ -22,15 +23,27 @@ module DiscourseTopicVoting
self.votes.where(archive: true)
end
+ def can_vote?
+ !reached_voting_limit?
+ end
+
def reached_voting_limit?
+ return false unless SiteSetting.topic_voting_enable_vote_limits
vote_count >= vote_limit
end
def vote_limit
+ return nil unless SiteSetting.topic_voting_enable_vote_limits
SiteSetting.public_send("topic_voting_tl#{self.trust_level}_vote_limit")
end
+ def votes_left
+ 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
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index f9ad943847d..009ff28f73b 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -141,7 +141,7 @@ after_initialize do
add_to_serializer(:current_user, :votes_exceeded) { object.reached_voting_limit? }
add_to_serializer(:current_user, :votes_count) { object.vote_count }
- add_to_serializer(:current_user, :votes_left) { [object.vote_limit - object.vote_count, 0].max }
+ add_to_serializer(:current_user, :votes_left) { object.votes_left }
add_to_serializer(:current_user, :vote_limit) { object.vote_limit }
topic_votes_value_from = ->(values) do
diff --git a/plugins/discourse-topic-voting/spec/requests/votes_controller_spec.rb b/plugins/discourse-topic-voting/spec/requests/votes_controller_spec.rb
index cadd4b480fd..fbbd90a8f9a 100644
--- a/plugins/discourse-topic-voting/spec/requests/votes_controller_spec.rb
+++ b/plugins/discourse-topic-voting/spec/requests/votes_controller_spec.rb
@@ -44,6 +44,35 @@ describe DiscourseTopicVoting::VotesController do
expect(user.reload.vote_count).to eq(0)
end
+ context "when vote limits are disabled" do
+ before do
+ SiteSetting.topic_voting_enable_vote_limits = false
+ SiteSetting.public_send("topic_voting_tl#{user.trust_level}_vote_limit=", 0)
+ end
+
+ it "allows voting and returns nil for limit fields" do
+ post "/voting/vote.json", params: { topic_id: topic.id }
+ expect(response.status).to eq(200)
+
+ json = response.parsed_body
+ expect(json["can_vote"]).to eq(true)
+ expect(json["vote_limit"]).to be_nil
+ expect(json["votes_left"]).to be_nil
+ expect(json["alert"]).to eq(false)
+ end
+
+ it "returns nil for limit fields on unvote" do
+ DiscourseTopicVoting::Vote.create!(user: user, topic: topic)
+
+ post "/voting/unvote.json", params: { topic_id: topic.id }
+ expect(response.status).to eq(200)
+
+ json = response.parsed_body
+ expect(json["vote_limit"]).to be_nil
+ expect(json["votes_left"]).to be_nil
+ end
+ end
+
it "triggers a topic_upvote webhook when voting" do
Fabricate(:topic_voting_web_hook)
post "/voting/vote.json", params: { topic_id: topic.id }
diff --git a/plugins/discourse-topic-voting/spec/serializers/current_user_serializer_spec.rb b/plugins/discourse-topic-voting/spec/serializers/current_user_serializer_spec.rb
index d0b988f8928..21e0fe3b64b 100644
--- a/plugins/discourse-topic-voting/spec/serializers/current_user_serializer_spec.rb
+++ b/plugins/discourse-topic-voting/spec/serializers/current_user_serializer_spec.rb
@@ -19,6 +19,18 @@ RSpec.describe CurrentUserSerializer do
expect(json[:votes_left]).to eq(nil)
end
+ it "returns nil for vote_limit and votes_left when vote limits are disabled" do
+ SiteSetting.topic_voting_enabled = true
+ SiteSetting.topic_voting_enable_vote_limits = false
+ Fabricate(:topic_voting_votes, user: user1, topic: topic1)
+
+ json = described_class.new(user1, scope: guardian, root: false).as_json
+
+ expect(json[:vote_limit]).to be_nil
+ expect(json[:votes_left]).to be_nil
+ expect(json[:votes_exceeded]).to eq(false)
+ end
+
describe "votes_exceeded" do
it "returns false when within voting limits" do
SiteSetting.topic_voting_enabled = true
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 2f9b36caf33..afde0725aff 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
@@ -41,6 +41,14 @@ module TopicVotingTopic
def has_no_remove_vote_button?
has_no_css?("button.remove-vote")
end
+
+ def has_see_all_votes_link?
+ has_css?(".see-votes", text: I18n.t("js.topic_voting.see_all_votes"))
+ end
+
+ def has_no_votes_left_text?
+ has_no_css?(".see-votes", text: %r{\d+/\d+})
+ end
end
PageObjects::Pages::Topic.include(TopicVotingTopic)
diff --git a/plugins/discourse-topic-voting/spec/system/voting_spec.rb b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
index 7525a43d2b6..6474f653c6a 100644
--- a/plugins/discourse-topic-voting/spec/system/voting_spec.rb
+++ b/plugins/discourse-topic-voting/spec/system/voting_spec.rb
@@ -124,4 +124,41 @@ RSpec.describe "Topic voting" do
)
end
end
+
+ context "when vote limits are disabled" do
+ fab!(:voting_post) { Fabricate(:post, topic: voting_topic1) }
+ fab!(:voting_post2) { Fabricate(:post, topic: voting_topic2) }
+
+ before do
+ DiscourseTopicVoting::CategorySetting.create!(category: voting_category)
+ SiteSetting.topic_voting_enable_vote_limits = false
+ SiteSetting.topic_voting_tl4_vote_limit = 1
+ end
+
+ it "allows voting past the TL limit and hides limit UI" do
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ topic_page.vote
+ expect(topic_page).to have_see_all_votes_link
+ expect(topic_page).to have_no_votes_left_text
+ expect(topic_page.vote_count).to have_text("1")
+
+ visit("/t/#{voting_topic2.slug}/#{voting_topic2.id}")
+
+ topic_page.vote
+ expect(topic_page).to have_see_all_votes_link
+ expect(topic_page).to have_no_votes_left_text
+ expect(topic_page.vote_count).to have_text("1")
+ end
+
+ it "allows removing a vote" do
+ DiscourseTopicVoting::Vote.create!(user: admin, topic: voting_topic1)
+ voting_topic1.update_vote_count
+
+ visit("/t/#{voting_topic1.slug}/#{voting_topic1.id}")
+
+ topic_page.remove_vote
+ expect(topic_page.vote_count).to have_text("0")
+ end
+ end
end
diff --git a/plugins/discourse-topic-voting/spec/voting_spec.rb b/plugins/discourse-topic-voting/spec/voting_spec.rb
index 302f6c4b4e8..ec8d53e211d 100644
--- a/plugins/discourse-topic-voting/spec/voting_spec.rb
+++ b/plugins/discourse-topic-voting/spec/voting_spec.rb
@@ -30,6 +30,21 @@ describe DiscourseTopicVoting do
expect(user0.reached_voting_limit?).to eq(true)
end
+ it "does not enforce vote limits when vote limits are disabled" do
+ SiteSetting.topic_voting_enable_vote_limits = false
+ SiteSetting.topic_voting_tl1_vote_limit = 0
+ user0.update!(trust_level: 1)
+
+ DiscourseTopicVoting::Vote.create!(user: user0, topic: topic0)
+
+ expect(user0.vote_limit).to be_nil
+ expect(user0.votes_left).to be_nil
+ 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)