2022-11-22 16:57:15 +01:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
2022-11-23 15:56:43 +00:00
|
|
|
from apps.base.messaging import BaseMessagingBackend
|
2023-08-29 16:02:09 +02:00
|
|
|
from apps.mobile_app.tasks.new_alert_group import notify_user_about_new_alert_group
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MobileAppBackend(BaseMessagingBackend):
|
|
|
|
|
backend_id = "MOBILE_APP"
|
2023-02-02 17:06:28 +02:00
|
|
|
label = "Mobile push"
|
|
|
|
|
short_label = "Mobile push"
|
2022-11-23 15:56:43 +00:00
|
|
|
available_for_use = True
|
2024-02-08 17:23:15 -05:00
|
|
|
|
|
|
|
|
templater = "apps.mobile_app.alert_rendering.AlertMobileAppTemplater"
|
|
|
|
|
template_fields = ("title", "message")
|
|
|
|
|
skip_default_template_fields = True
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
def generate_user_verification_code(self, user):
|
|
|
|
|
from apps.mobile_app.models import MobileAppVerificationToken
|
|
|
|
|
|
|
|
|
|
# remove existing token before creating a new one
|
|
|
|
|
MobileAppVerificationToken.objects.filter(user=user).delete()
|
|
|
|
|
|
|
|
|
|
_, token = MobileAppVerificationToken.create_auth_token(user, user.organization)
|
2022-11-22 16:57:15 +01:00
|
|
|
return json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"token": token,
|
|
|
|
|
"oncall_api_url": settings.BASE_URL,
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
def unlink_user(self, user):
|
2023-07-05 17:14:46 +02:00
|
|
|
from apps.mobile_app.models import FCMDevice, MobileAppAuthToken
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
token = MobileAppAuthToken.objects.get(user=user)
|
|
|
|
|
token.delete()
|
|
|
|
|
|
2022-12-01 15:17:01 +00:00
|
|
|
# delete push notification related info for user
|
2023-07-05 17:14:46 +02:00
|
|
|
user_active_device = FCMDevice.get_active_device_for_user(user)
|
|
|
|
|
if user_active_device is not None:
|
|
|
|
|
user_active_device.delete()
|
2022-12-01 15:17:01 +00:00
|
|
|
|
2022-11-23 15:56:43 +00:00
|
|
|
def serialize_user(self, user):
|
2024-04-11 16:49:47 +02:00
|
|
|
return {"connected": getattr(user, "mobileappauthtoken", None) is not None}
|
2022-11-23 15:56:43 +00:00
|
|
|
|
|
|
|
|
def notify_user(self, user, alert_group, notification_policy, critical=False):
|
2023-08-29 16:02:09 +02:00
|
|
|
notify_user_about_new_alert_group.delay(
|
2022-11-23 15:56:43 +00:00
|
|
|
user_pk=user.pk,
|
|
|
|
|
alert_group_pk=alert_group.pk,
|
|
|
|
|
notification_policy_pk=notification_policy.pk,
|
|
|
|
|
critical=critical,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MobileAppCriticalBackend(MobileAppBackend):
|
|
|
|
|
"""
|
|
|
|
|
This notification backend should not exist, criticality of the push notification should be an option passed to the
|
|
|
|
|
MobileAppBackend messaging backend.
|
|
|
|
|
TODO: add ability to pass options to messaging backends both on backend and frontend, delete this backend after that
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
backend_id = "MOBILE_APP_CRITICAL"
|
2023-02-02 17:06:28 +02:00
|
|
|
label = "Mobile push important"
|
|
|
|
|
short_label = "Mobile push important"
|
2022-11-23 15:56:43 +00:00
|
|
|
template_fields = []
|
|
|
|
|
|
|
|
|
|
def notify_user(self, user, alert_group, notification_policy, critical=True):
|
|
|
|
|
super().notify_user(user, alert_group, notification_policy, critical)
|