# Path: invenio_communities/utils.py
# def on_membership_change(identity=None):
#     """Handler called when a membership is changed."""
#     if identity is not None:
#         current_cache.delete(identity_cache_key(identity))


import re
from invenio_access.permissions import system_process
from invenio_pidstore.errors import PIDAlreadyExists
from invenio_records_resources.services.records.components import \
    ServiceComponent
from marshmallow.exceptions import ValidationError
from ...utils import on_membership_change
    def edit(self, identity, draft=None, record=None, **kwargs):
        """Update draft metadata."""
        draft.access = record.access

    def new_version(self, identity, draft=None, record=None, **kwargs):
        """Update draft metadata."""
        draft.access = record.access


# TODO: Move to Invenio-Records-Resources (and make reusable). Duplicated from
# Invenio-RDM-Records.
class CommunityAccessComponent(AccessComponent):
    """Service component for access integration."""

    def _populate_access_and_validate(self, identity, data, record, **kwargs):
        """Populate and validate the community's access field."""
        if record is not None and "access" in data:
            # populate the record's access field with the data already
            # validated by marshmallow
            record.setdefault('access', {})
            record['access'].update(data.get("access", {}))
            record.access.refresh_from_dict(record.get("access"))

    def _init_owners(self, identity, record, **kwargs):
        """If the record has no owners yet, add the current user."""
        # if the given identity is that of a user, we add the
        # corresponding user to the owners (record.access.owned_by)
        is_sys_id = system_process in identity.provides
        if not record.access.owned_by and not is_sys_id:
            record.access.owned_by.add({"user": identity.id})

# Path: invenio_communities/utils.py
# def on_membership_change(identity=None):
#     """Handler called when a membership is changed."""
#     if identity is not None:
#         current_cache.delete(identity_cache_key(identity))


# Path: invenio_communities/communities/services/components.py
import re
from invenio_access.permissions import system_process
from invenio_pidstore.errors import PIDAlreadyExists
from invenio_records_resources.services.records.components import \
    ServiceComponent
from marshmallow.exceptions import ValidationError
from ...utils import on_membership_change

    def edit(self, identity, draft=None, record=None, **kwargs):
        """Update draft metadata."""
        draft.access = record.access

    def new_version(self, identity, draft=None, record=None, **kwargs):
        """Update draft metadata."""
        draft.access = record.access


# TODO: Move to Invenio-Records-Resources (and make reusable). Duplicated from
# Invenio-RDM-Records.
class CommunityAccessComponent(AccessComponent):
    """Service component for access integration."""

    def _populate_access_and_validate(self, identity, data, record, **kwargs):
        """Populate and validate the community's access field."""
        if record is not None and "access" in data:
            # populate the record's access field with the data already
            # validated by marshmallow
            record.setdefault('access', {})
            record['access'].update(data.get("access", {}))
            record.access.refresh_from_dict(record.get("access"))

    def _init_owners(self, identity, record, **kwargs):
        """If the record has no owners yet, add the current user."""
        # if the given identity is that of a user, we add the
        # corresponding user to the owners (record.access.owned_by)
        is_sys_id = system_process in identity.provides
        if not record.access.owned_by and not is_sys_id:
            record.access.owned_by.add({"user": identity.id})

            on_membership_change(identity=identity)