Skip to content

Commit 92de1b7

Browse files
committed
fix: use single function to update node user
1 parent ae995b0 commit 92de1b7

File tree

8 files changed

+59
-55
lines changed

8 files changed

+59
-55
lines changed

app/db/crud/admin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,23 @@ async def get_admins(
198198
total = None
199199
active = None
200200
disabled = None
201-
201+
202202
if return_with_count:
203203
# Get total count
204204
count_stmt = select(func.count(Admin.id))
205205
if username:
206206
count_stmt = count_stmt.where(Admin.username.ilike(f"%{username}%"))
207207
result = await db.execute(count_stmt)
208208
total = result.scalar()
209-
209+
210210
# Get active count (not disabled)
211211
active_stmt = select(func.count(Admin.id))
212212
if username:
213213
active_stmt = active_stmt.where(Admin.username.ilike(f"%{username}%"))
214214
active_stmt = active_stmt.where(Admin.is_disabled.is_(False))
215215
result = await db.execute(active_stmt)
216216
active = result.scalar()
217-
217+
218218
# Get disabled count
219219
disabled_stmt = select(func.count(Admin.id))
220220
if username:

app/jobs/node_checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ async def process_node_health_check(db_node: Node, node: PasarGuardNode):
9292
health = await asyncio.wait_for(verify_node_backend_health(node, db_node.name), timeout=20)
9393
except asyncio.TimeoutError:
9494
if db_node.status == NodeStatus.connected:
95-
logger.warning(f"Node {db_node.id} ({db_node.name}) health check timed out but was previously connected, will retry")
95+
logger.warning(
96+
f"Node {db_node.id} ({db_node.name}) health check timed out but was previously connected, will retry"
97+
)
9698
return
9799
async with GetDB() as db:
98100
await NodeOperation._update_single_node_status(

app/jobs/reset_user_data_usage.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33

44
from app import scheduler
55
from app.db import GetDB
6-
from app.db.models import UserStatus
76
from app.db.crud.user import get_users_to_reset_data_usage, bulk_reset_user_data_usage
8-
from app.models.user import UserNotificationResponse
7+
from app.operation import OperatorType
8+
from app.operation.user import UserOperation
99
from app import notification
10-
from app.core.manager import core_manager
11-
from app.node import node_manager
1210
from app.jobs.dependencies import SYSTEM_ADMIN
1311
from app.utils.logger import get_logger
1412
from config import JOB_RESET_USER_DATA_USAGE_INTERVAL
1513

1614
logger = get_logger("jobs")
15+
user_operator = UserOperation(operator_type=OperatorType.SYSTEM)
1716

1817

1918
async def reset_data_usage():
@@ -24,16 +23,12 @@ async def reset_data_usage():
2423
updated_users = await bulk_reset_user_data_usage(db, users)
2524

2625
for db_user in updated_users:
27-
user = UserNotificationResponse.model_validate(db_user)
26+
user = await user_operator.update_user(db_user)
2827
asyncio.create_task(notification.reset_user_data_usage(user, SYSTEM_ADMIN))
2928

3029
if old_statuses.get(user.id) != user.status:
3130
asyncio.create_task(notification.user_status_change(user, SYSTEM_ADMIN))
3231

33-
# make user active if limited on usage reset
34-
if user.status == UserStatus.active:
35-
await node_manager.update_user(user=user, inbounds=await core_manager.get_inbounds())
36-
3732
logger.info(f'User data usage reset for User "{user.username}"')
3833

3934

app/jobs/review_users.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
update_users_status,
1818
bulk_create_notification_reminders,
1919
)
20+
from app.operation import OperatorType
21+
from app.operation.user import UserOperation
2022
from app.jobs.dependencies import SYSTEM_ADMIN
2123
from app.models.settings import Webhook
2224
from app.models.user import UserNotificationResponse
@@ -26,28 +28,25 @@
2628
from config import JOB_REVIEW_USERS_INTERVAL
2729

2830
logger = get_logger("review-users")
31+
user_operator = UserOperation(operator_type=OperatorType.SYSTEM)
2932

3033

3134
async def reset_user_by_next_report(db: AsyncSession, db_user: User):
3235
db_user = await reset_user_by_next(db, db_user)
33-
inbounds = await db_user.inbounds()
34-
user = UserNotificationResponse.model_validate(db_user)
36+
user = await user_operator.update_user(db_user)
3537

36-
await node_manager.update_user(user, inbounds)
3738
asyncio.create_task(notification.user_data_reset_by_next(user, SYSTEM_ADMIN))
3839

3940
logger.info(f'User "{db_user.username}" next plan activated')
4041

4142

4243
async def change_status(db: AsyncSession, db_user: User, status: UserStatus):
43-
user = UserNotificationResponse.model_validate(db_user)
44-
if user.status is not UserStatus.active:
45-
await node_manager.remove_user(user)
44+
user = await user_operator.update_user(db_user)
4645
asyncio.create_task(notification.user_status_change(user, SYSTEM_ADMIN))
4746

48-
logger.info(f'User "{db_user.username}" status changed to {status.value}')
47+
logger.info(f'User "{user.username}" status changed to {status.value}')
4948

50-
if db_user.next_plan and db_user.status is not UserStatus.active:
49+
if db_user.next_plan and db_user.status != UserStatus.active:
5150
await reset_user_by_next_report(db, db_user)
5251

5352

app/models/admin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ class AdminValidationResult(BaseModel):
115115

116116
class AdminsResponse(BaseModel):
117117
"""Response model for admins list with pagination and statistics."""
118+
118119
admins: list[AdminDetails]
119120
total: int
120121
active: int
121-
disabled: int
122+
disabled: int

app/operation/admin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
)
1616
from app.db.crud.bulk import activate_all_disabled_users, disable_all_active_users
1717
from app.db.crud.user import get_users, remove_users
18-
from app.db.models import Admin as DBAdmin
1918
from app.models.admin import AdminCreate, AdminDetails, AdminModify, AdminsResponse
2019
from app.node import node_manager
2120
from app.operation import BaseOperation, OperatorType
@@ -97,7 +96,7 @@ async def get_admins(
9796
admins, total, active, disabled = await get_admins(
9897
db, offset, limit, username, sort_list if sort_list else None, return_with_count=True
9998
)
100-
99+
101100
return AdminsResponse(
102101
admins=[AdminDetails.model_validate(admin) for admin in admins],
103102
total=total,

app/operation/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ async def update_user(self, db_user: User) -> UserNotificationResponse:
8989
user_inbounds = await db_user.inbounds()
9090
asyncio.create_task(node_manager.update_user(user, inbounds=user_inbounds))
9191
else:
92-
asyncio.create_task(node_manager.update_user(user))
92+
asyncio.create_task(node_manager.remove_user(user))
9393

9494
return user
9595

app/subscription/xray.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -248,27 +248,31 @@ def _transport_quic(self, config: QUICTransportConfig, path: str) -> dict:
248248
"""Handle QUIC transport - only gets QUIC config"""
249249
host = config.host if isinstance(config.host, str) else (config.host[0] if config.host else "")
250250

251-
return self._normalize_and_remove_none_values({
252-
"security": host,
253-
"header": {"type": config.header_type},
254-
"key": path,
255-
})
251+
return self._normalize_and_remove_none_values(
252+
{
253+
"security": host,
254+
"header": {"type": config.header_type},
255+
"key": path,
256+
}
257+
)
256258

257259
def _transport_kcp(self, config: KCPTransportConfig, path: str) -> dict:
258260
"""Handle KCP transport - only gets KCP config"""
259261
host = config.host if isinstance(config.host, str) else (config.host[0] if config.host else "")
260262

261-
return self._normalize_and_remove_none_values({
262-
"header": {"type": config.header_type, "domain": host},
263-
"mtu": config.mtu if config.mtu else 1350,
264-
"tti": config.tti if config.tti else 50,
265-
"uplinkCapacity": config.uplink_capacity if config.uplink_capacity else 12,
266-
"downlinkCapacity": config.downlink_capacity if config.downlink_capacity else 100,
267-
"congestion": config.congestion,
268-
"readBufferSize": config.read_buffer_size if config.read_buffer_size else 2,
269-
"writeBufferSize": config.write_buffer_size if config.write_buffer_size else 2,
270-
"seed": path,
271-
})
263+
return self._normalize_and_remove_none_values(
264+
{
265+
"header": {"type": config.header_type, "domain": host},
266+
"mtu": config.mtu if config.mtu else 1350,
267+
"tti": config.tti if config.tti else 50,
268+
"uplinkCapacity": config.uplink_capacity if config.uplink_capacity else 12,
269+
"downlinkCapacity": config.downlink_capacity if config.downlink_capacity else 100,
270+
"congestion": config.congestion,
271+
"readBufferSize": config.read_buffer_size if config.read_buffer_size else 2,
272+
"writeBufferSize": config.write_buffer_size if config.write_buffer_size else 2,
273+
"seed": path,
274+
}
275+
)
272276

273277
def _apply_transport(self, network: str, inbound: SubscriptionInboundData, path: str) -> dict | None:
274278
"""Apply transport settings using registry pattern"""
@@ -284,15 +288,17 @@ def _apply_tls(self, tls_config: TLSConfig, security: str) -> dict:
284288
sni = tls_config.sni if isinstance(tls_config.sni, str) else (tls_config.sni[0] if tls_config.sni else None)
285289

286290
if security == "reality":
287-
return self._normalize_and_remove_none_values({
288-
"serverName": sni,
289-
"fingerprint": tls_config.fingerprint,
290-
"show": False,
291-
"publicKey": tls_config.reality_public_key,
292-
"shortId": tls_config.reality_short_id,
293-
"spiderX": tls_config.reality_spx,
294-
"mldsa65Verify": tls_config.mldsa65_verify,
295-
})
291+
return self._normalize_and_remove_none_values(
292+
{
293+
"serverName": sni,
294+
"fingerprint": tls_config.fingerprint,
295+
"show": False,
296+
"publicKey": tls_config.reality_public_key,
297+
"shortId": tls_config.reality_short_id,
298+
"spiderX": tls_config.reality_spx,
299+
"mldsa65Verify": tls_config.mldsa65_verify,
300+
}
301+
)
296302
else: # tls
297303
config = {
298304
"serverName": sni,
@@ -356,11 +362,13 @@ def _download_config(self, download_settings: SubscriptionInboundData, link_form
356362
sockopt=sockopt,
357363
)
358364

359-
return self._normalize_and_remove_none_values({
360-
"address": download_settings.address,
361-
"port": self._select_port(download_settings.port),
362-
**stream_settings,
363-
})
365+
return self._normalize_and_remove_none_values(
366+
{
367+
"address": download_settings.address,
368+
"port": self._select_port(download_settings.port),
369+
**stream_settings,
370+
}
371+
)
364372

365373
# ========== Protocol Builders (Registry Methods) ==========
366374

0 commit comments

Comments
 (0)