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