Skip to content

Commit 5555473

Browse files
committed
feat(subscription): add dynamic variables for subscription title
1 parent 1a14a05 commit 5555473

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

app/operation/subscription.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from app.models.stats import Period, UserUsageStatsList
1212
from app.models.user import SubscriptionUserResponse, UsersResponseWithInbounds
1313
from app.settings import subscription_settings
14-
from app.subscription.share import encode_title, generate_subscription
14+
from app.subscription.share import encode_title, generate_subscription, setup_format_variables
1515
from app.templates import render_template
1616
from config import SUBSCRIPTION_PAGE_TEMPLATE
1717

@@ -58,16 +58,32 @@ def create_response_headers(user: UsersResponseWithInbounds, request_url: str, s
5858
if user.expire:
5959
user_info["expire"] = int(user.expire.timestamp())
6060

61+
# Setup format variables for dynamic title
62+
format_variables = setup_format_variables(user)
63+
64+
# Get profile title and format it with variables
65+
profile_title = (
66+
user.admin.profile_title
67+
if user.admin and user.admin.profile_title
68+
else sub_settings.profile_title
69+
)
70+
71+
# Format profile title with dynamic variables (same as host remark)
72+
if profile_title:
73+
try:
74+
profile_title = profile_title.format_map(format_variables)
75+
except (ValueError, KeyError):
76+
# If formatting fails (e.g., invalid format string), use original title
77+
pass
78+
6179
# Create and return headers
6280
return {
6381
"content-disposition": f'attachment; filename="{user.username}"',
6482
"profile-web-page-url": request_url,
6583
"support-url": user.admin.support_url
6684
if user.admin and user.admin.support_url
6785
else sub_settings.support_url,
68-
"profile-title": encode_title(user.admin.profile_title)
69-
if user.admin and user.admin.profile_title
70-
else encode_title(sub_settings.profile_title),
86+
"profile-title": encode_title(profile_title) if profile_title else encode_title("Subscription"),
7187
"profile-update-interval": str(sub_settings.update_interval),
7288
"subscription-userinfo": "; ".join(f"{key}={val}" for key, val in user_info.items()),
7389
}
@@ -98,9 +114,9 @@ async def user_subscription(
98114
# Handle HTML request (subscription page)
99115
sub_settings: SubSettings = await subscription_settings()
100116
db_user = await self.get_validated_sub(db, token)
101-
response_headers = self.create_response_headers(db_user, request_url, sub_settings)
102-
103117
user = await self.validated_user(db_user)
118+
119+
response_headers = self.create_response_headers(user, request_url, sub_settings)
104120

105121
if "text/html" in accept_header:
106122
template = (
@@ -141,9 +157,9 @@ async def user_subscription_with_client_type(
141157
if client_type == ConfigFormat.block or not getattr(sub_settings.manual_sub_request, client_type):
142158
await self.raise_error(message="Client not supported", code=406)
143159
db_user = await self.get_validated_sub(db, token=token)
144-
response_headers = self.create_response_headers(db_user, request_url, sub_settings)
145-
146160
user = await self.validated_user(db_user)
161+
162+
response_headers = self.create_response_headers(user, request_url, sub_settings)
147163
conf, media_type = await self.fetch_config(user, client_type)
148164

149165
# Create response headers

app/subscription/share.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ def setup_format_variables(user: UsersResponseWithInbounds) -> dict:
9696
if user_status != UserStatus.on_hold:
9797
if expire is not None:
9898
seconds_left = (expire - now).total_seconds()
99-
expire_date = expire.date()
99+
expire_date_obj = expire.date()
100+
expire_date = expire_date_obj.strftime("%Y-%m-%d")
100101
jalali_expire_date = jd.fromgregorian(
101-
year=expire_date.year, month=expire_date.month, day=expire_date.day
102+
year=expire_date_obj.year, month=expire_date_obj.month, day=expire_date_obj.day
102103
).strftime("%Y-%m-%d")
103104
if now < expire:
104105
days_left = (expire - now).days + 1

0 commit comments

Comments
 (0)