Improved formatter helper functions

This commit is contained in:
Frisk 2021-05-15 12:58:33 +02:00
parent 06e2d9f7ca
commit 662c7368e3
No known key found for this signature in database
GPG key ID: 213F7C15068AF8AC

View file

@ -16,6 +16,8 @@ from __future__ import annotations
import re import re
from urllib.parse import quote from urllib.parse import quote
from typing import Optional, Callable, TYPE_CHECKING from typing import Optional, Callable, TYPE_CHECKING
from src.exceptions import ServerError, MediaWikiError
from src.discord.message import DiscordMessage from src.discord.message import DiscordMessage
from src.configloader import settings from src.configloader import settings
import src.misc import src.misc
@ -80,10 +82,12 @@ def compact_author(ctx: Context, change: dict) -> (Optional[str], Optional[str])
"""Returns link to the author and the author itself respecting the settings""" """Returns link to the author and the author itself respecting the settings"""
author, author_url = None, None author, author_url = None, None
if ctx.event != "suppressed": if ctx.event != "suppressed":
author_url = clean_link(create_article_path("User:{user}".format(user=change["user"]))) # TODO Sanitize user in here and in embed_helper author_url = clean_link(create_article_path("User:{user}".format(user=sanitize_to_url(change["user"]))))
if "anon" in change: if "anon" in change:
change["user"] = _("Unregistered user") if settings.get("hide_ips", False):
author = change["user"] author = _("Unregistered user")
else:
author = change["user"]
else: else:
author = change["user"] author = change["user"]
return author, author_url return author, author_url
@ -99,41 +103,41 @@ def embed_helper(ctx: Context, message: DiscordMessage, change: dict, set_user=T
adding tags (if the log is tagged anyhow) adding tags (if the log is tagged anyhow)
setting default description (to ctx.parsedcomment)""" setting default description (to ctx.parsedcomment)"""
if set_user: if set_user:
# TODO Repurpose it so change['user'] stays the same author = None
if "anon" in change: if "anon" in change:
author_url = create_article_path("Special:Contributions/{user}".format( author_url = create_article_path("Special:Contributions/{user}".format(user=sanitize_to_url(change["user"])))
user=change["user"].replace(" ", "_"))) # Replace here needed in case of #75
ip_mapper = ctx.client.get_ipmapper() ip_mapper = ctx.client.get_ipmapper()
logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys())) logger.debug("current user: {} with cache of IPs: {}".format(change["user"], ip_mapper.keys()))
if change["user"] not in list(ip_mapper.keys()): if change["user"] not in list(ip_mapper.keys()):
contibs = ctx.client.make_api_request( try:
"{wiki}?action=query&format=json&list=usercontribs&uclimit=max&ucuser={user}&ucstart={timestamp}&ucprop=".format( contibs = ctx.client.make_api_request(
wiki=ctx.client.WIKI_API_PATH, user=change["user"], timestamp=change["timestamp"]), "query", "{wiki}?action=query&format=json&list=usercontribs&uclimit=max&ucuser={user}&ucstart={timestamp}&ucprop=".format(
"usercontribs") wiki=ctx.client.WIKI_API_PATH, user=sanitize_to_url(change["user"]), timestamp=change["timestamp"]), "query",
if contibs is None: "usercontribs")
logger.warning( except (ServerError, MediaWikiError):
"WARNING: Something went wrong when checking amount of contributions for given IP address") logger.warning("WARNING: Something went wrong when checking amount of contributions for given IP address")
if settings.get("hide_ips", False): if settings.get("hide_ips", False):
change["user"] = _("Unregistered user") author = _("Unregistered user")
change["user"] = change["user"] + "(?)" else:
author = change["user"] + "(?)"
else: else:
ip_mapper[change["user"]] = len(contibs) ip_mapper[change["user"]] = len(contibs)
logger.debug( logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper))
"Current params user {} and state of map_ips {}".format(change["user"], ip_mapper))
if settings.get("hide_ips", False): if settings.get("hide_ips", False):
change["user"] = _("Unregistered user") author = _("Unregistered user")
change["user"] = "{author} ({contribs})".format(author=change["user"], contribs=len(contibs)) else:
author = "{author} ({contribs})".format(author=change["user"], contribs=len(contibs))
else: else:
logger.debug( logger.debug("Current params user {} and state of map_ips {}".format(change["user"], ip_mapper))
"Current params user {} and state of map_ips {}".format(change["user"], ip_mapper))
if ctx.event in ("edit", "new"): if ctx.event in ("edit", "new"):
ip_mapper[change["user"]] += 1 ip_mapper[change["user"]] += 1
change["user"] = "{author} ({amount})".format( author = "{author} ({amount})".format(
author=change["user"] if settings.get("hide_ips", False) is False else _("Unregistered user"), author=change["user"] if settings.get("hide_ips", False) is False else _("Unregistered user"),
amount=ip_mapper[change["user"]]) amount=ip_mapper[change["user"]])
else: else:
author_url = create_article_path("User:{}".format(change["user"].replace(" ", "_"))) author_url = create_article_path("User:{}".format(sanitize_to_url(change["user"])))
message.set_author(change["user"], author_url) author = change["user"]
message.set_author(author, author_url)
if set_edit_meta: if set_edit_meta:
if settings["appearance"]["embed"]["show_footer"]: if settings["appearance"]["embed"]["show_footer"]:
message["timestamp"] = change["timestamp"] message["timestamp"] = change["timestamp"]