feat: add PreferSecureProtocol in config.ini

This commit is contained in:
Macintxsh 2025-05-21 05:06:45 +03:00
parent b266a1a2df
commit d4d460a2a0
Signed by: mctaylors
GPG key ID: 4EEF4F949A266EE2
4 changed files with 22 additions and 13 deletions

View file

@ -71,7 +71,8 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
keyboard = [ keyboard = [
[ [
InlineKeyboardButton( InlineKeyboardButton(
f"Open in {app.name}", url=f"http://{app.hostname}/posts/{post_id}" f"Open in {app.name}",
url=f"{app.protocol}://{app.hostname}/posts/{post_id}",
) )
] ]
] ]
@ -82,28 +83,28 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
m.append(f"ID: {html_parser.code(post_data['id'])}") m.append(f"ID: {html_parser.code(post_data['id'])}")
m.append( m.append(
f"Uploader: {html_parser.hyperlink(uploader_data['name'], f"Uploader: {html_parser.hyperlink(uploader_data['name'],
f"http://{app.hostname}/users/{post_data['uploader_id']}")} " f"{app.protocol}://{app.hostname}/users/{post_data['uploader_id']}")} "
f"{html_parser.hyperlink("»", f"http://{app.hostname}/posts?tags=user:{uploader_data['name']}")}" f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=user:{uploader_data['name']}")}"
) )
created_at = datetime.fromisoformat(post_data["created_at"]) created_at = datetime.fromisoformat(post_data["created_at"])
m.append( m.append(
f"Date: {html_parser.hyperlink( f"Date: {html_parser.hyperlink(
f"{created_at.strftime("%Y-%m-%d %X (%z)")}", f"{created_at.strftime("%Y-%m-%d %X (%z)")}",
f"http://{app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}" f"{app.protocol}://{app.hostname}/posts?tags=date:{created_at.strftime("%Y-%m-%d")}")}"
) )
if post_data["approver_id"] is not None: if post_data["approver_id"] is not None:
approver_data = get_json(f"users/{post_data['approver_id']}") approver_data = get_json(f"users/{post_data['approver_id']}")
m.append( m.append(
f"Approver: {html_parser.hyperlink(approver_data['name'], f"Approver: {html_parser.hyperlink(approver_data['name'],
f"http://{app.hostname}/users/{post_data['approver_id']}")} " f"{app.protocol}://{app.hostname}/users/{post_data['approver_id']}")} "
f"{html_parser.hyperlink("»", f"http://{app.hostname}/posts?tags=approver:{approver_data['name']}")}" f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/posts?tags=approver:{approver_data['name']}")}"
) )
m.append( m.append(
f"Size: {html_parser.hyperlink( f"Size: {html_parser.hyperlink(
f"{humanize_filesize(post_data['media_asset']['file_size'])} .{post_data['media_asset']['file_ext']}", f"{humanize_filesize(post_data['media_asset']['file_size'])} .{post_data['media_asset']['file_ext']}",
"" if post_data['is_banned'] else post_data['file_url'])} " "" if post_data['is_banned'] else post_data['file_url'])} "
f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']}) " f"({post_data['media_asset']['image_width']}x{post_data['media_asset']['image_height']}) "
f"{html_parser.hyperlink("»", f"http://{app.hostname}/media_assets/{post_data['media_asset']['id']}")}" f"{html_parser.hyperlink("»", f"{app.protocol}://{app.hostname}/media_assets/{post_data['media_asset']['id']}")}"
) )
m.append( m.append(
f"Source: {post_data['source'] if post_data['source'] != "" else "🚫"}" f"Source: {post_data['source'] if post_data['source'] != "" else "🚫"}"
@ -111,12 +112,12 @@ async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No
m.append(f"Rating: {format_rating(post_data['rating'])}") m.append(f"Rating: {format_rating(post_data['rating'])}")
m.append( m.append(
f"Score: {html_parser.hyperlink(post_data['score'], f"Score: {html_parser.hyperlink(post_data['score'],
f"http://{app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact")} " f"{app.protocol}://{app.hostname}/post_votes?search[post_id]={post_data['id']}&variant=compact")} "
f"(+{post_data['up_score']} / -{post_data['down_score']})" f"(+{post_data['up_score']} / -{post_data['down_score']})"
) )
m.append( m.append(
f"Favorites: {html_parser.hyperlink(post_data['fav_count'], f"Favorites: {html_parser.hyperlink(post_data['fav_count'],
f"http://{app.hostname}/posts/{post_data['id']}/favorites")}" f"{app.protocol}://{app.hostname}/posts/{post_data['id']}/favorites")}"
) )
m.append(f"Status: {format_status(post_data)}") m.append(f"Status: {format_status(post_data)}")

View file

@ -10,8 +10,12 @@ SourceUrl = git.mctaylors.ru
; Application name (e.g. Danbooru). ; Application name (e.g. Danbooru).
Name = Danbooru Name = Danbooru
; Host (without http://) used to access the application API. ; Host (without http://) used to access the application API.
Host = stashboo.ru Host = danbooru.donmai.us
; (Optional) Public host name (without http://) for the “Open in [App.Name]” button. ; (Optional) Public hostname (without http://) for example, the “Open in [App.Name]” button.
; Useful if the bot is hosted on the same server as the application. ; Useful if the bot is hosted on the same server as the application.
; If not set, [App.Host] will be used instead. ; If not set, [App.Host] will be used instead.
HostName = HostName =
; Prefer the secure HTTPS protocol for generating links that use a public hostname.
; If True, bot will generate HTTPS links.
; If False, bot will generate HTTP links.
PreferSecureProtocol = True

View file

@ -39,6 +39,9 @@ class _App:
self.hostname = _c.get("App", "HostName") self.hostname = _c.get("App", "HostName")
if not self.hostname: if not self.hostname:
self.hostname = self.host self.hostname = self.host
self.protocol = "http"
if eval(_c.get("App", "PreferSecureProtocol")):
self.protocol = "https"
general = _General() general = _General()

View file

@ -67,7 +67,8 @@ async def answer_query(update: Update, query: str, data) -> None:
keyboard = [ keyboard = [
[ [
InlineKeyboardButton( InlineKeyboardButton(
f"Open in {app.name}", url=f"http://{app.hostname}/posts/{query}" f"Open in {app.name}",
url=f"{app.protocol}://{app.hostname}/posts/{query}",
), ),
InlineKeyboardButton("View original", url=data["file_url"]), InlineKeyboardButton("View original", url=data["file_url"]),
] ]