2022-06-03 08:09:47 -06:00
|
|
|
from rest_framework import mixins, status, viewsets
|
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
|
2022-11-29 09:41:56 +01:00
|
|
|
from apps.api.permissions import RBACPermission
|
2022-06-03 08:09:47 -06:00
|
|
|
from apps.api.serializers.telegram import TelegramToOrganizationConnectorSerializer
|
|
|
|
|
from apps.auth_token.auth import PluginAuthentication
|
2024-01-30 13:07:19 -05:00
|
|
|
from apps.telegram.models import TelegramToOrganizationConnector
|
2022-06-03 08:09:47 -06:00
|
|
|
from common.api_helpers.mixins import PublicPrimaryKeyMixin
|
2023-04-17 15:16:18 +08:00
|
|
|
from common.insight_log.chatops_insight_logs import ChatOpsEvent, ChatOpsTypePlug, write_chatops_insight_log
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramChannelViewSet(
|
2024-01-30 13:07:19 -05:00
|
|
|
PublicPrimaryKeyMixin[TelegramToOrganizationConnector],
|
2022-06-03 08:09:47 -06:00
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
|
):
|
|
|
|
|
authentication_classes = (PluginAuthentication,)
|
2022-11-29 09:41:56 +01:00
|
|
|
permission_classes = (IsAuthenticated, RBACPermission)
|
|
|
|
|
|
|
|
|
|
rbac_permissions = {
|
|
|
|
|
"metadata": [RBACPermission.Permissions.CHATOPS_READ],
|
|
|
|
|
"list": [RBACPermission.Permissions.CHATOPS_READ],
|
|
|
|
|
"retrieve": [RBACPermission.Permissions.CHATOPS_READ],
|
|
|
|
|
"destroy": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
|
|
|
|
"set_default": [RBACPermission.Permissions.CHATOPS_UPDATE_SETTINGS],
|
2022-06-03 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serializer_class = TelegramToOrganizationConnectorSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
return TelegramToOrganizationConnector.objects.filter(organization=self.request.user.organization)
|
|
|
|
|
|
|
|
|
|
@action(detail=True, methods=["post"])
|
|
|
|
|
def set_default(self, request, pk):
|
|
|
|
|
telegram_channel = self.get_object()
|
|
|
|
|
telegram_channel.make_channel_default(request.user)
|
|
|
|
|
|
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
def perform_destroy(self, instance):
|
|
|
|
|
user = self.request.user
|
2022-08-24 12:04:44 +05:00
|
|
|
write_chatops_insight_log(
|
|
|
|
|
author=user,
|
|
|
|
|
event_name=ChatOpsEvent.CHANNEL_DISCONNECTED,
|
2023-04-17 15:16:18 +08:00
|
|
|
chatops_type=ChatOpsTypePlug.TELEGRAM.value,
|
2022-08-24 12:04:44 +05:00
|
|
|
channel_name=instance.channel_name,
|
|
|
|
|
)
|
2022-06-03 08:09:47 -06:00
|
|
|
instance.delete()
|