Merge branch 'dev' into feature/install-page
# Conflicts: # backend/bot/app/web/admin_api_impl/settings.py # backend/bot/app/web/webapp/cache_helpers.py # frontend/src/admin/sections/SettingsSection.svelte # tests/test_admin_settings_manifest_i18n.py
This commit is contained in:
@@ -690,6 +690,51 @@ class NotificationService:
|
||||
profile_keyboard = self._build_profile_keyboard(_, telegram_id)
|
||||
await self._send_to_log_channel(message, reply_markup=profile_keyboard)
|
||||
|
||||
async def notify_account_merged(
|
||||
self,
|
||||
*,
|
||||
primary_user_id: int,
|
||||
removed_user_id: int,
|
||||
email: Optional[str],
|
||||
telegram_id: Optional[int],
|
||||
username: Optional[str] = None,
|
||||
first_name: Optional[str] = None,
|
||||
final_end_date_text: Optional[str] = None,
|
||||
primary_panel_user_uuid: Optional[str] = None,
|
||||
removed_panel_user_uuid: Optional[str] = None,
|
||||
):
|
||||
"""Send notification when duplicate email/Telegram accounts are merged."""
|
||||
if not self.settings.LOG_NEW_USERS:
|
||||
return
|
||||
|
||||
admin_lang = self.settings.DEFAULT_LANGUAGE
|
||||
_ = lambda k, **kw: self.i18n.gettext(admin_lang, k, **kw) if self.i18n else k
|
||||
|
||||
display_user_id = int(telegram_id or primary_user_id)
|
||||
user_display = self._format_user_display(
|
||||
user_id=display_user_id,
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
)
|
||||
|
||||
message = _(
|
||||
"log_account_merged",
|
||||
primary_user_id=primary_user_id,
|
||||
removed_user_id=removed_user_id,
|
||||
telegram_id=telegram_id or "",
|
||||
user_display=user_display,
|
||||
email=hd.quote(email or ""),
|
||||
final_end_date=hd.quote(final_end_date_text or ""),
|
||||
primary_panel_user_uuid=hd.quote(primary_panel_user_uuid or ""),
|
||||
removed_panel_user_uuid=hd.quote(removed_panel_user_uuid or ""),
|
||||
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
)
|
||||
|
||||
profile_keyboard = (
|
||||
self._build_profile_keyboard(_, int(telegram_id)) if telegram_id else None
|
||||
)
|
||||
await self._send_to_log_channel(message, reply_markup=profile_keyboard)
|
||||
|
||||
def _format_traffic_gb_admin(self, traffic_gb: float) -> str:
|
||||
value = float(traffic_gb)
|
||||
if value.is_integer():
|
||||
|
||||
@@ -155,6 +155,13 @@ class TariffTrafficWorker:
|
||||
cached_panel_user = panel_users_by_uuid.get(str(sub.panel_user_uuid))
|
||||
if cached_panel_user is not None:
|
||||
return cached_panel_user
|
||||
return await self._repair_missing_panel_user_for_subscription(
|
||||
session,
|
||||
sub,
|
||||
panel_users_by_uuid=panel_users_by_uuid,
|
||||
semaphore=semaphore,
|
||||
confirmed_missing=True,
|
||||
)
|
||||
|
||||
async with semaphore:
|
||||
try:
|
||||
@@ -167,12 +174,22 @@ class TariffTrafficWorker:
|
||||
sub.panel_user_uuid,
|
||||
)
|
||||
return {}
|
||||
return data or {}
|
||||
if data:
|
||||
return data
|
||||
return await self._repair_missing_panel_user_for_subscription(
|
||||
session,
|
||||
sub,
|
||||
panel_users_by_uuid=None,
|
||||
semaphore=semaphore,
|
||||
confirmed_missing=False,
|
||||
)
|
||||
|
||||
for chunk_start in range(0, len(subs), TARIFF_WORKER_BATCH_SIZE):
|
||||
chunk = subs[chunk_start : chunk_start + TARIFF_WORKER_BATCH_SIZE]
|
||||
panel_payloads = await asyncio.gather(*(_fetch_panel(s) for s in chunk))
|
||||
for sub, panel_data in zip(chunk, panel_payloads):
|
||||
if not panel_data:
|
||||
continue
|
||||
try:
|
||||
tariff = self.settings.tariffs_config.require(sub.tariff_key)
|
||||
except Exception:
|
||||
@@ -255,6 +272,69 @@ class TariffTrafficWorker:
|
||||
)
|
||||
return by_uuid
|
||||
|
||||
async def _repair_missing_panel_user_for_subscription(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
sub: Subscription,
|
||||
*,
|
||||
panel_users_by_uuid: Optional[dict[str, dict]],
|
||||
semaphore: asyncio.Semaphore,
|
||||
confirmed_missing: bool,
|
||||
) -> dict:
|
||||
current_uuid = str(getattr(sub, "panel_user_uuid", "") or "").strip()
|
||||
try:
|
||||
user_id = int(sub.user_id)
|
||||
except (TypeError, ValueError):
|
||||
user_id = 0
|
||||
db_user = await user_dal.get_user_by_id(session, user_id) if user_id else None
|
||||
canonical_uuid = str(getattr(db_user, "panel_user_uuid", "") or "").strip()
|
||||
|
||||
if canonical_uuid and canonical_uuid != current_uuid:
|
||||
panel_user = None
|
||||
if panel_users_by_uuid is not None:
|
||||
panel_user = panel_users_by_uuid.get(canonical_uuid)
|
||||
else:
|
||||
async with semaphore:
|
||||
try:
|
||||
panel_user = await self.panel_service.get_user_by_uuid(
|
||||
canonical_uuid,
|
||||
log_response=False,
|
||||
)
|
||||
except Exception:
|
||||
logging.exception(
|
||||
"TariffTrafficWorker: failed to fetch canonical panel user %s",
|
||||
canonical_uuid,
|
||||
)
|
||||
panel_user = None
|
||||
if panel_user:
|
||||
logging.warning(
|
||||
"TariffTrafficWorker: repaired subscription %s panel UUID %s -> %s",
|
||||
sub.subscription_id,
|
||||
current_uuid,
|
||||
canonical_uuid,
|
||||
)
|
||||
sub.panel_user_uuid = canonical_uuid
|
||||
return panel_user
|
||||
|
||||
if confirmed_missing:
|
||||
sub.is_active = False
|
||||
sub.skip_notifications = True
|
||||
sub.status_from_panel = "PANEL_USER_NOT_FOUND"
|
||||
logging.warning(
|
||||
"TariffTrafficWorker: deactivated subscription %s because panel user %s "
|
||||
"is missing",
|
||||
sub.subscription_id,
|
||||
current_uuid,
|
||||
)
|
||||
else:
|
||||
logging.warning(
|
||||
"TariffTrafficWorker: skipping subscription %s because panel user %s "
|
||||
"could not be fetched",
|
||||
sub.subscription_id,
|
||||
current_uuid,
|
||||
)
|
||||
return {}
|
||||
|
||||
async def _ensure_period_reset_strategy(
|
||||
self,
|
||||
sub: Subscription,
|
||||
|
||||
Reference in New Issue
Block a user