Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-2586 [FEAT] Add email notification support for connector sharing (#1647)

Rahul Johny6bfbe67f047d
diff --git a/backend/connector_v2/views.py b/backend/connector_v2/views.py
index 7469658ae..1641b8536 100644
--- a/backend/connector_v2/views.py
+++ b/backend/connector_v2/views.py
@@ -10,6 +10,7 @@ from django.db import IntegrityError
from django.db.models import ProtectedError, QuerySet
from permissions.permission import IsOwner, IsOwnerOrSharedUserOrSharedToOrg
from rest_framework import status, viewsets
+from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.versioning import URLPathVersioning
from utils.filtering import FilterHelper
@@ -23,6 +24,16 @@ from .exceptions import DeleteConnectorInUseError
from .models import ConnectorInstance
from .serializers import ConnectorInstanceSerializer
+try:
+ from plugins.notification.constants import ResourceType
+ from plugins.notification.sharing_notification import SharingNotificationService
+
+ NOTIFICATION_PLUGIN_AVAILABLE = True
+ sharing_notification_service = SharingNotificationService()
+except ImportError:
+ NOTIFICATION_PLUGIN_AVAILABLE = False
+ sharing_notification_service = None
+
logger = logging.getLogger(__name__)
@@ -192,3 +203,44 @@ class ConnectorInstanceViewSet(viewsets.ModelViewSet):
f" named {instance.connector_name}"
)
raise DeleteConnectorInUseError(connector_name=instance.connector_name)
+
+ def partial_update(self, request: Request, *args: Any, **kwargs: Any) -> Response:
+ """Override to handle sharing notifications."""
+ instance = self.get_object()
+ current_shared_users = set(instance.shared_users.all())
+
+ response = super().partial_update(request, *args, **kwargs)
+
+ if (
+ response.status_code == 200
+ and "shared_users" in request.data
+ and NOTIFICATION_PLUGIN_AVAILABLE
+ ):
+ try:
+ instance.refresh_from_db()
+ new_shared_users = set(instance.shared_users.all())
+ newly_shared_users = new_shared_users - current_shared_users
+
+ if newly_shared_users:
+ # Only send notifications if there are newly shared users
+ sharing_notification_service.send_sharing_notification(
+ resource_type=ResourceType.CONNECTOR.value,
+ resource_name=instance.connector_name,
+ resource_id=str(instance.id),
+ shared_by=request.user,
+ shared_to=list(newly_shared_users),
+ resource_instance=instance,
+ )
+
+ logger.info(
+ f"Sent sharing notifications for connector "
+ f"to {len(newly_shared_users)} users"
+ )
+
+ except Exception as e:
+ # Log error but don't fail the update operation
+ logger.exception(
+ f"Failed to send sharing notification, continuing update though: {str(e)}"
+ )
+
+ return response