fix: add subgroup/special group support to key renewals and cluster sync

This commit is contained in:
Capybara-z
2025-10-22 02:07:18 +03:00
parent e6313bf5a3
commit a256a2f338
5 changed files with 33 additions and 4 deletions
+17 -2
View File
@@ -615,6 +615,7 @@ async def handle_sync_cluster(
traffic_limit_bytes = 0
hwid_limit = 0
subgroup_title = None
if key["tariff_id"]:
tariff = await session.get(Tariff, key["tariff_id"])
if tariff:
@@ -623,12 +624,22 @@ async def handle_sync_cluster(
else:
traffic_limit_bytes = 0
hwid_limit = tariff.device_limit
subgroup_title = tariff.subgroup_title
else:
logger.warning(
f"[Sync] Ключ {key['client_id']} с несуществующим тарифом ID={key['tariff_id']} — обновим без лимитов"
)
inbound_ids = [s["inbound_id"] for s in cluster_servers if s.get("inbound_id")]
filtered_servers = cluster_servers
if subgroup_title:
filtered_servers = [s for s in cluster_servers if subgroup_title in s.get("tariff_subgroups", [])]
if not filtered_servers:
logger.warning(
f"[Sync] В кластере {cluster_name} не найдено серверов для подгруппы '{subgroup_title}'. Использую весь кластер."
)
filtered_servers = cluster_servers
inbound_ids = [s["inbound_id"] for s in filtered_servers if s.get("inbound_id")]
success = await remna.update_user(
uuid=key["client_id"],
@@ -765,9 +776,10 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS
traffic_limit = 0
device_limit = 0
key_subgroup = None
if key.tariff_id:
result = await session.execute(
select(Tariff.traffic_limit, Tariff.device_limit).where(
select(Tariff.traffic_limit, Tariff.device_limit, Tariff.subgroup_title).where(
Tariff.id == key.tariff_id, Tariff.is_active.is_(True)
)
)
@@ -775,6 +787,7 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS
if tariff:
traffic_limit = int(tariff[0]) if tariff[0] is not None else 0
device_limit = int(tariff[1]) if tariff[1] is not None else 0
key_subgroup = tariff[2]
await renew_key_in_cluster(
cluster_name,
@@ -785,6 +798,8 @@ async def handle_days_input(message: Message, state: FSMContext, session: AsyncS
session=session,
hwid_device_limit=device_limit,
reset_traffic=False,
target_subgroup=key_subgroup,
old_subgroup=key_subgroup,
)
await update_key_expiry(session, key.client_id, new_expiry)
-1
View File
@@ -249,7 +249,6 @@ async def view_gift(callback: CallbackQuery, session: AsyncSession):
f"ID: <code>{gift.gift_id}</code>\n"
f"Срок: <b>{duration_text}</b>\n"
f"Активаций: <b>{usage_text}</b>\n"
f"Истекает: <i>{gift.expiry_time.strftime('%d.%m.%Y')}</i>\n"
f"<b>Ссылка для активации:</b>\n<blockquote>{gift.gift_link}</blockquote>"
)
+6 -1
View File
@@ -1223,14 +1223,17 @@ async def change_expiry_time(expiry_time: int, email: str, session: AsyncSession
traffic_limit = 0
device_limit = None
key_subgroup = None
if tariff_id:
result = await session.execute(
select(Tariff.traffic_limit, Tariff.device_limit).where(Tariff.id == tariff_id, Tariff.is_active.is_(True))
select(Tariff.traffic_limit, Tariff.device_limit, Tariff.subgroup_title).where(Tariff.id == tariff_id, Tariff.is_active.is_(True))
)
tariff = result.first()
if tariff:
traffic_limit = int(tariff[0]) if tariff[0] is not None else 0
device_limit = int(tariff[1]) if tariff[1] is not None else 0
key_subgroup = tariff[2]
servers = await get_servers(session=session)
@@ -1255,6 +1258,8 @@ async def change_expiry_time(expiry_time: int, email: str, session: AsyncSession
session=session,
hwid_device_limit=device_limit,
reset_traffic=False,
target_subgroup=key_subgroup,
old_subgroup=key_subgroup,
)
await update_key_expiry(session, client_id, expiry_time)
+6
View File
@@ -225,6 +225,10 @@ async def handle_key_extension(
total_gb = int(tariff["traffic_limit"]) if tariff and tariff.get("traffic_limit") else 0
device_limit = int(tariff["device_limit"]) if tariff and tariff.get("device_limit") else 0
key_subgroup = None
if tariff:
key_subgroup = tariff.get('subgroup_title')
await renew_key_in_cluster(
cluster_id=key.server_id,
email=key.email,
@@ -234,6 +238,8 @@ async def handle_key_extension(
session=session,
hwid_device_limit=device_limit,
reset_traffic=False,
target_subgroup=key_subgroup,
old_subgroup=key_subgroup,
)
await update_key_expiry(session, client_id, new_expiry)
await update_coupon_usage_count(session, coupon.id)
@@ -566,6 +566,8 @@ async def process_auto_renew_or_notify(
f"Продление подписки {email} на {duration_days} дней для пользователя {tg_id}. Баланс: {balance}, списываем: {renewal_cost}"
)
key_subgroup = selected_tariff.get('subgroup_title')
await renew_key_in_cluster(
cluster_id=server_id,
email=email,
@@ -574,6 +576,8 @@ async def process_auto_renew_or_notify(
total_gb=total_gb,
hwid_device_limit=device_limit,
session=conn,
target_subgroup=key_subgroup,
old_subgroup=key_subgroup,
)
await update_balance(conn, tg_id, -renewal_cost)
await update_key_expiry(conn, client_id, int(new_expiry_time))