From f6d9c4afca7d5cf621adb8bbd4fdc15444346e7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 8 Jun 2023 16:43:04 +0200 Subject: [PATCH] Continued: - moved templates to templates/views/ - introduced base.html which should be extended by view templates - TOP x lists now have an own view template --- api.py | 19 +++- fba/federation/gotosocial.py | 22 ++--- fba/federation/lemmy.py | 11 +-- fba/federation/mastodon.py | 4 +- fba/federation/misskey.py | 2 +- fba/federation/peertube.py | 2 +- templates/base.html | 109 ++++++++++++++++++++++ templates/index.html | 156 -------------------------------- templates/scoreboard.html | 123 ------------------------- templates/views/index.html | 44 +++++++++ templates/views/scoreboard.html | 55 +++++++++++ templates/views/top.html | 61 +++++++++++++ 12 files changed, 304 insertions(+), 304 deletions(-) create mode 100644 templates/base.html delete mode 100644 templates/index.html delete mode 100644 templates/scoreboard.html create mode 100644 templates/views/index.html create mode 100644 templates/views/scoreboard.html create mode 100644 templates/views/top.html diff --git a/api.py b/api.py index ebb18b2..49a5c95 100644 --- a/api.py +++ b/api.py @@ -166,7 +166,7 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference elif not response.ok: raise HTTPException(status_code=response.status_code, detail=response.text) - return templates.TemplateResponse("scoreboard.html", { + return templates.TemplateResponse("views/scoreboard.html", { "base_url" : config.get("base_url"), "slogan" : config.get("slogan"), "request" : request, @@ -180,9 +180,22 @@ def index(request: Request, blockers: int = None, blocked: int = None, reference }) @router.get(config.get("base_url") + "/") +def index(request: Request): + # Get info + response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/info.json") + + if not response.ok: + raise HTTPException(status_code=response.status_code, detail=response.text) + + return templates.TemplateResponse("views/index.html", { + "request": request, + "info" : response.json() + }) + +@router.get(config.get("base_url") + "/top") def index(request: Request, domain: str = None, reason: str = None, reverse: str = None): if domain == "" or reason == "" or reverse == "": - return fastapi.responses.RedirectResponse("/") + raise HTTPException(status_code=500, detail="Insufficient parameter provided") response = requests.get(f"http://{config.get('host')}:{config.get('port')}{config.get('base_url')}/api/info.json") @@ -214,7 +227,7 @@ def index(request: Request, domain: str = None, reason: str = None, reverse: str block["first_seen"] = datetime.utcfromtimestamp(block["first_seen"]).strftime(config.get("timestamp_format")) block["last_seen"] = datetime.utcfromtimestamp(block["last_seen"]).strftime(config.get("timestamp_format")) - return templates.TemplateResponse("index.html", { + return templates.TemplateResponse("views/top.html", { "request": request, "domain" : domain, "blocks" : blocklist, diff --git a/fba/federation/gotosocial.py b/fba/federation/gotosocial.py index 2ca6309..00a367f 100644 --- a/fba/federation/gotosocial.py +++ b/fba/federation/gotosocial.py @@ -50,15 +50,15 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): print(f"INFO: Checking {len(federation)} entries from domain='{domain}',software='gotosocial' ...") for peer in federation: blocked = peer["domain"].lower() - # DEBUG: print("DEBUG: BEFORE blocked:", blocked) + print("DEBUG: BEFORE blocked:", blocked) blocked = fba.tidyup_domain(blocked) - # DEBUG: print("DEBUG: AFTER blocked:", blocked) + print("DEBUG: AFTER blocked:", blocked) if blocked == "": print("WARNING: blocked is empty:", domain) continue elif blacklist.is_blacklisted(blocked): - # DEBUG: print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!") + print(f"DEBUG: blocked='{blocked}' is blacklisted - skipping!") continue elif blocked.count("*") > 0: # GTS does not have hashes for obscured domains, so we have to guess it @@ -78,16 +78,16 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): print(f"WARNING: blocked='{blocked}',software='gotosocial' is not a valid domain name - skipped!") continue - # DEBUG: print("DEBUG: Looking up instance by domain:", blocked) + print("DEBUG: Looking up instance by domain:", blocked) if not validators.domain(blocked): print(f"WARNING: blocked='{blocked}',software='gotosocial' is not a valid domain name - skipped!") continue elif not instances.is_registered(blocked): - # DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'") + print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'") instances.add(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url) if not blocks.is_instance_blocked(domain, blocked, "reject"): - # DEBUG: print(f"DEBUG: domain='{domain}' is blocking '{blocked}' for unknown reason at this point") + print(f"DEBUG: domain='{domain}' is blocking '{blocked}' for unknown reason at this point") blocks.add_instance(domain, blocked, "unknown", "reject") blockdict.append({ @@ -95,21 +95,21 @@ def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): "reason" : None }) else: - # DEBUG: print(f"DEBUG: Updating block last seen for domain='{domain}',blocked='{blocked}' ...") + print(f"DEBUG: Updating block last seen for domain='{domain}',blocked='{blocked}' ...") blocks.update_last_seen(domain, blocked, "reject") if "public_comment" in peer: - # DEBUG: print("DEBUG: Updating block reason:", domain, blocked, peer["public_comment"]) + print("DEBUG: Updating block reason:", domain, blocked, peer["public_comment"]) blocks.update_reason(peer["public_comment"], domain, blocked, "reject") for entry in blockdict: if entry["blocked"] == blocked: - # DEBUG: print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'") + print(f"DEBUG: Setting block reason for blocked='{blocked}':'{peer['public_comment']}'") entry["reason"] = peer["public_comment"] - # DEBUG: print("DEBUG: Committing changes ...") + print("DEBUG: Committing changes ...") fba.connection.commit() except Exception as e: print(f"ERROR: domain='{domain}',software='gotosocial',exception[{type(e)}]:'{str(e)}'") - # DEBUG: print("DEBUG: EXIT!") + print("DEBUG: EXIT!") diff --git a/fba/federation/lemmy.py b/fba/federation/lemmy.py index 9f9ebf9..46b2b8e 100644 --- a/fba/federation/lemmy.py +++ b/fba/federation/lemmy.py @@ -14,22 +14,20 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import sys - from fba import config from fba import fba from fba import instances def get_peers(domain: str) -> list: - # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software={software} - CALLED!") + # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software='lemmy' - CALLED!") if type(domain) != str: raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'") elif domain == "": raise ValueError(f"Parameter 'domain' is empty") - # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...") peers = list() try: + # DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...") response = fba.get_response(domain, "/api/v3/site", fba.api_headers, (config.get("connection_timeout"), config.get("read_timeout"))) data = fba.json_from_response(response) @@ -37,17 +35,16 @@ def get_peers(domain: str) -> list: # DEBUG: print(f"DEBUG: response.ok={response.ok},response.status_code='{response.status_code}',data[]='{type(data)}'") if not response.ok or response.status_code >= 400: print("WARNING: Could not reach any JSON API:", domain) - fba.update_last_error(domain, response) + instances.update_last_error(domain, response) elif response.ok and isinstance(data, list): print(f"UNSUPPORTED: domain='{domain}' returned a list: '{data}'") - sys.exit(255) elif "federated_instances" in data: # DEBUG: print(f"DEBUG: Found federated_instances for domain='{domain}'") peers = peers + fba.add_peers(data["federated_instances"]) # DEBUG: print("DEBUG: Added instance(s) to peers") else: print("WARNING: JSON response does not contain 'federated_instances':", domain) - fba.update_last_error(domain, response) + instances.update_last_error(domain, response) except BaseException as e: print(f"WARNING: Exception during fetching JSON: domain='{domain}',exception[{type(e)}]:'{str(e)}'") diff --git a/fba/federation/mastodon.py b/fba/federation/mastodon.py index e03a7d7..11eac1b 100644 --- a/fba/federation/mastodon.py +++ b/fba/federation/mastodon.py @@ -71,7 +71,7 @@ def fetch_blocks_from_about(domain: str) -> dict: ) except BaseException as e: print("ERROR: Cannot fetch from domain:", domain, e) - fba.update_last_error(domain, e) + instances.update_last_error(domain, e) return {} for header in doc.find_all("h3"): @@ -105,7 +105,7 @@ def fetch_blocks_from_about(domain: str) -> dict: } def fetch_blocks(domain: str, origin: str, nodeinfo_url: str): - print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!") + # DEBUG: print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!") if type(domain) != str: raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'") elif domain == "": diff --git a/fba/federation/misskey.py b/fba/federation/misskey.py index d77e37c..a995b14 100644 --- a/fba/federation/misskey.py +++ b/fba/federation/misskey.py @@ -71,7 +71,7 @@ def get_peers(domain: str) -> list: # DEBUG: print(f"DEBUG: fetched({len(fetched)})[]={type(fetched)}") if isinstance(fetched, dict) and "error" in fetched and "message" in fetched["error"]: print(f"WARNING: post_json_api() returned error: {fetched['error']['message']}") - fba.update_last_error(domain, fetched["error"]["message"]) + instances.update_last_error(domain, fetched["error"]["message"]) break already = 0 diff --git a/fba/federation/peertube.py b/fba/federation/peertube.py index dc54da5..8e839e3 100644 --- a/fba/federation/peertube.py +++ b/fba/federation/peertube.py @@ -19,7 +19,7 @@ from fba import fba from fba import instances def get_peers(domain: str) -> list: - # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software={software} - CALLED!") + # DEBUG: print(f"DEBUG: domain({len(domain)})={domain},software='peertube' - CALLED!") if type(domain) != str: raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'") elif domain == "": diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..6933f3e --- /dev/null +++ b/templates/base.html @@ -0,0 +1,109 @@ + + + fedi-block-api - {% block title %}{% endblock %} + {% block rss %} + + {% endblock %} + + + + + +
+ {% block content %}{% endblock %} +
+ +