diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js.coffee b/app/assets/javascripts/discourse/controllers/topic_controller.js.coffee index 43c4a9b23..4f434f33b 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js.coffee +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js.coffee @@ -267,19 +267,6 @@ Discourse.TopicController = Ember.ObjectController.extend Discourse.Presence, actionType.loadUsers() false - like:(e) -> - like_action = Discourse.get('site.post_action_types').findProperty('name_key', 'like') - e.context.act(like_action.get('id')) - - # log a post action towards this post - act: (action) -> - action.act() - false - - undoAction: (action) -> - action.undo() - false - showPrivateInviteModal: -> modal = Discourse.InvitePrivateModalView.create(topic: @get('content')) @get('controllers.modal')?.show(modal) diff --git a/app/assets/javascripts/discourse/models/action_summary.js.coffee b/app/assets/javascripts/discourse/models/action_summary.js.coffee index c07eb4a93..0005dffc3 100644 --- a/app/assets/javascripts/discourse/models/action_summary.js.coffee +++ b/app/assets/javascripts/discourse/models/action_summary.js.coffee @@ -28,12 +28,11 @@ window.Discourse.ActionSummary = Em.Object.extend Discourse.Presence, @set('can_act', false) @set('can_undo', true) - #TODO: mark all other flag types as acted - # Add ourselves to the users who liked it if present @users.pushObject(Discourse.get('currentUser')) if @present('users') # Create our post action + promise = new RSVP.Promise() jQuery.ajax url: "/post_actions", type: 'POST' @@ -44,8 +43,9 @@ window.Discourse.ActionSummary = Em.Object.extend Discourse.Presence, error: (error) => @removeAction() errors = jQuery.parseJSON(error.responseText).errors - bootbox.alert(errors[0]) - + promise.reject(errors) + success: -> promise.resolve() + promise # Undo this action undo: -> diff --git a/app/assets/javascripts/discourse/views/actions_history_view.js.coffee b/app/assets/javascripts/discourse/views/actions_history_view.js.coffee index 50f6a30a6..4b31a9058 100644 --- a/app/assets/javascripts/discourse/views/actions_history_view.js.coffee +++ b/app/assets/javascripts/discourse/views/actions_history_view.js.coffee @@ -55,11 +55,11 @@ window.Discourse.ActionsHistoryView = Em.View.extend Discourse.Presence, return false if actionTypeId = $target.data('act') - @get('controller').act(@content.findProperty('id', actionTypeId)) + @content.findProperty('id', actionTypeId).act() return false if actionTypeId = $target.data('undo') - @get('controller').undoAction(@content.findProperty('id', actionTypeId)) + @content.findProperty('id', actionTypeId).undo() return false false diff --git a/app/assets/javascripts/discourse/views/flag_view.js.coffee b/app/assets/javascripts/discourse/views/flag_view.js.coffee index e73787155..fe041f52e 100644 --- a/app/assets/javascripts/discourse/views/flag_view.js.coffee +++ b/app/assets/javascripts/discourse/views/flag_view.js.coffee @@ -1,4 +1,4 @@ -window.Discourse.FlagView = Ember.View.extend +window.Discourse.FlagView = Discourse.ModalBodyView.extend templateName: 'flag' title: Em.String.i18n('flagging.title') @@ -12,8 +12,9 @@ window.Discourse.FlagView = Ember.View.extend createFlag: -> actionType = Discourse.get("site").postActionTypeById(@get('postActionTypeId')) - @get("post.actionByName.#{actionType.get('name_key')}")?.act(message: @get('customFlagMessage')) - $('#discourse-modal').modal('hide') + @get("post.actionByName.#{actionType.get('name_key')}")?.act(message: @get('customFlagMessage')).then -> + $('#discourse-modal').modal('hide') + , (errors) => @displayErrors(errors) false customPlaceholder: (-> diff --git a/app/assets/javascripts/discourse/views/modal/modal_body_view.js.coffee b/app/assets/javascripts/discourse/views/modal/modal_body_view.js.coffee index 7ce167d55..b571ff908 100644 --- a/app/assets/javascripts/discourse/views/modal/modal_body_view.js.coffee +++ b/app/assets/javascripts/discourse/views/modal/modal_body_view.js.coffee @@ -7,7 +7,7 @@ window.Discourse.ModalBodyView = window.Discourse.View.extend # Pass the errors to our errors view displayErrors: (errors, callback) -> - @set('parentView.modalErrorsView.errors', errors) + @set('parentView.parentView.modalErrorsView.errors', errors) callback?() # Just use jQuery to show an alert. We don't need anythign fancier for now diff --git a/app/controllers/post_actions_controller.rb b/app/controllers/post_actions_controller.rb index 97a5e751d..1edc1e313 100644 --- a/app/controllers/post_actions_controller.rb +++ b/app/controllers/post_actions_controller.rb @@ -9,13 +9,18 @@ class PostActionsController < ApplicationController id = params[:post_action_type_id].to_i if action = PostActionType.where(id: id).first guardian.ensure_post_can_act!(@post, PostActionType.Types.invert[id]) - PostAction.act(current_user, @post, action.id, params[:message]) - # We need to reload or otherwise we are showing the old values on the front end - @post.reload + post_action = PostAction.act(current_user, @post, action.id, params[:message]) + + if post_action.blank? or post_action.errors.present? + render_json_error(post_action) + else + # We need to reload or otherwise we are showing the old values on the front end + @post.reload + post_serializer = PostSerializer.new(@post, scope: guardian, root: false) + render_json_dump(post_serializer) + end - post_serializer = PostSerializer.new(@post, scope: guardian, root: false) - render_json_dump(post_serializer) else raise Discourse::InvalidParameters.new(:post_action_type_id) end diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 4aef3edcd..2036d9ff9 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -16,6 +16,8 @@ class PostAction < ActiveRecord::Base rate_limit :post_action_rate_limiter + validate :message_quality + def self.update_flagged_posts_count posts_flagged_count = PostAction.joins(post: :topic) @@ -113,6 +115,17 @@ class PostAction < ActiveRecord::Base end end + def message_quality + return if message.blank? + sentinel = TextSentinel.title_sentinel(message) + if sentinel.valid? + # It's possible the sentinel has cleaned up the title a bit + self.message = sentinel.text + else + errors.add(:message, I18n.t(:is_invalid)) unless sentinel.valid? + end + end + before_create do raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id, post_id: post_id, diff --git a/app/models/topic.rb b/app/models/topic.rb index 6f83b3d33..8bd8813d4 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -120,10 +120,7 @@ class Topic < ActiveRecord::Base # We don't care about quality on private messages return if private_message? - sentinel = TextSentinel.new(title, - min_entropy: SiteSetting.title_min_entropy, - max_word_length: SiteSetting.max_word_length, - remove_interior_spaces: true) + sentinel = TextSentinel.title_sentinel(title) if sentinel.valid? # It's possible the sentinel has cleaned up the title a bit self.title = sentinel.text diff --git a/lib/text_sentinel.rb b/lib/text_sentinel.rb index 3af20efd0..b081396d4 100644 --- a/lib/text_sentinel.rb +++ b/lib/text_sentinel.rb @@ -25,6 +25,13 @@ class TextSentinel end end + def self.title_sentinel(text) + TextSentinel.new(text, + min_entropy: SiteSetting.title_min_entropy, + max_word_length: SiteSetting.max_word_length, + remove_interior_spaces: true) + end + # Entropy is a number of how many unique characters the string needs. def entropy return 0 if @text.blank?