Enforce entropy on flag text

This commit is contained in:
Robin Ward 2013-02-08 16:55:40 -05:00
parent 1635d8e553
commit 12d3c3b66b
9 changed files with 42 additions and 32 deletions

View file

@ -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)

View file

@ -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: ->

View file

@ -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

View file

@ -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: (->

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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

View file

@ -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?