gitscope
← marzneshin / master / app/notification/helper.py
https://dev.kronoz.win/r/marzneshin.git
app/notification/helper.py · Python
Blame Raw
 1from datetime import datetime
 2from typing import Dict, Optional
 3
 4from aiogram import html
 5
 6from app.models.admin import Admin
 7from app.models.notification import Notification, UserNotification
 8from app.models.user import UserResponse
 9from app.utils.system import readable_size
10
11
12def create_text(notif: Notification) -> str:
13    data = prepare_data(notif)
14    A = UserNotification.Action
15
16    texts: Dict[UserNotification.Action, str] = {
17        A.user_created: "🆕 <b>#Created</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username :</b> <code>{username}</code>\n<b>Traffic Limit :</b> <code>{data_limit}</code>\n<b>Expire Date :</b> <code>{expire_date}</code>\n<b>Services :</b> <code>{services}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
18        A.user_updated: "✏️ <b>#Modified</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username :</b> <code>{username}</code>\n<b>Traffic Limit :</b> <code>{data_limit}</code>\n<b>Expire Date :</b> <code>{expire_date}</code>\n<b>Services :</b> <code>{services}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
19        A.user_activated: "✅ <b>#Activated</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>",
20        A.user_deactivated: "❌ <b>#Deactivated</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>",
21        A.user_deleted: "🗑 <b>#Deleted</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
22        A.user_enabled: "☑️ <b>#Enabled</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
23        A.user_disabled: "🛑 <b>#Disabled</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>\n<b>By :</b> <b>#{by}</b>",
24        A.data_usage_reset: "🔁 <b>#Reset</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>By</b> : <b>#{by}</b>",
25        A.subscription_revoked: "🔁 <b>#Revoked</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n➖➖➖➖➖➖➖➖➖\n<b>By</b> : <b>#{by}</b>",
26        A.reached_usage_percent: "⚠️<b>#DataLimitWarning</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Used Percent</b> : <code>{usage_percent}</code>\n<b>Remaining Traffic</b> : <code>{remaining_traffic}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>",
27        A.reached_days_left: "⚠️<b>#ExpirationWarning</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Remaining Days</b> : <code>{remaining_days}</code>\n➖➖➖➖➖➖➖➖➖\n<b>Belongs To :</b> <code>{owner_username}</code>",
28    }
29
30    if notif.action == A.user_deactivated and not notif.by:
31        if notif.user.expired:
32            texts[A.user_deactivated] = (
33                "🕔 <b>#Expired</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>"
34            )
35        elif notif.user.data_limit_reached:
36            texts[A.user_deactivated] = (
37                "🪫 <b>#Limited</b>\n➖➖➖➖➖➖➖➖➖\n<b>Username</b> : <code>{username}</code>\n<b>Belongs To :</b> <code>{owner_username}</code>"
38            )
39
40    text = texts.get(notif.action)
41    formatted_message = text.format_map(data)
42
43    return formatted_message
44
45
46def prepare_data(notif: Notification) -> dict:
47    user = UserResponse.model_validate(notif.user)
48    by: Optional[Admin] = (
49        Admin.model_validate(notif.by)
50        if hasattr(notif, "by") and notif.by
51        else None
52    )
53    data = {
54        "username": html.quote(user.username),
55        "data_limit": (
56            readable_size(user.data_limit) if user.data_limit else "Unlimited"
57        ),
58        "remaining_traffic": (
59            readable_size(max(user.data_limit - user.used_traffic, 0))
60            if user.data_limit
61            else "Unlimited"
62        ),
63        "usage_percent": (
64            f"{round(min((user.used_traffic / user.data_limit) * 100, 100),2)}%"
65            if isinstance(user.data_limit, int) and user.data_limit > 0
66            else "0%"
67        ),
68        "expire_date": (
69            user.expire_date.strftime("%H:%M:%S %Y-%m-%d")
70            if user.expire_date
71            else "Never"
72        ),
73        "remaining_days": (
74            (user.expire_date - datetime.now()).days
75            if user.expire_date
76            else "Never"
77        ),
78        "services": user.service_ids if user.service_ids else "",
79        "owner_username": user.owner_username,
80        "by": html.quote(by.username) if by else None,
81    }
82
83    return data