Class: Forem::Reaction

Inherits:
APIResource show all
Extended by:
APIOperations::Create
Defined in:
lib/forem/resources/reaction.rb

Overview

Represents a reaction (like, unicorn, exploding head, etc.) on a Forem article or comment.

Toggle user's reaction to a reactable (Article, Comment, or User). First call creates the reaction; second call removes it.

Reactions are emoji-style responses that authenticated users can add to articles or comments. The create endpoint adds a reaction, and the toggle endpoint adds it if it does not exist or removes it if it already does.

Available operations (via mixins):

- +Create+ — POST /api/reactions

Examples:

Create a reaction on an article

client.reactions.create(
  reactable_id: 12345,
  reactable_type: "Article",
  category: "like"
)

Toggle a reaction (add if absent, remove if present)

result = client.reactions.toggle(
  reactable_id: 12345,
  reactable_type: "Article",
  category: "unicorn"
)
puts result.result   # => "create" or "destroy"

See Also:

Constant Summary collapse

OBJECT_NAME =
"reaction"
RESOURCE_PATH =
"/api/reactions"

Instance Attribute Summary

Attributes inherited from ForemObject

#requestor

Class Method Summary collapse

Methods included from APIOperations::Create

create

Methods inherited from APIResource

#refresh, resource_path, #resource_url

Methods inherited from ForemObject

#==, #[], #[]=, construct_from, cursor_list, #initialize, #inspect, #method_missing, paginated_list, #respond_to_missing?, #to_hash

Methods included from APIOperations::Request

included, #request

Constructor Details

This class inherits a constructor from Forem::ForemObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Forem::ForemObject

Class Method Details

.toggle(params = {}, opts = {}) ⇒ Forem::Reaction

Toggle a reaction on a reactable object.

If the authenticated user has not yet reacted with the given category, the reaction is created. If they have already reacted, it is removed. Sends a POST request to /api/reactions/toggle.

Examples:

result = client.reactions.toggle(
  reactable_id: 42,
  reactable_type: "Article",
  category: "fire"
)
puts result.result   # => "create" or "destroy"

Parameters:

  • params (Hash) (defaults to: {})

    request body

  • opts (Hash) (defaults to: {})

    per-request options (e.g., :api_key)

Options Hash (params):

  • :category (String)

    the reaction type (e.g., "like", "readinglist")

  • :reactable_id (Integer)

    the ID of the object to react to

  • :reactable_type (String)

    One of: "Article", "Comment", "User"

Returns:

  • (Forem::Reaction)

    reaction object with a result field (++"create"++ or ++"destroy"++)

See Also:



56
57
58
59
# File 'lib/forem/resources/reaction.rb', line 56

def self.toggle(params = {}, opts = {})
  resp = request(:post, "/api/reactions/toggle", params, opts)
  construct_from(resp.parsed_body)
end