Class: PostActionDestroyer

Inherits:
Object
  • Object
show all
Defined in:
lib/post_action_destroyer.rb

Defined Under Namespace

Classes: DestroyResult

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(destroyed_by, post, post_action_type_id, opts = {}) ⇒ PostActionDestroyer

Returns a new instance of PostActionDestroyer.



8
9
10
11
12
13
14
# File 'lib/post_action_destroyer.rb', line 8

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

Class Method Details

.destroy(destroyed_by, post, action_key, opts = {}) ⇒ Object



16
17
18
# File 'lib/post_action_destroyer.rb', line 16

def self.destroy(destroyed_by, post, action_key, opts = {})
  new(destroyed_by, post, PostActionType.types[action_key], opts).perform
end

Instance Method Details

#performObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# File 'lib/post_action_destroyer.rb', line 20

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
  if @post_action_type_id == PostActionType.types[:like]
    GivenDailyLike.decrement_for(@destroyed_by.id)
  end

  case @post_action_type_id
  when *PostActionType.notify_flag_type_ids
    DiscourseEvent.trigger(:flag_destroyed, post_action, self)
  when PostActionType.types[:like]
    DiscourseEvent.trigger(:like_destroyed, post_action, self)
  end

  UserActionManager.post_action_destroyed(post_action)
  PostActionNotifier.post_action_deleted(post_action)
  result.success = true
  result.post = @post.reload

  notify_subscribers

  result
end