# How to like posts from a plugin in a secure way

**URL:** https://meta.discourse.org/t/how-to-like-posts-from-a-plugin-in-a-secure-way/60808
**Category:** Development
**Created:** [4월 11, 2017, 9:32오후 UTC](https://meta.discourse.org/t/how-to-like-posts-from-a-plugin-in-a-secure-way/60808 "2017-04-11T21:32:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [4월 11, 2017, 9:32오후 UTC](https://meta.discourse.org/t/how-to-like-posts-from-a-plugin-in-a-secure-way/60808/1 "2017-04-11T21:32:15Z")

</div>

I’m working on allowing users to “like” posts via my telegram bot plugin (and eventually be able to post responses through telegram).

I have it working, but I want to make sure I’m implementing it in the right way to make sure it won’t allow users to bypass things like rate limiters.

I’ve taken `post_actions_controller.rb` as a reference point. From reading through the `create` method, it looks like all I need to do is extract this logic:

```plaintext
    post_action_type_id = PostActionType.types[:like]

    taken = PostAction.counts_for([@post], current_user)[@post.id]

    guardian.ensure_post_can_act!(
      @post,
      PostActionType.types[@post_action_type_id],
      is_warning: params[:is_warning],
      taken_actions: taken
    )

    post_action = PostAction.act(current_user, @post, @post_action_type_id, args)

```

And then for un-liking stuff I have this:

```plaintext
    post_action_type_id = PostActionType.types[:like]
    post_action = current_user.post_actions.find_by(post_id: params[:id].to_i, post_action_type_id: @post_action_type_id, deleted_at: nil)
    guardian.ensure_can_delete!(post_action)
    PostAction.remove_act(current_user, @post, post_action.post_action_type_id)

```

Is that what I should be doing, or are there some simpler methods I can call which take care of all of the `Guardian` stuff?

On a related note, is there any way for my plugin to run logic whenever a “like” occurs? I can’t find a relavent DiscourseEvent, but I may be missing something.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [4월 11, 2017, 10:22오후 UTC](https://meta.discourse.org/t/how-to-like-posts-from-a-plugin-in-a-secure-way/60808/2 "2017-04-11T22:22:43Z")

</div>

After some more investigation I’ve found `lib/post_action_creator` which seems ideal. However, I can’t find an equivalent to “un-like” a post. Would that be PR material?

> <https://github.com/discourse/discourse/blob/main/lib/post_action_creator.rb>
