# Path: zenodo/modules/communities/tasks.py
# @shared_task(ignore_results=True)
# def dispatch_webhook(community_id, record_id, event_type):
#     """Build webhook payload and dispatch delivery tasks."""
#     webhooks_cfg = current_app.config.get('ZENODO_COMMUNITIES_WEBHOOKS', {})
#     recipients = webhooks_cfg.get(community_id, [])
#     if not recipients:
#         return
# 
#     # TODO: Extract to a utility?
#     record = ZenodoRecord.get_record(record_id)
#     community = Community.query.get(community_id)
#     # TODO: Make configurable
#     record_payload = legacyjson_v1.transform_record(record.pid, record)
#     payload = {
#         "timestamp": datetime.utcnow().isoformat(),
#         "id": str(uuid.uuid4()),
#         "event_type": event_type,
#         "context": {
#             "community": community.id,
#             "user": record['owners'][0],
#         },
#         "payload": {
#             "community": {
#                 "id": community.id,
#                 "owner": {
#                     "id": community.id_user,
#                 }
#             },
#             "record": record_payload,
#         },
#     }
#     for recipient_id in recipients:
#         deliver_webhook.delay(payload, community_id, recipient_id)


from .tasks import dispatch_webhook
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zenodo; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Receivers for Zenodo Communities."""

from __future__ import absolute_import, print_function



def send_inclusion_request_webhook(sender, request=None, **kwargs):
    """Signal receiver to send webhooks after a community inclusion request."""
    dispatch_webhook.delay(
        community_id=str(request.id_community),
        record_id=str(request.id_record),
        event_type='community.records.inclusion',
    )


def send_record_accepted_webhook(
        sender, record_id=None, community_id=None, **kwargs):
    """Signal receiver to send webhooks on a record accepted in a community."""

# Path: zenodo/modules/communities/tasks.py
# @shared_task(ignore_results=True)
# def dispatch_webhook(community_id, record_id, event_type):
#     """Build webhook payload and dispatch delivery tasks."""
#     webhooks_cfg = current_app.config.get('ZENODO_COMMUNITIES_WEBHOOKS', {})
#     recipients = webhooks_cfg.get(community_id, [])
#     if not recipients:
#         return
# 
#     # TODO: Extract to a utility?
#     record = ZenodoRecord.get_record(record_id)
#     community = Community.query.get(community_id)
#     # TODO: Make configurable
#     record_payload = legacyjson_v1.transform_record(record.pid, record)
#     payload = {
#         "timestamp": datetime.utcnow().isoformat(),
#         "id": str(uuid.uuid4()),
#         "event_type": event_type,
#         "context": {
#             "community": community.id,
#             "user": record['owners'][0],
#         },
#         "payload": {
#             "community": {
#                 "id": community.id,
#                 "owner": {
#                     "id": community.id_user,
#                 }
#             },
#             "record": record_payload,
#         },
#     }
#     for recipient_id in recipients:
#         deliver_webhook.delay(payload, community_id, recipient_id)


# Path: zenodo/modules/communities/receivers.py
from .tasks import dispatch_webhook

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zenodo; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Receivers for Zenodo Communities."""

from __future__ import absolute_import, print_function



def send_inclusion_request_webhook(sender, request=None, **kwargs):
    """Signal receiver to send webhooks after a community inclusion request."""
    dispatch_webhook.delay(
        community_id=str(request.id_community),
        record_id=str(request.id_record),
        event_type='community.records.inclusion',
    )


def send_record_accepted_webhook(
        sender, record_id=None, community_id=None, **kwargs):
    """Signal receiver to send webhooks on a record accepted in a community."""

    dispatch_webhook.delay(