2023-06-12 18:50:33 +02:00
|
|
|
import datetime
|
2022-06-03 08:09:47 -06:00
|
|
|
from typing import Optional, Tuple
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
from django.db import models
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
|
|
from apps.telegram.models import TelegramToOrganizationConnector
|
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 TelegramChannelVerificationCode(models.Model):
|
|
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
|
|
|
|
|
datetime = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
|
|
organization = models.OneToOneField(
|
|
|
|
|
"user_management.Organization", on_delete=models.CASCADE, related_name="telegram_verification_code"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
author = models.ForeignKey("user_management.User", on_delete=models.CASCADE, null=True, default=None)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def is_active(self) -> bool:
|
2023-06-12 18:50:33 +02:00
|
|
|
return self.datetime + datetime.timedelta(days=1) < timezone.now()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-10-25 14:53:07 +08:00
|
|
|
@property
|
2022-12-06 22:42:58 +08:00
|
|
|
def uuid_with_org_uuid(self) -> str:
|
|
|
|
|
return f"{self.organization.uuid}_{self.uuid}"
|
2022-10-25 14:53:07 +08:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def uuid_without_org_id(cls, verification_code: str) -> str:
|
|
|
|
|
try:
|
|
|
|
|
return verification_code.split("_")[1]
|
|
|
|
|
except IndexError:
|
|
|
|
|
raise ValidationError("Invalid verification code format")
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
@classmethod
|
|
|
|
|
def verify_channel_and_discussion_group(
|
|
|
|
|
cls,
|
2022-10-25 14:53:07 +08:00
|
|
|
verification_code: str,
|
2022-06-03 08:09:47 -06:00
|
|
|
channel_chat_id: int,
|
|
|
|
|
channel_name: str,
|
|
|
|
|
discussion_group_chat_id: int,
|
|
|
|
|
discussion_group_name: str,
|
|
|
|
|
) -> Tuple[Optional[TelegramToOrganizationConnector], bool]:
|
|
|
|
|
try:
|
2022-10-25 14:53:07 +08:00
|
|
|
uuid_code = cls.uuid_without_org_id(verification_code)
|
|
|
|
|
|
|
|
|
|
code_instance = cls.objects.get(uuid=uuid_code)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
# see if a organization has other channels connected
|
|
|
|
|
# if it is the first channel, make it default for the organization
|
2022-10-25 14:53:07 +08:00
|
|
|
connector_exists = code_instance.organization.telegram_channel.exists()
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
connector, created = TelegramToOrganizationConnector.objects.get_or_create(
|
|
|
|
|
channel_chat_id=channel_chat_id,
|
|
|
|
|
defaults={
|
2022-10-25 14:53:07 +08:00
|
|
|
"organization": code_instance.organization,
|
2022-06-03 08:09:47 -06:00
|
|
|
"channel_name": channel_name,
|
|
|
|
|
"discussion_group_chat_id": discussion_group_chat_id,
|
|
|
|
|
"discussion_group_name": discussion_group_name,
|
|
|
|
|
"is_default_channel": not connector_exists,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2022-08-24 12:04:44 +05:00
|
|
|
write_chatops_insight_log(
|
2022-10-25 14:53:07 +08:00
|
|
|
author=code_instance.author,
|
2022-08-24 12:04:44 +05:00
|
|
|
event_name=ChatOpsEvent.CHANNEL_CONNECTED,
|
2023-04-17 15:16:18 +08:00
|
|
|
chatops_type=ChatOpsTypePlug.TELEGRAM.value,
|
2022-08-24 12:04:44 +05:00
|
|
|
channel_name=channel_name,
|
2022-06-03 08:09:47 -06:00
|
|
|
)
|
|
|
|
|
if not connector_exists:
|
2022-08-24 12:04:44 +05:00
|
|
|
write_chatops_insight_log(
|
2022-10-25 14:53:07 +08:00
|
|
|
author=code_instance.author,
|
2022-08-24 12:04:44 +05:00
|
|
|
event_name=ChatOpsEvent.DEFAULT_CHANNEL_CHANGED,
|
2023-04-17 15:16:18 +08:00
|
|
|
chatops_type=ChatOpsTypePlug.TELEGRAM.value,
|
2022-08-24 12:04:44 +05:00
|
|
|
prev_channel=None,
|
|
|
|
|
new_channel=channel_name,
|
2022-06-03 08:09:47 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return connector, created
|
|
|
|
|
|
|
|
|
|
except (ValidationError, cls.DoesNotExist):
|
|
|
|
|
return None, False
|