Class: PostActionCreator
- Inherits:
-
Object
- Object
- PostActionCreator
- Defined in:
- lib/post_action_creator.rb
Defined Under Namespace
Classes: CreateResult
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(created_by, post, post_action_type_id, is_warning: false, message: nil, take_action: false, flag_topic: false, created_at: nil, queue_for_review: false, reason: nil, silent: false) ⇒ PostActionCreator
constructor
A new instance of PostActionCreator.
- #perform ⇒ Object
- #post_can_act? ⇒ Boolean
- #taken_actions ⇒ Object
Constructor Details
#initialize(created_by, post, post_action_type_id, is_warning: false, message: nil, take_action: false, flag_topic: false, created_at: nil, queue_for_review: false, reason: nil, silent: false) ⇒ PostActionCreator
Returns a new instance of PostActionCreator.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/post_action_creator.rb', line 42 def initialize( created_by, post, post_action_type_id, is_warning: false, message: nil, take_action: false, flag_topic: false, created_at: nil, queue_for_review: false, reason: nil, silent: false ) @created_by = created_by @created_at = created_at || Time.zone.now @post = post @post_action_type_id = post_action_type_id @post_action_type_view = PostActionTypeView.new @post_action_name = @post_action_type_view.types[@post_action_type_id] @is_warning = is_warning @take_action = take_action && guardian.is_staff? @message = @flag_topic = flag_topic @meta_post = nil @reason = reason @queue_for_review = queue_for_review @reason = "queued_by_staff" if reason.nil? && @queue_for_review @silent = silent end |
Class Method Details
.create(created_by, post, action_key, message: nil, created_at: nil, reason: nil, silent: false) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/post_action_creator.rb', line 10 def create( created_by, post, action_key, message: nil, created_at: nil, reason: nil, silent: false ) new( created_by, post, PostActionType.types[action_key], message: , created_at: created_at, reason: reason, silent: silent, ).perform end |
Instance Method Details
#perform ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/post_action_creator.rb', line 94 def perform result = CreateResult.new if !post_can_act? || (@queue_for_review && !guardian.is_staff?) result.forbidden = true if taken_actions&.keys&.include?(@post_action_type_view.types[@post_action_name]) result.add_error(I18n.t("action_already_performed")) else result.add_error(I18n.t("invalid_access")) end return result end PostAction.limit_action!(@created_by, @post, @post_action_type_id) reviewable = Reviewable.includes(:reviewable_scores).find_by(target: @post) if reviewable && flagging_post? && cannot_flag_again?(reviewable) result.add_error(I18n.t("reviewables.already_handled")) return result end # create meta topic / post if needed if @message.present? && (@post_action_type_view..keys | %i[spam illegal]).include?( @post_action_name, ) creator = # We need to check if the creator exists because it's possible `create_message_creator` returns nil # in the event that a `post_action_notify_user_handler` evaluated to false, haulting the post creation. if creator post = creator.create if creator.errors.present? result.add_errors_from(creator) return result end @meta_post = post end end begin post_action = create_post_action if post_action.blank? || post_action.errors.present? result.add_errors_from(post_action) else create_reviewable(result) enforce_rules UserActionManager.post_action_created(post_action) PostActionNotifier.post_action_created(post_action) if !@silent notify_subscribers # agree with other flags if @take_action && reviewable = @post.reviewable_flag result.reviewable.perform(@created_by, :agree_and_keep) post_action.try(:update_counters) end result.success = true result.post_action = post_action end rescue ActiveRecord::RecordNotUnique # If the user already performed this action, it's probably due to a different browser tab # or non-debounced clicking. We can ignore. result.success = true result.post_action = PostAction.find_by( user: @created_by, post: @post, post_action_type_id: @post_action_type_id, ) end result end |
#post_can_act? ⇒ Boolean
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/post_action_creator.rb', line 78 def post_can_act? guardian.post_can_act?( @post, @post_action_name, opts: { is_warning: @is_warning, taken_actions: taken_actions, }, ) end |
#taken_actions ⇒ Object
89 90 91 92 |
# File 'lib/post_action_creator.rb', line 89 def taken_actions return @taken_actions if defined?(@taken_actions) @taken_actions = PostAction.counts_for([@post].compact, @created_by)[@post&.id] end |