mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Enforce entropy on flag text
This commit is contained in:
parent
1635d8e553
commit
12d3c3b66b
9 changed files with 42 additions and 32 deletions
|
@ -267,19 +267,6 @@ Discourse.TopicController = Ember.ObjectController.extend Discourse.Presence,
|
||||||
actionType.loadUsers()
|
actionType.loadUsers()
|
||||||
false
|
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: ->
|
showPrivateInviteModal: ->
|
||||||
modal = Discourse.InvitePrivateModalView.create(topic: @get('content'))
|
modal = Discourse.InvitePrivateModalView.create(topic: @get('content'))
|
||||||
@get('controllers.modal')?.show(modal)
|
@get('controllers.modal')?.show(modal)
|
||||||
|
|
|
@ -28,12 +28,11 @@ window.Discourse.ActionSummary = Em.Object.extend Discourse.Presence,
|
||||||
@set('can_act', false)
|
@set('can_act', false)
|
||||||
@set('can_undo', true)
|
@set('can_undo', true)
|
||||||
|
|
||||||
#TODO: mark all other flag types as acted
|
|
||||||
|
|
||||||
# Add ourselves to the users who liked it if present
|
# Add ourselves to the users who liked it if present
|
||||||
@users.pushObject(Discourse.get('currentUser')) if @present('users')
|
@users.pushObject(Discourse.get('currentUser')) if @present('users')
|
||||||
|
|
||||||
# Create our post action
|
# Create our post action
|
||||||
|
promise = new RSVP.Promise()
|
||||||
jQuery.ajax
|
jQuery.ajax
|
||||||
url: "/post_actions",
|
url: "/post_actions",
|
||||||
type: 'POST'
|
type: 'POST'
|
||||||
|
@ -44,8 +43,9 @@ window.Discourse.ActionSummary = Em.Object.extend Discourse.Presence,
|
||||||
error: (error) =>
|
error: (error) =>
|
||||||
@removeAction()
|
@removeAction()
|
||||||
errors = jQuery.parseJSON(error.responseText).errors
|
errors = jQuery.parseJSON(error.responseText).errors
|
||||||
bootbox.alert(errors[0])
|
promise.reject(errors)
|
||||||
|
success: -> promise.resolve()
|
||||||
|
promise
|
||||||
|
|
||||||
# Undo this action
|
# Undo this action
|
||||||
undo: ->
|
undo: ->
|
||||||
|
|
|
@ -55,11 +55,11 @@ window.Discourse.ActionsHistoryView = Em.View.extend Discourse.Presence,
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if actionTypeId = $target.data('act')
|
if actionTypeId = $target.data('act')
|
||||||
@get('controller').act(@content.findProperty('id', actionTypeId))
|
@content.findProperty('id', actionTypeId).act()
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if actionTypeId = $target.data('undo')
|
if actionTypeId = $target.data('undo')
|
||||||
@get('controller').undoAction(@content.findProperty('id', actionTypeId))
|
@content.findProperty('id', actionTypeId).undo()
|
||||||
return false
|
return false
|
||||||
|
|
||||||
false
|
false
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
window.Discourse.FlagView = Ember.View.extend
|
window.Discourse.FlagView = Discourse.ModalBodyView.extend
|
||||||
templateName: 'flag'
|
templateName: 'flag'
|
||||||
title: Em.String.i18n('flagging.title')
|
title: Em.String.i18n('flagging.title')
|
||||||
|
|
||||||
|
@ -12,8 +12,9 @@ window.Discourse.FlagView = Ember.View.extend
|
||||||
|
|
||||||
createFlag: ->
|
createFlag: ->
|
||||||
actionType = Discourse.get("site").postActionTypeById(@get('postActionTypeId'))
|
actionType = Discourse.get("site").postActionTypeById(@get('postActionTypeId'))
|
||||||
@get("post.actionByName.#{actionType.get('name_key')}")?.act(message: @get('customFlagMessage'))
|
@get("post.actionByName.#{actionType.get('name_key')}")?.act(message: @get('customFlagMessage')).then ->
|
||||||
$('#discourse-modal').modal('hide')
|
$('#discourse-modal').modal('hide')
|
||||||
|
, (errors) => @displayErrors(errors)
|
||||||
false
|
false
|
||||||
|
|
||||||
customPlaceholder: (->
|
customPlaceholder: (->
|
||||||
|
|
|
@ -7,7 +7,7 @@ window.Discourse.ModalBodyView = window.Discourse.View.extend
|
||||||
|
|
||||||
# Pass the errors to our errors view
|
# Pass the errors to our errors view
|
||||||
displayErrors: (errors, callback) ->
|
displayErrors: (errors, callback) ->
|
||||||
@set('parentView.modalErrorsView.errors', errors)
|
@set('parentView.parentView.modalErrorsView.errors', errors)
|
||||||
callback?()
|
callback?()
|
||||||
|
|
||||||
# Just use jQuery to show an alert. We don't need anythign fancier for now
|
# Just use jQuery to show an alert. We don't need anythign fancier for now
|
||||||
|
|
|
@ -9,13 +9,18 @@ class PostActionsController < ApplicationController
|
||||||
id = params[:post_action_type_id].to_i
|
id = params[:post_action_type_id].to_i
|
||||||
if action = PostActionType.where(id: id).first
|
if action = PostActionType.where(id: id).first
|
||||||
guardian.ensure_post_can_act!(@post, PostActionType.Types.invert[id])
|
guardian.ensure_post_can_act!(@post, PostActionType.Types.invert[id])
|
||||||
PostAction.act(current_user, @post, action.id, params[:message])
|
|
||||||
|
|
||||||
|
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
|
# We need to reload or otherwise we are showing the old values on the front end
|
||||||
@post.reload
|
@post.reload
|
||||||
|
|
||||||
post_serializer = PostSerializer.new(@post, scope: guardian, root: false)
|
post_serializer = PostSerializer.new(@post, scope: guardian, root: false)
|
||||||
render_json_dump(post_serializer)
|
render_json_dump(post_serializer)
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
raise Discourse::InvalidParameters.new(:post_action_type_id)
|
raise Discourse::InvalidParameters.new(:post_action_type_id)
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,8 @@ class PostAction < ActiveRecord::Base
|
||||||
|
|
||||||
rate_limit :post_action_rate_limiter
|
rate_limit :post_action_rate_limiter
|
||||||
|
|
||||||
|
validate :message_quality
|
||||||
|
|
||||||
def self.update_flagged_posts_count
|
def self.update_flagged_posts_count
|
||||||
|
|
||||||
posts_flagged_count = PostAction.joins(post: :topic)
|
posts_flagged_count = PostAction.joins(post: :topic)
|
||||||
|
@ -113,6 +115,17 @@ class PostAction < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
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
|
before_create do
|
||||||
raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id,
|
raise AlreadyFlagged if is_flag? and PostAction.where(user_id: user_id,
|
||||||
post_id: post_id,
|
post_id: post_id,
|
||||||
|
|
|
@ -120,10 +120,7 @@ class Topic < ActiveRecord::Base
|
||||||
# We don't care about quality on private messages
|
# We don't care about quality on private messages
|
||||||
return if private_message?
|
return if private_message?
|
||||||
|
|
||||||
sentinel = TextSentinel.new(title,
|
sentinel = TextSentinel.title_sentinel(title)
|
||||||
min_entropy: SiteSetting.title_min_entropy,
|
|
||||||
max_word_length: SiteSetting.max_word_length,
|
|
||||||
remove_interior_spaces: true)
|
|
||||||
if sentinel.valid?
|
if sentinel.valid?
|
||||||
# It's possible the sentinel has cleaned up the title a bit
|
# It's possible the sentinel has cleaned up the title a bit
|
||||||
self.title = sentinel.text
|
self.title = sentinel.text
|
||||||
|
|
|
@ -25,6 +25,13 @@ class TextSentinel
|
||||||
end
|
end
|
||||||
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.
|
# Entropy is a number of how many unique characters the string needs.
|
||||||
def entropy
|
def entropy
|
||||||
return 0 if @text.blank?
|
return 0 if @text.blank?
|
||||||
|
|
Loading…
Reference in a new issue