2022-06-03 08:09:47 -06:00
|
|
|
from django.conf import settings
|
|
|
|
|
from django.utils.module_loading import import_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BaseMessagingBackend:
|
|
|
|
|
backend_id = "SOMEID"
|
|
|
|
|
label = "The Backend"
|
|
|
|
|
short_label = "Backend"
|
|
|
|
|
available_for_use = False
|
2022-10-19 12:32:56 +01:00
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
templater = None
|
2022-10-19 12:32:56 +01:00
|
|
|
template_fields = ("title", "message", "image_url")
|
2024-02-08 17:23:15 -05:00
|
|
|
skip_default_template_fields = False
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-08-11 17:26:45 -03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self.notification_channel_id = kwargs.get("notification_channel_id")
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
def get_templater_class(self):
|
|
|
|
|
if self.templater:
|
|
|
|
|
return import_string(self.templater)
|
|
|
|
|
|
2022-06-29 14:05:02 +03:00
|
|
|
def validate_channel_filter_data(self, organization, data):
|
2022-06-03 08:09:47 -06:00
|
|
|
"""Validate JSON channel data for a channel filter update.
|
|
|
|
|
|
|
|
|
|
Ensure the required/expected data is provided as needed by the backend.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
def generate_channel_verification_code(self, organization):
|
|
|
|
|
"""Return a verification code for a channel registration."""
|
|
|
|
|
raise NotImplementedError("generate_channel_verification_code method missing implementation")
|
|
|
|
|
|
|
|
|
|
def generate_user_verification_code(self, user):
|
|
|
|
|
"""Return a verification code to link a user with an account."""
|
|
|
|
|
raise NotImplementedError("generate_user_verification_code method missing implementation")
|
|
|
|
|
|
|
|
|
|
def unlink_user(self, user):
|
|
|
|
|
"""Remove backend link to user account."""
|
|
|
|
|
return
|
|
|
|
|
|
2023-01-18 15:52:25 +00:00
|
|
|
@staticmethod
|
|
|
|
|
def is_enabled_for_organization(organization):
|
|
|
|
|
return True
|
|
|
|
|
|
2024-04-11 11:51:56 -03:00
|
|
|
def is_configured_for_organization(self, organization):
|
|
|
|
|
return True
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
def serialize_user(self, user):
|
|
|
|
|
"""Return a serialized backend user representation."""
|
|
|
|
|
raise NotImplementedError("serialize_user method missing implementation")
|
|
|
|
|
|
|
|
|
|
def notify_user(self, user, alert_group, notification_policy):
|
|
|
|
|
"""Send user a notification for the given alert group.
|
|
|
|
|
|
|
|
|
|
The notification policy links to the backend as the notification channel.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
raise NotImplementedError("notify_user method missing implementation")
|
|
|
|
|
|
2023-03-01 16:32:15 +08:00
|
|
|
@property
|
|
|
|
|
def slug(self):
|
|
|
|
|
return self.backend_id.lower()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def customizable_templates(self):
|
|
|
|
|
"""
|
|
|
|
|
customizable_templates indicates if templates for messaging backend can be changes by user
|
|
|
|
|
"""
|
|
|
|
|
return True
|
|
|
|
|
|
2022-06-03 08:09:47 -06:00
|
|
|
|
2022-08-11 17:26:45 -03:00
|
|
|
def load_backend(path, *args, **kwargs):
|
|
|
|
|
return import_string(path)(*args, **kwargs)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_messaging_backends():
|
|
|
|
|
global _messaging_backends
|
|
|
|
|
if _messaging_backends is None:
|
|
|
|
|
_messaging_backends = {}
|
2023-06-12 18:50:33 +02:00
|
|
|
for backend_path, notification_channel_id in settings.EXTRA_MESSAGING_BACKENDS:
|
2022-08-11 17:26:45 -03:00
|
|
|
backend = load_backend(backend_path, notification_channel_id=notification_channel_id)
|
2022-06-03 08:09:47 -06:00
|
|
|
_messaging_backends[backend.backend_id] = backend
|
|
|
|
|
return _messaging_backends.items()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_messaging_backend_from_id(backend_id):
|
2022-06-10 11:09:05 -03:00
|
|
|
return _messaging_backends.get(backend_id)
|
2022-06-03 08:09:47 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
_messaging_backends = None
|