1from datetime import datetime as dt
2from typing import Optional
3
4from app import telegram
5from app.db import Session, create_notification_reminder
6from app.db.models import UserStatus
7from app.models.admin import Admin
8from app.models.user import ReminderType, UserResponse
9from app.utils.notification import (Notification, ReachedDaysLeft,
10 ReachedUsagePercent, UserCreated,
11 UserDataUsageReset, UserDeleted,
12 UserDisabled, UserEnabled, UserExpired,
13 UserLimited, UserSubscriptionRevoked,
14 UserUpdated, notify)
15
16
17async def status_change(username: str, status: UserStatus, user: UserResponse, by: Optional[Admin] = None) -> None:
18 try:
19 await telegram.report_status_change(username, status)
20 except Exception:
21 pass
22 if status == UserStatus.limited:
23 await notify(UserLimited(username=username, action=Notification.Type.user_limited, user=user))
24 elif status == UserStatus.expired:
25 await notify(UserExpired(username=username, action=Notification.Type.user_expired, user=user))
26 elif status == UserStatus.disabled:
27 await notify(UserDisabled(username=username, action=Notification.Type.user_disabled, user=user, by=by))
28 elif status == UserStatus.active:
29 await notify(UserEnabled(username=username, action=Notification.Type.user_enabled, user=user, by=by))
30
31
32async def user_created(user: UserResponse, user_id: int, by: Admin) -> None:
33 await telegram.report_new_user(
34 user_id=user_id,
35 username=user.username,
36 by=by.username,
37 expire_date=user.expire,
38 data_limit=user.data_limit,
39 # proxies=[]
40 services=user.services
41 )
42
43 await notify(UserCreated(username=user.username, action=Notification.Type.user_created, by=by, user=user))
44
45
46async def user_updated(user: UserResponse, by: Admin) -> None:
47 try:
48 await telegram.report_user_modification(
49 username=user.username,
50 expire_date=user.expire,
51 data_limit=user.data_limit,
52 services=user.services,
53 by=by.username,
54 )
55 except Exception:
56 pass
57 await notify(UserUpdated(username=user.username, action=Notification.Type.user_updated, by=by, user=user))
58
59
60async def user_deleted(username: str, by: Admin) -> None:
61 try:
62 await telegram.report_user_deletion(username=username, by=by.username)
63 except Exception:
64 pass
65 await notify(UserDeleted(username=username, action=Notification.Type.user_deleted, by=by))
66
67
68async def user_data_usage_reset(user: UserResponse, by: Admin) -> None:
69 try:
70 await telegram.report_user_usage_reset(
71 username=user.username,
72 by=by.username,
73 )
74 except Exception:
75 pass
76 await notify(UserDataUsageReset(username=user.username, action=Notification.Type.data_usage_reset, by=by, user=user))
77
78
79async def user_subscription_revoked(user: UserResponse, by: Admin) -> None:
80 try:
81 await telegram.report_user_subscription_revoked(
82 username=user.username,
83 by=by.username,
84 )
85 except Exception:
86 pass
87 await notify(UserSubscriptionRevoked(username=user.username, action=Notification.Type.subscription_revoked, by=by, user=user))
88
89
90async def data_usage_percent_reached(
91 db: Session, percent: float, user: UserResponse, user_id: int, expire: Optional[int] = None) -> None:
92 await notify(ReachedUsagePercent(username=user.username, user=user, used_percent=percent))
93 await create_notification_reminder(db, ReminderType.data_usage,
94 expires_at=dt.utcfromtimestamp(expire) if expire else None, user_id=user_id)
95
96
97async def expire_days_reached(db: Session, days: int, user: UserResponse, user_id: int, expire: int) -> None:
98 await notify(ReachedDaysLeft(username=user.username, user=user, days_left=days))
99 await create_notification_reminder(
100 db, ReminderType.expiration_date, expires_at=dt.utcfromtimestamp(expire),
101 user_id=user_id)