| 1 |
a47eb4c |
Dawsh |
2y ago |
from datetime import datetime as dt |
| 2 |
a47eb4c |
Dawsh |
2y ago |
from typing import Optional |
| 3 |
a47eb4c |
Dawsh |
2y ago |
|
| 4 |
a47eb4c |
Dawsh |
2y ago |
from app import telegram |
| 5 |
a47eb4c |
Dawsh |
2y ago |
from app.db import Session, create_notification_reminder |
| 6 |
a47eb4c |
Dawsh |
2y ago |
from app.models.admin import Admin |
| 7 |
a47eb4c |
Dawsh |
2y ago |
from app.models.user import ReminderType, UserResponse |
| 8 |
01cdf2e |
Dawsh |
2y ago |
from app.utils.notification import ( |
| 9 |
01cdf2e |
Dawsh |
2y ago |
Notification, |
| 10 |
01cdf2e |
Dawsh |
2y ago |
ReachedDaysLeft, |
| 11 |
01cdf2e |
Dawsh |
2y ago |
ReachedUsagePercent, |
| 12 |
01cdf2e |
Dawsh |
2y ago |
UserCreated, |
| 13 |
01cdf2e |
Dawsh |
2y ago |
UserDataUsageReset, |
| 14 |
01cdf2e |
Dawsh |
2y ago |
UserDeleted, |
| 15 |
01cdf2e |
Dawsh |
2y ago |
UserEnabled, |
| 16 |
01cdf2e |
Dawsh |
2y ago |
UserLimited, |
| 17 |
01cdf2e |
Dawsh |
2y ago |
UserSubscriptionRevoked, |
| 18 |
01cdf2e |
Dawsh |
2y ago |
UserUpdated, |
| 19 |
01cdf2e |
Dawsh |
2y ago |
notify, |
| 20 |
01cdf2e |
Dawsh |
2y ago |
) |
| 21 |
01cdf2e |
Dawsh |
2y ago |
|
| 22 |
01cdf2e |
Dawsh |
2y ago |
|
| 23 |
01cdf2e |
Dawsh |
2y ago |
async def status_change( |
| 24 |
f1ab16c |
Dawsh |
2y ago |
username: str, |
| 25 |
d377c4c |
Dawsh |
2y ago |
activation: bool, |
| 26 |
f1ab16c |
Dawsh |
2y ago |
user: UserResponse, |
| 27 |
f1ab16c |
Dawsh |
2y ago |
by: Optional[Admin] = None, |
| 28 |
01cdf2e |
Dawsh |
2y ago |
) -> None: |
| 29 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 30 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_status_change(username, status) |
| 31 |
a47eb4c |
Dawsh |
2y ago |
except Exception: |
| 32 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 33 |
d377c4c |
Dawsh |
2y ago |
if activation is False: |
| 34 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 35 |
01cdf2e |
Dawsh |
2y ago |
UserLimited( |
| 36 |
f1ab16c |
Dawsh |
2y ago |
username=username, |
| 37 |
d377c4c |
Dawsh |
2y ago |
action=Notification.Type.user_disabled, |
| 38 |
f1ab16c |
Dawsh |
2y ago |
user=user, |
| 39 |
01cdf2e |
Dawsh |
2y ago |
) |
| 40 |
01cdf2e |
Dawsh |
2y ago |
) |
| 41 |
d377c4c |
Dawsh |
2y ago |
elif activation is True: |
| 42 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 43 |
01cdf2e |
Dawsh |
2y ago |
UserEnabled( |
| 44 |
01cdf2e |
Dawsh |
2y ago |
username=username, |
| 45 |
01cdf2e |
Dawsh |
2y ago |
action=Notification.Type.user_enabled, |
| 46 |
01cdf2e |
Dawsh |
2y ago |
user=user, |
| 47 |
01cdf2e |
Dawsh |
2y ago |
by=by, |
| 48 |
01cdf2e |
Dawsh |
2y ago |
) |
| 49 |
01cdf2e |
Dawsh |
2y ago |
) |
| 50 |
a47eb4c |
Dawsh |
2y ago |
|
| 51 |
a47eb4c |
Dawsh |
2y ago |
|
| 52 |
a47eb4c |
Dawsh |
2y ago |
async def user_created(user: UserResponse, user_id: int, by: Admin) -> None: |
| 53 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_new_user( |
| 54 |
a47eb4c |
Dawsh |
2y ago |
user_id=user_id, |
| 55 |
a47eb4c |
Dawsh |
2y ago |
username=user.username, |
| 56 |
a47eb4c |
Dawsh |
2y ago |
by=by.username, |
| 57 |
d377c4c |
Dawsh |
2y ago |
expire_date=user.expire_date, |
| 58 |
a47eb4c |
Dawsh |
2y ago |
data_limit=user.data_limit, |
| 59 |
a47eb4c |
Dawsh |
2y ago |
# proxies=[] |
| 60 |
01cdf2e |
Dawsh |
2y ago |
services=user.services, |
| 61 |
01cdf2e |
Dawsh |
2y ago |
) |
| 62 |
01cdf2e |
Dawsh |
2y ago |
|
| 63 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 64 |
01cdf2e |
Dawsh |
2y ago |
UserCreated( |
| 65 |
01cdf2e |
Dawsh |
2y ago |
username=user.username, |
| 66 |
01cdf2e |
Dawsh |
2y ago |
action=Notification.Type.user_created, |
| 67 |
01cdf2e |
Dawsh |
2y ago |
by=by, |
| 68 |
01cdf2e |
Dawsh |
2y ago |
user=user, |
| 69 |
01cdf2e |
Dawsh |
2y ago |
) |
| 70 |
a47eb4c |
Dawsh |
2y ago |
) |
| 71 |
a47eb4c |
Dawsh |
2y ago |
|
| 72 |
a47eb4c |
Dawsh |
2y ago |
|
| 73 |
a47eb4c |
Dawsh |
2y ago |
async def user_updated(user: UserResponse, by: Admin) -> None: |
| 74 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 75 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_user_modification( |
| 76 |
a47eb4c |
Dawsh |
2y ago |
username=user.username, |
| 77 |
d377c4c |
Dawsh |
2y ago |
expire_date=user.expire_date, |
| 78 |
a47eb4c |
Dawsh |
2y ago |
data_limit=user.data_limit, |
| 79 |
a47eb4c |
Dawsh |
2y ago |
services=user.services, |
| 80 |
a47eb4c |
Dawsh |
2y ago |
by=by.username, |
| 81 |
a47eb4c |
Dawsh |
2y ago |
) |
| 82 |
a47eb4c |
Dawsh |
2y ago |
except Exception: |
| 83 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 84 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 85 |
01cdf2e |
Dawsh |
2y ago |
UserUpdated( |
| 86 |
01cdf2e |
Dawsh |
2y ago |
username=user.username, |
| 87 |
01cdf2e |
Dawsh |
2y ago |
action=Notification.Type.user_updated, |
| 88 |
01cdf2e |
Dawsh |
2y ago |
by=by, |
| 89 |
01cdf2e |
Dawsh |
2y ago |
user=user, |
| 90 |
01cdf2e |
Dawsh |
2y ago |
) |
| 91 |
01cdf2e |
Dawsh |
2y ago |
) |
| 92 |
a47eb4c |
Dawsh |
2y ago |
|
| 93 |
a47eb4c |
Dawsh |
2y ago |
|
| 94 |
a47eb4c |
Dawsh |
2y ago |
async def user_deleted(username: str, by: Admin) -> None: |
| 95 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 96 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_user_deletion(username=username, by=by.username) |
| 97 |
a47eb4c |
Dawsh |
2y ago |
except Exception: |
| 98 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 99 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 100 |
f1ab16c |
Dawsh |
2y ago |
UserDeleted( |
| 101 |
f1ab16c |
Dawsh |
2y ago |
username=username, action=Notification.Type.user_deleted, by=by |
| 102 |
f1ab16c |
Dawsh |
2y ago |
) |
| 103 |
01cdf2e |
Dawsh |
2y ago |
) |
| 104 |
a47eb4c |
Dawsh |
2y ago |
|
| 105 |
a47eb4c |
Dawsh |
2y ago |
|
| 106 |
a47eb4c |
Dawsh |
2y ago |
async def user_data_usage_reset(user: UserResponse, by: Admin) -> None: |
| 107 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 108 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_user_usage_reset( |
| 109 |
a47eb4c |
Dawsh |
2y ago |
username=user.username, |
| 110 |
a47eb4c |
Dawsh |
2y ago |
by=by.username, |
| 111 |
a47eb4c |
Dawsh |
2y ago |
) |
| 112 |
a47eb4c |
Dawsh |
2y ago |
except Exception: |
| 113 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 114 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 115 |
01cdf2e |
Dawsh |
2y ago |
UserDataUsageReset( |
| 116 |
01cdf2e |
Dawsh |
2y ago |
username=user.username, |
| 117 |
01cdf2e |
Dawsh |
2y ago |
action=Notification.Type.data_usage_reset, |
| 118 |
01cdf2e |
Dawsh |
2y ago |
by=by, |
| 119 |
01cdf2e |
Dawsh |
2y ago |
user=user, |
| 120 |
01cdf2e |
Dawsh |
2y ago |
) |
| 121 |
01cdf2e |
Dawsh |
2y ago |
) |
| 122 |
a47eb4c |
Dawsh |
2y ago |
|
| 123 |
a47eb4c |
Dawsh |
2y ago |
|
| 124 |
a47eb4c |
Dawsh |
2y ago |
async def user_subscription_revoked(user: UserResponse, by: Admin) -> None: |
| 125 |
a47eb4c |
Dawsh |
2y ago |
try: |
| 126 |
a47eb4c |
Dawsh |
2y ago |
await telegram.report_user_subscription_revoked( |
| 127 |
a47eb4c |
Dawsh |
2y ago |
username=user.username, |
| 128 |
a47eb4c |
Dawsh |
2y ago |
by=by.username, |
| 129 |
a47eb4c |
Dawsh |
2y ago |
) |
| 130 |
a47eb4c |
Dawsh |
2y ago |
except Exception: |
| 131 |
a47eb4c |
Dawsh |
2y ago |
pass |
| 132 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 133 |
01cdf2e |
Dawsh |
2y ago |
UserSubscriptionRevoked( |
| 134 |
01cdf2e |
Dawsh |
2y ago |
username=user.username, |
| 135 |
01cdf2e |
Dawsh |
2y ago |
action=Notification.Type.subscription_revoked, |
| 136 |
01cdf2e |
Dawsh |
2y ago |
by=by, |
| 137 |
01cdf2e |
Dawsh |
2y ago |
user=user, |
| 138 |
01cdf2e |
Dawsh |
2y ago |
) |
| 139 |
01cdf2e |
Dawsh |
2y ago |
) |
| 140 |
a47eb4c |
Dawsh |
2y ago |
|
| 141 |
a47eb4c |
Dawsh |
2y ago |
|
| 142 |
a47eb4c |
Dawsh |
2y ago |
async def data_usage_percent_reached( |
| 143 |
01cdf2e |
Dawsh |
2y ago |
db: Session, |
| 144 |
01cdf2e |
Dawsh |
2y ago |
percent: float, |
| 145 |
01cdf2e |
Dawsh |
2y ago |
user: UserResponse, |
| 146 |
01cdf2e |
Dawsh |
2y ago |
user_id: int, |
| 147 |
01cdf2e |
Dawsh |
2y ago |
expire: Optional[int] = None, |
| 148 |
01cdf2e |
Dawsh |
2y ago |
) -> None: |
| 149 |
01cdf2e |
Dawsh |
2y ago |
await notify( |
| 150 |
f1ab16c |
Dawsh |
2y ago |
ReachedUsagePercent( |
| 151 |
f1ab16c |
Dawsh |
2y ago |
username=user.username, user=user, used_percent=percent |
| 152 |
f1ab16c |
Dawsh |
2y ago |
) |
| 153 |
01cdf2e |
Dawsh |
2y ago |
) |
| 154 |
01cdf2e |
Dawsh |
2y ago |
await create_notification_reminder( |
| 155 |
01cdf2e |
Dawsh |
2y ago |
db, |
| 156 |
01cdf2e |
Dawsh |
2y ago |
ReminderType.data_usage, |
| 157 |
01cdf2e |
Dawsh |
2y ago |
expires_at=dt.utcfromtimestamp(expire) if expire else None, |
| 158 |
01cdf2e |
Dawsh |
2y ago |
user_id=user_id, |
| 159 |
01cdf2e |
Dawsh |
2y ago |
) |
| 160 |
a47eb4c |
Dawsh |
2y ago |
|
| 161 |
a47eb4c |
Dawsh |
2y ago |
|
| 162 |
01cdf2e |
Dawsh |
2y ago |
async def expire_days_reached( |
| 163 |
01cdf2e |
Dawsh |
2y ago |
db: Session, days: int, user: UserResponse, user_id: int, expire: int |
| 164 |
01cdf2e |
Dawsh |
2y ago |
) -> None: |
| 165 |
f1ab16c |
Dawsh |
2y ago |
await notify( |
| 166 |
f1ab16c |
Dawsh |
2y ago |
ReachedDaysLeft(username=user.username, user=user, days_left=days) |
| 167 |
f1ab16c |
Dawsh |
2y ago |
) |
| 168 |
a47eb4c |
Dawsh |
2y ago |
await create_notification_reminder( |
| 169 |
01cdf2e |
Dawsh |
2y ago |
db, |
| 170 |
01cdf2e |
Dawsh |
2y ago |
ReminderType.expiration_date, |
| 171 |
01cdf2e |
Dawsh |
2y ago |
expires_at=dt.utcfromtimestamp(expire), |
| 172 |
01cdf2e |
Dawsh |
2y ago |
user_id=user_id, |
| 173 |
01cdf2e |
Dawsh |
2y ago |
) |