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