Rename symbols

This commit is contained in:
Nat 2022-11-10 17:42:32 +08:00
parent 1d81f8f3ed
commit 81bd69030b
No known key found for this signature in database
GPG Key ID: C095C891379A319E
9 changed files with 116 additions and 114 deletions

View File

@ -16,12 +16,12 @@ module DiscourseTopicVoting
topic_id = params["topic_id"].to_i
topic = Topic.find_by(id: topic_id)
raise Discourse::InvalidAccess if !topic.can_vote? || topic.user_voted?(current_user)
raise Discourse::InvalidAccess if !topic.can_topic_vote? || topic.user_topic_voted?(current_user)
guardian.ensure_can_see!(topic)
voted = false
unless current_user.reached_voting_limit?
unless current_user.reached_topic_voting_limit?
DiscourseTopicVoting::Vote.find_or_create_by(user: current_user, topic_id: topic_id)
@ -30,12 +30,12 @@ module DiscourseTopicVoting
end
obj = {
can_vote: !current_user.reached_voting_limit?,
vote_limit: current_user.vote_limit,
can_vote: !current_user.reached_topic_voting_limit?,
vote_limit: current_user.topic_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
alert: current_user.alert_low_topic_votes?,
votes_left: [(current_user.topic_vote_limit - current_user.topic_vote_count), 0].max
}
render json: obj, status: voted ? 200 : 403
@ -52,11 +52,11 @@ module DiscourseTopicVoting
topic.update_vote_count
obj = {
can_vote: !current_user.reached_voting_limit?,
vote_limit: current_user.vote_limit,
can_vote: !current_user.reached_topic_voting_limit?,
vote_limit: current_user.topic_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
votes_left: [(current_user.topic_vote_limit - current_user.vote_count), 0].max
}
render json: obj

View File

@ -8,15 +8,18 @@ module DiscourseTopicVoting
base.attribute :current_user_voted
end
def can_vote?
@can_vote ||= SiteSetting.voting_enabled && regular? && Category.can_vote?(category_id) && category && category.topic_id != id
def can_topic_vote?
@can_topic_vote ||= SiteSetting.voting_enabled && regular? && Category.can_vote?(category_id) && category && category.topic_id != id
end
def vote_count
# a little verbose but we have to call it this way as
# a topic has post votes, and
# a topic has_one :topic_vote_count
def topic_topic_vote_count
self.topic_vote_count&.votes_count.to_i
end
def user_voted?(user)
def user_topic_voted?(user)
if self.current_user_voted
self.current_user_voted == 1
else

View File

@ -39,12 +39,12 @@ after_initialize do
Topic.class_eval { prepend DiscourseTopicVoting::TopicExtension }
User.class_eval { prepend DiscourseTopicVoting::UserExtension }
add_to_serializer(:post, :can_vote, false) { object.topic&.can_vote? }
add_to_serializer(:post, :include_can_vote?) { SiteSetting.voting_enabled && object.post_number == 1 }
add_to_serializer(:post, :can_topic_vote, false) { object.topic&.can_topic_vote? }
add_to_serializer(:post, :include_can_topic_vote?) { SiteSetting.voting_enabled && object.post_number == 1 }
add_to_serializer(:topic_view, :can_vote) { object.topic.can_vote? }
add_to_serializer(:topic_view, :vote_count) { object.topic.vote_count }
add_to_serializer(:topic_view, :user_voted) { scope.user ? object.topic.user_voted?(scope.user) : false }
add_to_serializer(:topic_view, :can_topic_vote) { object.topic.can_topic_vote? }
add_to_serializer(:topic_view, :topic_vote_count) { object.topic.topic_topic_vote_count }
add_to_serializer(:topic_view, :user_topic_voted) { scope.user ? object.topic.user_topic_voted?(scope.user) : false }
if TopicQuery.respond_to?(:results_filter_callbacks)
TopicQuery.results_filter_callbacks << ->(_type, result, user, options) {
@ -73,12 +73,12 @@ after_initialize do
object.custom_fields.merge(enable_topic_voting: DiscourseTopicVoting::CategorySetting.find_by(category_id: object.id).present?)
end
add_to_serializer(:topic_list_item, :vote_count, false) { object.vote_count }
add_to_serializer(:topic_list_item, :can_vote, false) { object.can_vote? }
add_to_serializer(:topic_list_item, :user_voted, false) { object.user_voted?(scope.user) if scope.user }
add_to_serializer(:topic_list_item, :include_vote_count?) { object.can_vote? }
add_to_serializer(:topic_list_item, :include_can_vote?) { SiteSetting.voting_enabled && object.regular? }
add_to_serializer(:topic_list_item, :include_user_voted?) { object.can_vote? }
add_to_serializer(:topic_list_item, :topic_vote_count, false) { object.topic_topic_vote_count }
add_to_serializer(:topic_list_item, :can_topic_vote, false) { object.can_topic_vote? }
add_to_serializer(:topic_list_item, :user_topic_voted, false) { object.user_topic_voted?(scope.user) if scope.user }
add_to_serializer(:topic_list_item, :include_topic_vote_count?) { object.can_topic_vote? }
add_to_serializer(:topic_list_item, :include_can_topic_vote?) { SiteSetting.voting_enabled && object.regular? }
add_to_serializer(:topic_list_item, :include_user_topic_voted?) { object.can_topic_vote? }
add_to_serializer(:basic_category, :can_vote, false) { true }
add_to_serializer(:basic_category, :include_can_vote?) { Category.can_vote?(object.id) }
@ -119,34 +119,34 @@ after_initialize do
require_dependency 'user'
class ::User
def vote_count
topics_with_vote.length
def topic_vote_count
topics_with_topic_vote.length
end
def alert_low_votes?
(vote_limit - vote_count) <= SiteSetting.voting_alert_votes_left
def alert_low_topic_votes?
(topic_vote_limit - topic_vote_count) <= SiteSetting.voting_alert_votes_left
end
def topics_with_vote
def topics_with_topic_vote
self.votes.where(archive: false)
end
def topics_with_archived_vote
def topics_with_archived_topic_vote
self.votes.where(archive: true)
end
def reached_voting_limit?
vote_count >= vote_limit
def reached_topic_voting_limit?
topic_vote_count >= topic_vote_limit
end
def vote_limit
def topic_vote_limit
SiteSetting.public_send("voting_tl#{self.trust_level}_vote_limit")
end
end
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, :topic_votes_exceeded) { object.reached_topic_voting_limit? }
add_to_serializer(:current_user, :topic_votes_count) { object.topic_vote_count }
add_to_serializer(:current_user, :topic_votes_left) { [object.topic_vote_limit - object.topic_vote_count, 0].max }
require_dependency 'list_controller'
class ::ListController
@ -267,8 +267,8 @@ after_initialize do
orig.who_voted.each do |user|
next if user.blank?
user_votes = user.topics_with_vote.pluck(:topic_id)
user_archived_votes = user.topics_with_archived_vote.pluck(:topic_id)
user_votes = user.topics_with_topic_vote.pluck(:topic_id)
user_archived_votes = user.topics_with_archived_topic_vote.pluck(:topic_id)
if user_votes.include?(orig.id) || user_archived_votes.include?(orig.id)
if user_votes.include?(dest.id) || user_archived_votes.include?(dest.id)

View File

@ -23,7 +23,6 @@ describe DiscourseTopicVoting::VotesController do
end
it "can correctly show deal with voting workflow" do
SiteSetting.public_send "voting_tl#{user.trust_level}_vote_limit=", 2
post "/voting/vote.json", params: { topic_id: topic.id }
@ -31,8 +30,8 @@ describe DiscourseTopicVoting::VotesController do
post "/voting/vote.json", params: { topic_id: topic.id }
expect(response.status).to eq(403)
expect(topic.reload.vote_count).to eq(1)
expect(user.reload.vote_count).to eq(1)
expect(topic.reload.topic_topic_vote_count).to eq(1)
expect(user.reload.topic_vote_count).to eq(1)
get "/voting/who.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
@ -44,7 +43,7 @@ describe DiscourseTopicVoting::VotesController do
post "/voting/unvote.json", params: { topic_id: topic.id }
expect(response.status).to eq(200)
expect(topic.reload.vote_count).to eq(0)
expect(user.reload.vote_count).to eq(0)
expect(topic.reload.topic_topic_vote_count).to eq(0)
expect(user.reload.topic_vote_count).to eq(0)
end
end

View File

@ -14,9 +14,9 @@ RSpec.describe CurrentUserSerializer do
SiteSetting.voting_enabled = false
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(nil)
expect(json[:vote_count]).to eq(nil)
expect(json[:votes_left]).to eq(nil)
expect(json[:topic_votes_exceeded]).to eq(nil)
expect(json[:topic_vote_count]).to eq(nil)
expect(json[:topic_votes_left]).to eq(nil)
end
describe 'votes_exceeded' do
@ -27,7 +27,7 @@ RSpec.describe CurrentUserSerializer do
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(false)
expect(json[:topic_votes_exceeded]).to eq(false)
end
it 'returns true when hit voting limits' do
@ -37,7 +37,7 @@ RSpec.describe CurrentUserSerializer do
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_exceeded]).to eq(true)
expect(json[:topic_votes_exceeded]).to eq(true)
end
end
@ -47,19 +47,19 @@ RSpec.describe CurrentUserSerializer do
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(3)
expect(json[:topic_votes_left]).to eq(3)
Fabricate(:topic_voting_votes, user: user1, topic: topic1)
Fabricate(:topic_voting_votes, user: user1, topic: topic2)
Fabricate(:topic_voting_votes, user: user1, topic: topic3)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(0)
expect(json[:topic_votes_left]).to eq(0)
Fabricate(:topic_voting_votes, user: user1, topic: topic4)
json = described_class.new(user1, scope: guardian, root: false).as_json
expect(json[:votes_left]).to eq(0)
expect(json[:topic_votes_left]).to eq(0)
end
end
end

View File

@ -14,17 +14,17 @@ describe PostSerializer do
SiteSetting.voting_enabled = true
end
it "serializes can_vote for first posts only" do
it "serializes can_topic_vote for first posts only" do
post = Fabricate(:post, topic: topic)
json = PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
expect(json[:can_vote]).to eq(true)
expect(json[:can_topic_vote]).to eq(true)
post = Fabricate(:post, topic: topic)
json = PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
expect(json[:can_vote]).to eq(nil)
expect(json[:can_topic_vote]).to eq(nil)
post = Fabricate(:post)
json = PostSerializer.new(post, scope: Guardian.new(user), root: false).as_json
expect(json[:can_vote]).to eq(false)
expect(json[:can_topic_vote]).to eq(false)
end
end

View File

@ -11,18 +11,18 @@ describe TopicListItemSerializer do
json = TopicListItemSerializer.new(topic, scope: guardian, root: false).as_json
expect(json[:vote_count]).to eq nil
expect(json[:user_voted]).to eq nil
expect(json[:can_vote]).to eq nil
expect(json[:topic_vote_count]).to eq nil
expect(json[:user_topic_voted]).to eq nil
expect(json[:can_topic_vote]).to eq nil
end
it 'adds can_vote when enabled' do
SiteSetting.voting_enabled = true
json = TopicListItemSerializer.new(topic, scope: guardian, root: false).as_json
expect(json[:vote_count]).to eq nil
expect(json[:user_voted]).to eq nil
expect(json[:can_vote]).to eq false
expect(json[:topic_vote_count]).to eq nil
expect(json[:user_topic_voted]).to eq nil
expect(json[:can_topic_vote]).to eq false
end
it 'updates vote count to 0 when topic is votable' do
@ -30,9 +30,9 @@ describe TopicListItemSerializer do
DiscourseTopicVoting::CategorySetting.create!(category: category)
json = TopicListItemSerializer.new(topic, scope: guardian, root: false).as_json
expect(json[:vote_count]).to eq 0
expect(json[:user_voted]).to eq false
expect(json[:can_vote]).to eq true
expect(json[:topic_vote_count]).to eq 0
expect(json[:user_topic_voted]).to eq false
expect(json[:can_topic_vote]).to eq true
end
it "returns all the values" do
@ -42,8 +42,8 @@ describe TopicListItemSerializer do
Fabricate(:topic_voting_vote_count, topic: topic)
json = TopicListItemSerializer.new(topic, scope: guardian, root: false).as_json
expect(json[:vote_count]).to eq 1
expect(json[:user_voted]).to eq true
expect(json[:can_vote]).to eq true
expect(json[:topic_vote_count]).to eq 1
expect(json[:user_topic_voted]).to eq true
expect(json[:can_topic_vote]).to eq true
end
end

View File

@ -9,14 +9,14 @@ describe TopicViewSerializer do
let(:topic_view) { TopicView.new(topic, user) }
let(:guardian) { Guardian.new(user) }
describe 'can_vote' do
describe 'can_topic_vote' do
it 'returns nil when voting disabled' do
SiteSetting.voting_enabled = false
DiscourseTopicVoting::CategorySetting.create!(category: category)
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:can_vote]).to eq(nil)
expect(json[:can_topic_vote]).to eq(nil)
end
it 'returns false when topic not in category' do
@ -24,13 +24,13 @@ describe TopicViewSerializer do
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:can_vote]).to eq(false)
expect(json[:can_topic_vote]).to eq(false)
end
it 'returns false when voting disabled and topic not in category' do
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:can_vote]).to eq(false)
expect(json[:can_topic_vote]).to eq(false)
end
it 'returns true when voting enabled and topic in category' do
@ -39,29 +39,29 @@ describe TopicViewSerializer do
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:can_vote]).to eq(true)
expect(json[:can_topic_vote]).to eq(true)
end
end
describe 'vote_count' do
describe 'topic_vote_count' do
it 'returns the topic vote counts' do
Fabricate(:topic_voting_vote_count, topic: topic, votes_count: 3)
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:vote_count]).to eq(3)
expect(json[:topic_vote_count]).to eq(3)
end
end
describe 'user_voted' do
describe 'user_topic_voted' do
it 'returns true if the user has voted on the topic' do
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:user_voted]).to eq(false)
expect(json[:user_topic_voted]).to eq(false)
Fabricate(:topic_voting_votes, topic: topic, user: user)
json = TopicViewSerializer.new(topic_view, scope: guardian, root: false).as_json
expect(json[:user_voted]).to eq(false)
expect(json[:user_topic_voted]).to eq(false)
end
end
end

View File

@ -26,11 +26,11 @@ describe DiscourseTopicVoting do
SiteSetting.voting_tl1_vote_limit = 1
user0.update!(trust_level: 1)
expect(user0.reached_voting_limit?).to eq(false)
expect(user0.reached_topic_voting_limit?).to eq(false)
DiscourseTopicVoting::Vote.create!(user: user0, topic: topic0)
expect(user0.reached_voting_limit?).to eq(true)
expect(user0.reached_topic_voting_limit?).to eq(true)
end
context "with two topics" do
@ -57,26 +57,26 @@ describe DiscourseTopicVoting do
topic0.move_posts(Discourse.system_user, topic0.posts.pluck(:id), destination_topic_id: topic1.id)
users.each { |user| user.reload }
expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[0].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[0].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[1].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[1].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[2].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[2].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_topic_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[4].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[5].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[5].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[5].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[5].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(topic0.reload.vote_count).to eq(0)
expect(topic1.reload.vote_count).to eq(5)
expect(topic0.reload.topic_topic_vote_count).to eq(0)
expect(topic1.reload.topic_topic_vote_count).to eq(5)
merged_post = topic0.posts.find_by(action_code: 'split_topic')
expect(merged_post.raw).to include(I18n.t('topic_voting.votes_moved', count: 2))
@ -87,19 +87,19 @@ describe DiscourseTopicVoting do
topic0.move_posts(Discourse.system_user, [topic0.posts.order(:post_number).first.id], destination_topic_id: topic1.id)
users.each { |user| user.reload }
expect(users[0].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic0.id)
expect(users[0].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[1].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[1].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[2].topics_with_vote.pluck(:topic_id)).to contain_exactly(topic0.id, topic1.id)
expect(users[2].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_archived_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_archived_vote.pluck(:topic_id)).to contain_exactly(topic0.id)
expect(users[0].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic0.id)
expect(users[0].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[1].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic1.id)
expect(users[1].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[2].topics_with_topic_vote.pluck(:topic_id)).to contain_exactly(topic0.id, topic1.id)
expect(users[2].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_topic_vote.pluck(:topic_id)).to be_blank
expect(users[3].topics_with_archived_topic_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_topic_vote.pluck(:topic_id)).to be_blank
expect(users[4].topics_with_archived_topic_vote.pluck(:topic_id)).to contain_exactly(topic0.id)
expect(topic0.reload.vote_count).to eq(4)
expect(topic1.reload.vote_count).to eq(3)
expect(topic0.reload.topic_topic_vote_count).to eq(4)
expect(topic1.reload.topic_topic_vote_count).to eq(3)
end
end
@ -109,8 +109,8 @@ describe DiscourseTopicVoting do
end
it "returns a vote count of zero" do
expect(user0.vote_count).to eq (0)
expect(user0.topics_with_archived_vote.pluck(:topic_id)).to eq ([])
expect(user0.topic_vote_count).to eq (0)
expect(user0.topics_with_archived_topic_vote.pluck(:topic_id)).to eq ([])
end
end
@ -146,11 +146,11 @@ describe DiscourseTopicVoting do
DiscourseTopicVoting::Vote.create!(user: user0, topic: topic1)
topic1.reload.trash!
expect(user0.reload.topics_with_vote.pluck(:topic_id)).to eq([])
expect(user0.reload.topics_with_topic_vote.pluck(:topic_id)).to eq([])
expect(user0.notifications.count).to eq(0)
topic1.recover!
expect(user0.reload.topics_with_vote.pluck(:topic_id)).to eq([topic1.id])
expect(user0.reload.topics_with_topic_vote.pluck(:topic_id)).to eq([topic1.id])
end
end
@ -176,8 +176,8 @@ describe DiscourseTopicVoting do
Jobs::VoteReclaim.new.execute(topic_id: post1.topic_id)
user.reload
expect(user.topics_with_vote.pluck(:topic_id)).to eq([post1.topic_id])
expect(user.topics_with_archived_vote.pluck(:topic_id)).to eq([456456])
expect(user.topics_with_topic_vote.pluck(:topic_id)).to eq([post1.topic_id])
expect(user.topics_with_archived_topic_vote.pluck(:topic_id)).to eq([456456])
end
it "enqueus a job to release votes if voting is disabled for the new category" do
@ -191,8 +191,8 @@ describe DiscourseTopicVoting do
Jobs::VoteRelease.new.execute(topic_id: post0.topic_id)
user.reload
expect(user.topics_with_archived_vote.pluck(:topic_id)).to eq([post0.topic_id])
expect(user.topics_with_vote.pluck(:topic_id)).to eq([456456])
expect(user.topics_with_archived_topic_vote.pluck(:topic_id)).to eq([post0.topic_id])
expect(user.topics_with_topic_vote.pluck(:topic_id)).to eq([456456])
end
it "doesn't enqueue a job if the topic has no votes" do
@ -240,7 +240,7 @@ describe DiscourseTopicVoting do
it 'is not erroring when topic without category' do
topic1.category.destroy
expect(topic1.reload.can_vote?).to be_falsey
expect(topic1.reload.can_topic_vote?).to be_falsey
end
end
end