From 7e628de11021197024b89651979be4ee277771ea Mon Sep 17 00:00:00 2001 From: Angus McLeod Date: Thu, 8 Aug 2019 12:51:02 +1000 Subject: [PATCH] Fix namespace issues and improve error messages --- .../discourse/initializers/qa-edits.js.es6 | 4 +- .../discourse/widgets/qa-post.js.es6 | 30 +++++--------- config/locales/server.en.yml | 5 +-- config/settings.yml | 5 ++- ...sting_post_vote_counts_to_custom_fields.rb | 4 +- jobs/update_post_order.rb | 2 +- lib/qa.rb | 36 +++++++++-------- lib/qa_post_edits.rb | 40 +++++++++---------- lib/qa_topic_edits.rb | 27 +++++++------ 9 files changed, 75 insertions(+), 78 deletions(-) diff --git a/assets/javascripts/discourse/initializers/qa-edits.js.es6 b/assets/javascripts/discourse/initializers/qa-edits.js.es6 index df1c1b6..514e64e 100644 --- a/assets/javascripts/discourse/initializers/qa-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/qa-edits.js.es6 @@ -42,7 +42,7 @@ export default { && !model.get('reply_to_post_number') && model.get('qa_enabled')) { return helper.attach('qa-post', { - count: model.get('vote_count'), + count: model.get('qa_vote_count'), post: model }); } @@ -261,7 +261,7 @@ export default { const post = this.model; const user = this.currentUser; - post.set('topic.voted', false); + post.set('topic.qa_voted', false); let vote = { user_id: user.id, diff --git a/assets/javascripts/discourse/widgets/qa-post.js.es6 b/assets/javascripts/discourse/widgets/qa-post.js.es6 index 4c9a4f5..d3a86b2 100644 --- a/assets/javascripts/discourse/widgets/qa-post.js.es6 +++ b/assets/javascripts/discourse/widgets/qa-post.js.es6 @@ -26,18 +26,6 @@ export default createWidget('qa-post', { } const post = this.attrs.post; - const siteSettings = this.siteSettings; - - if (!post.get('topic.can_vote')) { - return bootbox.alert(I18n.t('vote.user_over_limit')); - } - - if (!siteSettings.qa_allow_multiple_votes_per_post && - post.get('topic.votes').indexOf(post.id) > -1) { - return bootbox.alert(I18n.t('vote.one_vote_per_post')); - } - - post.set('topic.voted', true); let vote = { user_id: user.id, @@ -45,14 +33,16 @@ export default createWidget('qa-post', { direction }; - castVote({ - vote - }).then(result => { - if (result.can_vote) { - post.set('topic.can_vote', result.can_vote); - } - if (result.votes) { - post.set('topic.votes', result.votes); + castVote({ vote }).then(result => { + if (result.success) { + post.set('topic.qa_voted', true); + + if (result.qa_can_vote) { + post.set('topic.qa_can_vote', result.qa_can_vote); + } + if (result.qa_votes) { + post.set('topic.qa_votes', result.qa_votes); + } } }); } diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e53b2a1..3131124 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -6,13 +6,12 @@ en: qa_disable_like_on_answers: "Disables like button on answers in QnA topics" qa_undo_vote_action_window: "Number of minutes users are allowed to undo votes in QnA topics (enter 0 for no limit)" qa_comments_default: "Maximum number of comments to display by default." - + qa_trust_level_vote_limits: "Use trust level vote limits for the question and answer plugin" qa_tl1_vote_limit: "The vote limit for the question and answer plugin for trust level 1 users" qa_tl2_vote_limit: "The vote limit for the question and answer plugin for trust level 2 users" qa_tl3_vote_limit: "The vote limit for the question and answer plugin for trust level 3 users" qa_tl4_vote_limit: "The vote limit for the question and answer plugin for trust level 4 users" - - qa_allow_multiple_votes_per_post: "Allow users to vote multiple times for the same post." + qa_tl_allow_multiple_votes_per_post: "Allow users to vote multiple times for the same post." post_action_types: vote: diff --git a/config/settings.yml b/config/settings.yml index e79654c..31e36e0 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -17,6 +17,9 @@ plugins: qa_comments_default: default: 3 client: true + qa_trust_level_vote_limits: + default: false + client: true qa_tl1_vote_limit: default: 1 client: true @@ -29,6 +32,6 @@ plugins: qa_tl4_vote_limit: default: 4 client: true - qa_allow_multiple_votes_per_post: + qa_tl_allow_multiple_votes_per_post: default: false client: true diff --git a/db/migrate/20180712004204_save_existing_post_vote_counts_to_custom_fields.rb b/db/migrate/20180712004204_save_existing_post_vote_counts_to_custom_fields.rb index 895f046..2502160 100644 --- a/db/migrate/20180712004204_save_existing_post_vote_counts_to_custom_fields.rb +++ b/db/migrate/20180712004204_save_existing_post_vote_counts_to_custom_fields.rb @@ -4,7 +4,7 @@ def up PostAction.where(post_action_type_id: 5).each do |action| if post = Post.find_by(id: action[:post_id]) - votes = post.vote_history + votes = post.qa_vote_history votes.push( "direction": QuestionAnswer::Vote::UP, @@ -32,7 +32,7 @@ def up if vote_totals.any? vote_totals.each do |k, v| if post = Post.find_by(id: k) - post.custom_fields['vote_history'] = post.vote_history.to_json + post.custom_fields['vote_history'] = post.qa_vote_history.to_json post.custom_fields['vote_count'] = v[:count].to_i post.custom_fields['voted'] = v[:voted] post.save_custom_fields(true) diff --git a/jobs/update_post_order.rb b/jobs/update_post_order.rb index d4d510e..0ae61a6 100644 --- a/jobs/update_post_order.rb +++ b/jobs/update_post_order.rb @@ -7,7 +7,7 @@ def execute(args) Topic.where(category_id: args[:category_id]).each do |topic| puts "HERE IS THE TOPIC: #{topic.title}" if qa_enabled - Topic.update_vote_order(topic.id) + Topic.qa_update_vote_order(topic.id) else topic.posts.each do |post| puts "UPDATING COLUMN: #{post.post_number}" diff --git a/lib/qa.rb b/lib/qa.rb index 83b44ad..a305134 100644 --- a/lib/qa.rb +++ b/lib/qa.rb @@ -31,18 +31,22 @@ class QuestionAnswer::VotesController < ::ApplicationController before_action :ensure_qa_enabled, only: [:create, :destroy] def create - if !Topic.can_vote(@post.topic, @user) - raise Discourse::InvalidAccess.new, I18n.t('vote.error.user_over_limit') + if !Topic.qa_can_vote(@post.topic, @user) + raise Discourse::InvalidAccess.new(nil, nil, + custom_message: 'vote.error.user_over_limit' + ) end - if !@post.can_vote(@user.id) - raise Discourse::InvalidAccess.new, I18n.t('vote.error.one_vote_per_post') + if !@post.qa_can_vote(@user.id) + raise Discourse::InvalidAccess.new(nil, nil, + custom_message: 'vote.error.one_vote_per_post' + ) end if QuestionAnswer::Vote.vote(@post, @user, vote_args) render json: success_json.merge( - votes: Topic.votes(@post.topic, @user), - can_vote: Topic.can_vote(@post.topic, @user) + qa_votes: Topic.qa_votes(@post.topic, @user), + qa_can_vote: Topic.qa_can_vote(@post.topic, @user) ) else render json: failed_json, status: 422 @@ -50,14 +54,14 @@ def create end def destroy - if Topic.votes(@post.topic, @user).length == 0 + if Topic.qa_votes(@post.topic, @user).length == 0 raise Discourse::InvalidAccess.new, I18n.t('vote.error.user_has_not_voted') end if QuestionAnswer::Vote.vote(@post, @user, vote_args) render json: success_json.merge( - votes: Topic.votes(@post.topic, @user), - can_vote: Topic.can_vote(@post.topic, @user) + qa_votes: Topic.qa_votes(@post.topic, @user), + qa_can_vote: Topic.qa_can_vote(@post.topic, @user) ) else render json: failed_json, status: 422 @@ -67,8 +71,8 @@ def destroy def voters voters = [] - if @post.voted.any? - @post.voted.each do |user_id| + if @post.qa_voted.any? + @post.qa_voted.each do |user_id| if user = User.find_by(id: user_id) voters.push(Voter.new(user)) end @@ -132,9 +136,9 @@ def self.vote(post, user, args) modifier = args[:action] === CREATE ? 1 : -1 end - post.custom_fields['vote_count'] = post.vote_count + modifier + post.custom_fields['vote_count'] = post.qa_vote_count + modifier - voted = post.voted + voted = post.qa_voted if args[:direction] === UP if args[:action] === CREATE @@ -146,7 +150,7 @@ def self.vote(post, user, args) post.custom_fields['voted'] = voted - votes = post.vote_history + votes = post.qa_vote_history votes.push( direction: args[:direction], @@ -158,7 +162,7 @@ def self.vote(post, user, args) post.custom_fields['vote_history'] = votes.to_json if post.save_custom_fields(true) - Topic.update_vote_order(post.topic) + Topic.qa_update_vote_order(post.topic) post.publish_change_to_clients! :acted true @@ -169,6 +173,6 @@ def self.vote(post, user, args) def self.can_undo(post, user) window = SiteSetting.qa_undo_vote_action_window.to_i - window === 0 || post.last_voted(user.id).to_i > window.minutes.ago.to_i + window === 0 || post.qa_last_voted(user.id).to_i > window.minutes.ago.to_i end end diff --git a/lib/qa_post_edits.rb b/lib/qa_post_edits.rb index cf3ae85..3d0e5aa 100644 --- a/lib/qa_post_edits.rb +++ b/lib/qa_post_edits.rb @@ -6,11 +6,11 @@ def actions_summary user = scope.current_user summary = { id: PostActionType.types[:vote], - count: object.vote_count + count: object.qa_vote_count } if user - voted = object.voted.include?(user.id) + voted = object.qa_voted.include?(user.id) if voted summary[:acted] = true @@ -37,8 +37,8 @@ def actions_summary class ::PostSerializer prepend PostSerializerQAExtension - attributes :vote_count, - :voted, + attributes :qa_vote_count, + :qa_voted, :qa_enabled, :last_answerer, :last_answered_at, @@ -46,12 +46,12 @@ class ::PostSerializer :last_answer_post_number, :last_answerer - def vote_count - object.vote_count + def qa_vote_count + object.qa_vote_count end - def voted - object.voted + def qa_voted + object.qa_voted end def qa_enabled @@ -99,18 +99,18 @@ def include_last_answerer? end end -## 'vote_count' and 'voted' are used for quick access, whereas 'vote_history' is used for record keeping +## 'qa_vote_count' and 'qa_voted' are used for quick access, whereas 'qa_vote_history' is used for record keeping ## See QuestionAnswer::Vote for how these fields are saved / updated Post.register_custom_field_type('vote_count', :integer) Post.register_custom_field_type('vote_history', :json) class ::Post - after_create :update_vote_order, if: :qa_enabled + after_create :qa_update_vote_order, if: :qa_enabled self.ignored_columns = %w(vote_count) - def vote_count + def qa_vote_count if custom_fields['vote_count'].present? custom_fields['vote_count'].to_i else @@ -118,7 +118,7 @@ def vote_count end end - def voted + def qa_voted if custom_fields['voted'].present? [*custom_fields['voted']].map(&:to_i) else @@ -126,7 +126,7 @@ def voted end end - def vote_history + def qa_vote_history if custom_fields['vote_history'].present? [*custom_fields['vote_history']] else @@ -138,12 +138,12 @@ def qa_enabled ::Topic.qa_enabled(topic) end - def update_vote_order - ::Topic.update_vote_order(topic_id) + def qa_update_vote_order + ::Topic.qa_update_vote_order(topic_id) end - def last_voted(user_id) - user_votes = vote_history.select do |v| + def qa_last_voted(user_id) + user_votes = qa_vote_history.select do |v| v['user_id'].to_i === user_id && v['action'] === 'create' end @@ -154,8 +154,8 @@ def last_voted(user_id) end end - def can_vote(user_id) - SiteSetting.qa_allow_multiple_votes_per_post || - !voted.include?(user_id) + def qa_can_vote(user_id) + SiteSetting.qa_tl_allow_multiple_votes_per_post || + !qa_voted.include?(user_id) end end diff --git a/lib/qa_topic_edits.rb b/lib/qa_topic_edits.rb index 0b77ee0..7832366 100644 --- a/lib/qa_topic_edits.rb +++ b/lib/qa_topic_edits.rb @@ -59,14 +59,15 @@ def last_answerer class ::Topic prepend TopicQAExtension - def self.can_vote(topic, user) - return nil if !user || !SiteSetting.qa_enabled - vote_count = self.votes(topic, user).length - vote_limit = SiteSetting.send("qa_tl#{user.trust_level}_vote_limit") - vote_limit >= vote_count + def self.qa_can_vote(topic, user) + return false if !user || !SiteSetting.qa_enabled + topic_vote_count = self.qa_votes(topic, user).length + return false if topic_vote_count > 0 && !SiteSetting.qa_trust_level_vote_limits + topic_vote_limit = SiteSetting.send("qa_tl#{user.trust_level}_vote_limit") + topic_vote_limit.to_i >= topic_vote_count end - def self.votes(topic, user) + def self.qa_votes(topic, user) return nil if !user || !SiteSetting.qa_enabled PostCustomField.where(post_id: topic.posts.map(&:id), name: 'voted', @@ -85,7 +86,7 @@ def self.qa_enabled(topic) has_qa_tag || is_qa_category || is_qa_subtype end - def self.update_vote_order(topic_id) + def self.qa_update_vote_order(topic_id) return if !SiteSetting.qa_enabled posts = Post.where(topic_id: topic_id) @@ -145,8 +146,8 @@ class TopicView require_dependency 'basic_user_serializer' class ::TopicViewSerializer attributes :qa_enabled, - :votes, - :can_vote, + :qa_votes, + :qa_can_vote, :last_answered_at, :last_commented_on, :answer_count, @@ -158,12 +159,12 @@ def qa_enabled object.qa_enabled end - def votes - Topic.votes(object.topic, scope.current_user) + def qa_votes + Topic.qa_votes(object.topic, scope.current_user) end - def can_vote - Topic.can_vote(object.topic, scope.current_user) + def qa_can_vote + Topic.qa_can_vote(object.topic, scope.current_user) end def last_answered_at