| 1 |
a47eb4c |
Dawsh |
2y ago |
from collections import deque |
| 2 |
a47eb4c |
Dawsh |
2y ago |
from datetime import datetime as dt |
| 3 |
a47eb4c |
Dawsh |
2y ago |
from enum import Enum |
| 4 |
a47eb4c |
Dawsh |
2y ago |
|
| 5 |
a47eb4c |
Dawsh |
2y ago |
from pydantic import BaseModel |
| 6 |
a47eb4c |
Dawsh |
2y ago |
|
| 7 |
8111eb7 |
Dawsh |
2y ago |
from app.config.env import WEBHOOK_ADDRESS |
| 8 |
a47eb4c |
Dawsh |
2y ago |
from app.models.admin import Admin |
| 9 |
a47eb4c |
Dawsh |
2y ago |
from app.models.user import UserResponse |
| 10 |
a47eb4c |
Dawsh |
2y ago |
|
| 11 |
a47eb4c |
Dawsh |
2y ago |
queue = deque() |
| 12 |
a47eb4c |
Dawsh |
2y ago |
|
| 13 |
a47eb4c |
Dawsh |
2y ago |
|
| 14 |
a47eb4c |
Dawsh |
2y ago |
class Notification(BaseModel): |
| 15 |
a47eb4c |
Dawsh |
2y ago |
class Type(str, Enum): |
| 16 |
a47eb4c |
Dawsh |
2y ago |
user_created = "user_created" |
| 17 |
a47eb4c |
Dawsh |
2y ago |
user_updated = "user_updated" |
| 18 |
a47eb4c |
Dawsh |
2y ago |
user_deleted = "user_deleted" |
| 19 |
a47eb4c |
Dawsh |
2y ago |
user_limited = "user_limited" |
| 20 |
a47eb4c |
Dawsh |
2y ago |
user_expired = "user_expired" |
| 21 |
a47eb4c |
Dawsh |
2y ago |
user_enabled = "user_enabled" |
| 22 |
a47eb4c |
Dawsh |
2y ago |
user_disabled = "user_disabled" |
| 23 |
a47eb4c |
Dawsh |
2y ago |
data_usage_reset = "data_usage_reset" |
| 24 |
a47eb4c |
Dawsh |
2y ago |
subscription_revoked = "subscription_revoked" |
| 25 |
a47eb4c |
Dawsh |
2y ago |
|
| 26 |
a47eb4c |
Dawsh |
2y ago |
reached_usage_percent = "reached_usage_percent" |
| 27 |
a47eb4c |
Dawsh |
2y ago |
reached_days_left = "reached_days_left" |
| 28 |
a47eb4c |
Dawsh |
2y ago |
|
| 29 |
a47eb4c |
Dawsh |
2y ago |
enqueued_at: float = dt.utcnow().timestamp() |
| 30 |
a47eb4c |
Dawsh |
2y ago |
send_at: float = dt.utcnow().timestamp() |
| 31 |
a47eb4c |
Dawsh |
2y ago |
tries: int = 0 |
| 32 |
a47eb4c |
Dawsh |
2y ago |
|
| 33 |
a47eb4c |
Dawsh |
2y ago |
|
| 34 |
a47eb4c |
Dawsh |
2y ago |
class UserNotification(Notification): |
| 35 |
a47eb4c |
Dawsh |
2y ago |
username: str |
| 36 |
a47eb4c |
Dawsh |
2y ago |
|
| 37 |
a47eb4c |
Dawsh |
2y ago |
|
| 38 |
a47eb4c |
Dawsh |
2y ago |
class ReachedUsagePercent(UserNotification): |
| 39 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.reached_usage_percent |
| 40 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 41 |
a47eb4c |
Dawsh |
2y ago |
used_percent: float |
| 42 |
a47eb4c |
Dawsh |
2y ago |
|
| 43 |
a47eb4c |
Dawsh |
2y ago |
|
| 44 |
a47eb4c |
Dawsh |
2y ago |
class ReachedDaysLeft(UserNotification): |
| 45 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.reached_days_left |
| 46 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 47 |
a47eb4c |
Dawsh |
2y ago |
days_left: int |
| 48 |
a47eb4c |
Dawsh |
2y ago |
|
| 49 |
a47eb4c |
Dawsh |
2y ago |
|
| 50 |
a47eb4c |
Dawsh |
2y ago |
class UserCreated(UserNotification): |
| 51 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_created |
| 52 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 53 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 54 |
a47eb4c |
Dawsh |
2y ago |
|
| 55 |
a47eb4c |
Dawsh |
2y ago |
|
| 56 |
a47eb4c |
Dawsh |
2y ago |
class UserUpdated(UserNotification): |
| 57 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_updated |
| 58 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 59 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 60 |
a47eb4c |
Dawsh |
2y ago |
|
| 61 |
a47eb4c |
Dawsh |
2y ago |
|
| 62 |
a47eb4c |
Dawsh |
2y ago |
class UserDeleted(UserNotification): |
| 63 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_deleted |
| 64 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 65 |
a47eb4c |
Dawsh |
2y ago |
|
| 66 |
a47eb4c |
Dawsh |
2y ago |
|
| 67 |
a47eb4c |
Dawsh |
2y ago |
class UserLimited(UserNotification): |
| 68 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_limited |
| 69 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 70 |
a47eb4c |
Dawsh |
2y ago |
|
| 71 |
a47eb4c |
Dawsh |
2y ago |
|
| 72 |
a47eb4c |
Dawsh |
2y ago |
class UserExpired(UserNotification): |
| 73 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_expired |
| 74 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 75 |
a47eb4c |
Dawsh |
2y ago |
|
| 76 |
a47eb4c |
Dawsh |
2y ago |
|
| 77 |
a47eb4c |
Dawsh |
2y ago |
class UserEnabled(UserNotification): |
| 78 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_enabled |
| 79 |
a47eb4c |
Dawsh |
2y ago |
by: Admin = None |
| 80 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 81 |
a47eb4c |
Dawsh |
2y ago |
|
| 82 |
a47eb4c |
Dawsh |
2y ago |
|
| 83 |
a47eb4c |
Dawsh |
2y ago |
class UserDisabled(UserNotification): |
| 84 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.user_disabled |
| 85 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 86 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 87 |
a47eb4c |
Dawsh |
2y ago |
reason: str = None |
| 88 |
a47eb4c |
Dawsh |
2y ago |
|
| 89 |
a47eb4c |
Dawsh |
2y ago |
|
| 90 |
a47eb4c |
Dawsh |
2y ago |
class UserDataUsageReset(UserNotification): |
| 91 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.data_usage_reset |
| 92 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 93 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 94 |
a47eb4c |
Dawsh |
2y ago |
|
| 95 |
a47eb4c |
Dawsh |
2y ago |
|
| 96 |
a47eb4c |
Dawsh |
2y ago |
class UserSubscriptionRevoked(UserNotification): |
| 97 |
a47eb4c |
Dawsh |
2y ago |
action: Notification.Type = Notification.Type.subscription_revoked |
| 98 |
a47eb4c |
Dawsh |
2y ago |
by: Admin |
| 99 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse |
| 100 |
a47eb4c |
Dawsh |
2y ago |
|
| 101 |
a47eb4c |
Dawsh |
2y ago |
|
| 102 |
9a6b0fa |
Dawsh |
2y ago |
async def notify(message: Notification) -> None: |
| 103 |
8111eb7 |
Dawsh |
2y ago |
if WEBHOOK_ADDRESS: |
| 104 |
a47eb4c |
Dawsh |
2y ago |
queue.append(message) |