# Notification Message Types

## Notification Message Types Documentation

This document describes the two primary notification message types (`ImageGenerationTaskCompleted` and `ImageGenerationProgress`) used in the AdCreative system. These messages can be consumed via **WebHook HTTP endpoints** or **RabbitMQ queues**.

### Overview of Notification Delivery Mechanisms

1. **WebHook HTTP Endpoint**:\
   Messages are sent as HTTP POST requests to a specified endpoint. The message payload is serialized in JSON format.
2. **RabbitMQ Queue**:\
   Messages are delivered to the specified RabbitMQ queue in a serialized binary or JSON format.

***

### Notification Message Types

#### Common Structure

All notification message types share the same structure, with the only difference being the `MessageType` field.

#### List of Message Types

* `ImageGenerationProgress`
* `ImageUpscalingProgress`
* `ImageFaceEnhancingProgress`
* `ImageBackgroundRemovingProgress`
* `ImageStockGenerationProgress`
* `ImageGenerationTaskCompleted`
* `ImageUpscalingTaskCompleted`
* `ImageFaceEnhancingTaskCompleted`
* `ImageBackgroundRemovingTaskCompleted`
* `ImageStockGenerationTaskCompleted`

The `MessageType` field specifies the type of notification being sent.

***

#### 1. **ImageGenerationTaskCompleted**

**Description:**

This message is sent when a single image generation task is completed, indicating whether the task was successful and including the image content if applicable.

**WebHook JSON Example**

```json
{
  "MessageType": "ImageGenerationTaskCompleted",
  "ApplicationId": "d5eabbb8-c0f6-4640-94fd-3566edad499f",
  "UserId": "2581c740-8e9d-4d63-96bc-5665bb9a646b",
  "ImageRenderProcessId": "51d690da-f5ea-405e-90ff-10d56b0d633f",
  "TaskId": "9baa8e55-ddc8-4940-907f-eb5fc81e53f6",
  "ImageContentBytes": "base64encodedimagecontent==",
  "IsSuccessful": true,
  "ErrorMessage": ""
}
```

**RabbitMQ Model**

* **Exchange**: `image-render.notifications`
* **Routing Key**: `task.completed`
* **Message Payload**:

  ```json
  {
    "MessageType": "ImageGenerationTaskCompleted",
    "ApplicationId": "d5eabbb8-c0f6-4640-94fd-3566edad499f",
    "UserId": "2581c740-8e9d-4d63-96bc-5665bb9a646b",
    "ImageRenderProcessId": "51d690da-f5ea-405e-90ff-10d56b0d633f",
    "TaskId": "9baa8e55-ddc8-4940-907f-eb5fc81e53f6",
    "ImageContentBytes": "base64encodedimagecontent==",
    "IsSuccessful": true,
    "ErrorMessage": ""
  }
  ```

***

#### 2. **ImageGenerationProgress**

**Description:**

This message provides the current progress of an image generation process, detailing task completion status and other metadata.

**WebHook JSON Example**

```json
{
  "MessageType": "ImageGenerationProgress",
  "RequestRenderState": "Succeeded",
  "ApplicationId": "d5eabbb8-c0f6-4640-94fd-3566edad499f",
  "UserId": "2581c740-8e9d-4d63-96bc-5665bb9a646b",
  "ImageRenderProcessId": "51d690da-f5ea-405e-90ff-10d56b0d633f",
  "TotalTaskCount": 10,
  "CompletedTaskCount": 8,
  "Progress": 80.0,
  "Tasks": {
    "24929cc8-34da-455b-a4f1-61f895ac3a06": "Succeeded",
    "855c3f61-0f00-457a-81a0-b2c0653d2b21": "Succeeded"
  },
  "RenderKind": 1,
  "RenderType": 2,
  "IsSuccessful": true,
  "ErrorMessage": ""
}
```

**RabbitMQ Model**

* **Exchange**: `image-render.notifications`
* **Routing Key**: `progress.update`
* **Message Payload**:

  ```json
  {
    "MessageType": "ImageGenerationProgress",
    "RequestRenderState": "Started",
    "ApplicationId": "d5eabbb8-c0f6-4640-94fd-3566edad499f",
    "UserId": "2581c740-8e9d-4d63-96bc-5665bb9a646b",
    "ImageRenderProcessId": "51d690da-f5ea-405e-90ff-10d56b0d633f",
    "TotalTaskCount": 10,
    "CompletedTaskCount": 3,
    "Progress": 30.0,
    "Tasks": {
      "24929cc8-34da-455b-a4f1-61f895ac3a06": "Started",
      "855c3f61-0f00-457a-81a0-b2c0653d2b21": "NotStarted"
    },
    "RenderKind": 1,
    "RenderType": 2,
    "IsSuccessful": true,
    "ErrorMessage": ""
  }
  ```

***

### How to Consume Notifications

#### WebHook Endpoint

1. **Setup**:
   * Provide the WebHook URL during application configuration.
   * Ensure the endpoint accepts HTTP POST requests with JSON payloads.
2. **Processing**:
   * Parse the `MessageType` field to determine the type of notification.
   * Handle the notification data as required.

#### RabbitMQ Queue

1. **Setup**:
   * Bind to the exchange `image-render.notifications` with appropriate routing keys (`task.completed`, `progress.update`).
   * Consume messages from the queue.
2. **Processing**:
   * Deserialize the message payload into the appropriate model (`ImageGenerationTaskCompleted` or `ImageGenerationProgress`).
   * Implement business logic based on the notification type and content.

***

### Notes

* **Security**:
  * Ensure RabbitMQ queues are securely configured with proper access credentials.
  * Validate incoming WebHook requests to verify authenticity.
* **Retry Mechanisms**:
  * Implement retry logic for failed WebHook delivery attempts.
  * Handle RabbitMQ message requeuing for unprocessed messages.

This documentation should help developers understand and integrate notification message types effectively into their systems.
