Refactor flag types for more customization

This commit is contained in:
Robin Ward
2017-10-17 13:31:45 -04:00
parent e600fb79b3
commit 838568cbc3
26 changed files with 253 additions and 82 deletions

View File

@@ -411,11 +411,11 @@ class Post < ActiveRecord::Base
end
def is_flagged?
post_actions.where(post_action_type_id: PostActionType.flag_types.values, deleted_at: nil).count != 0
post_actions.where(post_action_type_id: PostActionType.flag_types_without_custom.values, deleted_at: nil).count != 0
end
def has_active_flag?
post_actions.active.where(post_action_type_id: PostActionType.flag_types.values).count != 0
post_actions.active.where(post_action_type_id: PostActionType.flag_types_without_custom.values).count != 0
end
def unhide!

View File

@@ -44,7 +44,7 @@ class PostAction < ActiveRecord::Base
def self.flag_count_by_date(start_date, end_date, category_id = nil)
result = where('post_actions.created_at >= ? AND post_actions.created_at <= ?', start_date, end_date)
result = result.where(post_action_type_id: PostActionType.flag_types.values)
result = result.where(post_action_type_id: PostActionType.flag_types_without_custom.values)
result = result.joins(post: :topic).where("topics.category_id = ?", category_id) if category_id
result.group('date(post_actions.created_at)')
.order('date(post_actions.created_at)')
@@ -164,7 +164,7 @@ SQL
if moderator.id == Discourse::SYSTEM_USER_ID
PostActionType.auto_action_flag_types.values
else
PostActionType.flag_types.values
PostActionType.flag_types_without_custom.values
end
actions = PostAction.where(post_id: post.id)
@@ -179,8 +179,13 @@ SQL
end
# reset all cached counters
f = action_type_ids.map { |t| ["#{PostActionType.types[t]}_count", 0] }
Post.with_deleted.where(id: post.id).update_all(Hash[*f.flatten])
cached = {}
action_type_ids.each do |atid|
column = "#{PostActionType.types[atid]}_count"
cached[column] = 0 if ActiveRecord::Base.connection.column_exists?(:posts, column)
end
Post.with_deleted.where(id: post.id).update_all(cached)
update_flagged_posts_count
end
@@ -188,7 +193,7 @@ SQL
def self.defer_flags!(post, moderator, delete_post = false)
actions = PostAction.active
.where(post_id: post.id)
.where(post_action_type_id: PostActionType.flag_types.values)
.where(post_action_type_id: PostActionType.flag_types_without_custom.values)
actions.each do |action|
action.deferred_at = Time.zone.now
@@ -355,7 +360,7 @@ SQL
end
def is_flag?
PostActionType.flag_types.values.include?(post_action_type_id)
!!PostActionType.flag_types[post_action_type_id]
end
def is_private_message?
@@ -387,7 +392,7 @@ SQL
end
before_create do
post_action_type_ids = is_flag? ? PostActionType.flag_types.values : post_action_type_id
post_action_type_ids = is_flag? ? PostActionType.flag_types_without_custom.values : post_action_type_id
raise AlreadyActed if PostAction.where(user_id: user_id)
.where(post_id: post_id)
.where(post_action_type_id: post_action_type_ids)
@@ -445,7 +450,9 @@ SQL
.sum("CASE WHEN users.moderator OR users.admin THEN #{SiteSetting.staff_like_weight} ELSE 1 END")
Post.where(id: post_id).update_all ["like_count = :count, like_score = :score", count: count, score: score]
else
Post.where(id: post_id).update_all ["#{column} = ?", count]
if ActiveRecord::Base.connection.column_exists?(:posts, column)
Post.where(id: post_id).update_all ["#{column} = ?", count]
end
end
topic_id = Post.with_deleted.where(id: post_id).pluck(:topic_id).first
@@ -583,7 +590,7 @@ SQL
end
def self.post_action_type_for_post(post_id)
post_action = PostAction.find_by(deferred_at: nil, post_id: post_id, post_action_type_id: PostActionType.flag_types.values, deleted_at: nil)
post_action = PostAction.find_by(deferred_at: nil, post_id: post_id, post_action_type_id: PostActionType.flag_types_without_custom.values, deleted_at: nil)
PostActionType.types[post_action.post_action_type_id]
end

View File

@@ -1,5 +1,6 @@
require_dependency 'enum'
require_dependency 'distributed_cache'
require_dependency 'flag_settings'
class PostActionType < ActiveRecord::Base
after_save :expire_cache
@@ -14,23 +15,72 @@ class PostActionType < ActiveRecord::Base
class << self
def flag_settings
unless @flag_settings
@flag_settings = FlagSettings.new
@flag_settings.add(
3,
:off_topic,
notify_type: true,
auto_action_type: true
)
@flag_settings.add(
4,
:inappropriate,
topic_type: true,
notify_type: true,
auto_action_type: true
)
@flag_settings.add(
8,
:spam,
topic_type: true,
notify_type: true,
auto_action_type: true
)
@flag_settings.add(
6,
:notify_user,
topic_type: true,
notify_type: true,
custom_type: true
)
@flag_settings.add(
7,
:notify_moderators,
topic_type: true,
notify_type: true,
custom_type: true
)
end
@flag_settings
end
def replace_flag_settings(settings)
@flag_settings = settings
@types = nil
end
def ordered
order('position asc')
end
def types
@types ||= Enum.new(bookmark: 1,
like: 2,
off_topic: 3,
inappropriate: 4,
vote: 5,
notify_user: 6,
notify_moderators: 7,
spam: 8)
unless @types
@types = Enum.new(
bookmark: 1,
like: 2,
vote: 5
)
@types.merge!(flag_settings.flag_types)
end
@types
end
def auto_action_flag_types
@auto_action_flag_types ||= flag_types.except(:notify_user, :notify_moderators)
flag_settings.auto_action_types
end
def public_types
@@ -41,17 +91,29 @@ class PostActionType < ActiveRecord::Base
@public_type_ids ||= public_types.values
end
def flag_types_without_custom
flag_settings.without_custom_types
end
def flag_types
@flag_types ||= types.only(:off_topic, :spam, :inappropriate, :notify_moderators)
flag_settings.flag_types
end
# flags resulting in mod notifications
def notify_flag_type_ids
@notify_flag_type_ids ||= types.only(:off_topic, :spam, :inappropriate, :notify_moderators).values
notify_flag_types.values
end
def notify_flag_types
flag_settings.notify_types
end
def topic_flag_types
@topic_flag_types ||= types.only(:spam, :inappropriate, :notify_moderators)
flag_settings.topic_flag_types
end
def custom_types
flag_settings.custom_types
end
def is_flag?(sym)

View File

@@ -175,7 +175,7 @@ class Report
# Post action counts:
def self.report_flags(report)
basic_report_about report, PostAction, :flag_count_by_date, report.start_date, report.end_date, report.category_id
countable = PostAction.where(post_action_type_id: PostActionType.flag_types.values)
countable = PostAction.where(post_action_type_id: PostActionType.flag_types_without_custom.values)
countable = countable.joins(post: :topic).where("topics.category_id = ?", report.category_id) if report.category_id
add_counts report, countable, 'post_actions.created_at'
end

View File

@@ -615,7 +615,7 @@ class User < ActiveRecord::Base
end
def flags_given_count
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types.values).count
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types_without_custom.values).count
end
def warnings_received_count
@@ -623,7 +623,7 @@ class User < ActiveRecord::Base
end
def flags_received_count
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types.values).count
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types_without_custom.values).count
end
def private_topics_count