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