discourse/lib/post_action_destroyer.rb
Blake Erickson 367de2594d
FIX: Unlike own posts on ownership transfer (#10446)
* FIX: Unlike own posts on ownership transfer

If a user has liked a post that has passed the
`post_undo_action_window_mins` system setting window and you transfer ownership
of that post to that user you will be the owner of a post that you have
liked, but cannot unlike resulting in a weird UI behavior. This commit
fixes this issue.

The existing tests didn't check for the timeout window for unliking
posts so I added that in.

I couldn't find a good way to do this logic inside of the guardian class
so rather than duplicating behavior of the `PostActionDestroyer` class
inside of the `PostOwnerChanger` I decided to pass in a "bypass"
variable that could be used to check if the calling class is the
'post_owner_changer' and bypass the guardian instead. I went this route
because the guardian `can_delete_post_action` method has no way of
distinguishing how to allow a user to be able to unlike their own posts
after the timeout window but only on a post owner change.

* use an options hash instead
2020-08-19 09:21:02 -06:00

64 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class PostActionDestroyer
class DestroyResult < PostActionResult
attr_accessor :post
end
def initialize(destroyed_by, post, post_action_type_id, opts = {})
@destroyed_by, @post, @post_action_type_id, @opts = destroyed_by, post, post_action_type_id, opts
end
def self.destroy(destroyed_by, post, action_key, opts = {})
new(destroyed_by, post, PostActionType.types[action_key], opts).perform
end
def perform
result = DestroyResult.new
if @post.blank?
result.not_found = true
return result
end
finder = PostAction.where(
user: @destroyed_by,
post: @post,
post_action_type_id: @post_action_type_id
)
finder = finder.with_deleted if @destroyed_by.staff?
post_action = finder.first
if post_action.blank?
result.not_found = true
return result
end
unless @opts[:skip_delete_check] == true || guardian.can_delete?(post_action)
result.forbidden = true
result.add_error(I18n.t("invalid_access"))
return result
end
RateLimiter.new(@destroyed_by, "post_action-#{@post.id}_#{@post_action_type_id}", 4, 1.minute).performed!
post_action.remove_act!(@destroyed_by)
post_action.post.unhide! if post_action.staff_took_action
GivenDailyLike.decrement_for(@destroyed_by.id) if @post_action_type_id == PostActionType.types[:like]
UserActionManager.post_action_destroyed(post_action)
PostActionNotifier.post_action_deleted(post_action)
result.success = true
result.post = @post.reload
result
end
protected
def guardian
@guardian ||= Guardian.new(@destroyed_by)
end
end