# Reactions

## Introduction

`Reactions` are a quick and easy way to provide feedback to `Reactable` entities via an emoji. Each [User](/api-reference/users.md) can only submit one `Reaction` per `Reactable`.

The `Reactions` of all [Users](/api-reference/users.md) are then summarized in order to obtain a quick picture of user sentiment for the `Reactable`.

## Model Definition

### Relations

| Key         | Relation                        | Type       | Relation Field(s)                |
| ----------- | ------------------------------- | ---------- | -------------------------------- |
| `user`      | [User](/api-reference/users.md) | Belongs to | `user_id`                        |
| `reactable` | `Reactable`                     | Morph to   | `reactable_type`, `reactable_id` |

### Reactable types

* `infoboardPost` - React to a [InfoboardPost](/api-reference/infoboard/infoboard-posts.md).
* `infoboardComment` - React to a [InfoboardComment](/api-reference/infoboard/infoboard-comments.md).
* `manualEntry` - React to a [ManualEntry](/api-reference/manual/manual-entries.md).
* `chatMessage` - React to a `ChatMessages`.

### Appends

The following `appends` can be applied on all `Reactable` entities:

#### `reactions_summary`

Adds the `reactions_summary` append to the response, containing the `reaction` and the `count`. The reaction counts are sorted from highest to lowest.

**Example response:**

```json
{
  "reactions_summary": [
    {
      "reaction": "👍",
      "count": 2
    },
    {
      "reaction": "👎",
      "count": 1
    }
  ]
}
```

## List by reactable

List the `Reactions` for given `Reactable`.

**Definition**

<mark style="color:green;">`GET`</mark> `/api/reactions/{reactableType}/{reactableId}`

**Example Request**

{% tabs %}
{% tab title="PHP" %}

```php
$client = new GuzzleHttp\Client(['base_uri' => 'https://{tenant}.intratool.de']);
$response = $client->request('POST', '/api/reactions/infoboardPost/1', [
    'headers' => ['Authorization' => "Bearer {accessToken}"],
]);
```

{% endtab %}
{% endtabs %}

**Example Response**

```json
[
  {
    "user_id": 1,
    "reactable_id": 1,
    "reactable_type": "infoboardPost",
    "value": "👍",
    "created_at": "2024-07-01 12:00:00",
    "updated_at": "2024-07-01 12:00:00"
  },
  {
    "user_id": 2,
    "reactable_id": 1,
    "reactable_type": "infoboardPost",
    "value": "👍",
    "created_at": "2024-07-01 13:00:00",
    "updated_at": "2024-07-01 13:00:00"
  },
  {
    "user_id": 3,
    "reactable_id": 1,
    "reactable_type": "infoboardPost",
    "reaction": "👎",
    "created_at": "2024-07-01 14:00:00",
    "updated_at": "2024-07-01 14:00:00"
  }
]
```

## Summary by reactable

Get a summary of `Reactions` for given `Reactable`. It has the same structure as the `reactions_summary` append.

**Definition**

<mark style="color:green;">`GET`</mark> `/api/reactions/{reactableType}/{reactableId}/summary`

**Example Request**

{% tabs %}
{% tab title="PHP" %}

```php
$client = new GuzzleHttp\Client(['base_uri' => 'https://{tenant}.intratool.de']);
$response = $client->request('POST', '/api/reactions/infoboardPost/1/summary', [
    'headers' => ['Authorization' => "Bearer {accessToken}"],
]);
```

{% endtab %}
{% endtabs %}

**Example Response**

```json
[
  {
    "reaction": "👍",
    "count": 2
  },
  {
    "reaction": "👎",
    "count": 1
  }
]
```

## Create or update

Create a new `Reaction`. If the reaction for given `reactable_type`, `reactable_id` and the authenticated [User's](/api-reference/users.md) ID exits, it will be updated.

**Definition**

<mark style="color:yellow;">`POST`</mark> `/api/reactions`

**Request Keys**

| Key                | Type    | Description                                               |
| ------------------ | ------- | --------------------------------------------------------- |
| `reactable_id`\*   | integer | The ID of the `Reactable` entity.                         |
| `reactable_type`\* | string  | The [Reactable type](#reactable-types) of the `Reaction`. |
| `value`\*          | string  | The emoji to react to the `Reactable` entity.             |

Keys with `*` are required.

**Example Request**

{% tabs %}
{% tab title="PHP" %}

```php
$client = new GuzzleHttp\Client(['base_uri' => 'https://{tenant}.intratool.de']);
$response = $client->request('POST', '/api/reactions', [
    'headers' => ['Authorization' => "Bearer {accessToken}"],
    'json' => [
        'reactable_id' => 1,
        'reactable_type' => 'infoboardPost',
        'value' => "👍"
    ]
]);
```

{% endtab %}
{% endtabs %}

**Example Response**

```json
{
  "id": 4,
  "reactable_id": 1,
  "reactable_type": "infoboardPost",
  "value": "👍",
  "user_id": 1,
  "created_at": "2024-07-01 15:00:00",
  "updated_at": "2024-07-01 15:00:00"
}
```

## Delete

Delete an `Reaction` by `id`.

**Definition**

<mark style="color:red;">`DELETE`</mark> `/api/reactions/{id}`

**Example Request**

{% tabs %}
{% tab title="PHP" %}

```php
$client = new GuzzleHttp\Client(['base_uri' => 'https://{tenant}.intratool.de']);
$response = $client->request('DELETE', '/api/reactions/4', [
    'headers' => ['Authorization' => "Bearer {accessToken}"]
]);
```

{% endtab %}
{% endtabs %}

**Example Response**

```json
{
  "status": "success",
  "data": []
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.api.intratool.de/api-reference/reactions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
