From 48d9729201e751ca24ec6477e8ce9c3bfd7cac76 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 3 Jan 2025 17:43:58 -0500 Subject: [PATCH] refresh Mastodon 4.x site token if unable to verify it. --- mastodon/jobs.py | 30 +++++++++++++++++------------- mastodon/models/mastodon.py | 21 ++++++++++++++++++++- pyproject.toml | 2 +- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/mastodon/jobs.py b/mastodon/jobs.py index f049f585..b4a27b82 100644 --- a/mastodon/jobs.py +++ b/mastodon/jobs.py @@ -5,7 +5,7 @@ from loguru import logger from common.models import BaseJob, JobManager -from mastodon.models import MastodonApplication, detect_server_info, verify_client +from mastodon.models import MastodonApplication, detect_server_info @JobManager.register @@ -18,6 +18,7 @@ def run(self): count_checked = 0 count_unreachable = 0 count_disabled = 0 + count_refreshed = 0 q = Q(last_reachable_date__lte=timezone.now() - timedelta(days=1)) | Q( last_reachable_date__isnull=True ) @@ -56,17 +57,20 @@ def run(self): "disabled", ] ) - # try: - # if not verify_client(site): - # logger.error( - # f"Unable to verify client app for {site.api_domain}, consider deleting it." - # ) - # # site.delete() - # except Exception as e: - # logger.error( - # f"Failed to verify client app for {site.api_domain}", - # extra={"exception": e}, - # ) + try: + if ( + site.server_version.startswith("4.") + and "(compatible;" not in site.server_version + and not site.verify() + ): + # always verify token from Mastodon 4.x + site.refresh() + count_refreshed += 1 + except Exception as e: + logger.error( + f"Failed to verify/refresh client app for {site.api_domain}", + extra={"exception": e}, + ) logger.info( - f"Mastodon Site Check finished, {count_checked} checked, {count_unreachable} unreachable, {count_disabled} disabled." + f"Mastodon Site Check finished, {count_checked} checked, {count_unreachable} unreachable, {count_disabled} disabled, {count_refreshed} refreshed." ) diff --git a/mastodon/models/mastodon.py b/mastodon/models/mastodon.py index 37b3d176..2e1d94c5 100644 --- a/mastodon/models/mastodon.py +++ b/mastodon/models/mastodon.py @@ -440,7 +440,7 @@ def get_toot_visibility(visibility, user) -> TootVisibilityEnum: return TootVisibilityEnum.UNLISTED -def get_or_create_fediverse_application(login_domain): +def get_or_create_fediverse_application(login_domain: str): domain = login_domain app = MastodonApplication.objects.filter(domain_name__iexact=domain).first() if not app: @@ -556,6 +556,25 @@ def detect_configurations(self): if next(filter(lambda e: e["shortcode"] == "star_half", j), None): self.star_mode = 1 + def verify(self): + return verify_client(self) + + def refresh(self): + response = create_app(self.api_domain, self.server_version) + if response.status_code != 200: + logger.error( + f"Error creating app for {self.domain_name} on {self.api_domain}: {response.status_code}" + ) + return False + data = response.json() + self.app_id = data["id"] + self.client_id = data["client_id"] + self.client_secret = data["client_secret"] + self.vapid_key = data.get("vapid_key", "") + self.save() + logger.info(f"Refreshed {self.api_domain}") + return True + class Mastodon: @staticmethod diff --git a/pyproject.toml b/pyproject.toml index 4140dcf2..80fc2288 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "neodb" -version = "0.10" +version = "0.11" description = "🧩 self-hosted server tracking what you read/watch/listen/play, powering a global distributed community federating via ActivityPub." readme = "README.md" requires-python = ">= 3.12"