refresh Mastodon 4.x site token if unable to verify it.

This commit is contained in:
Your Name 2025-01-03 17:43:58 -05:00 committed by Henri Dickson
parent 65f340ea88
commit ea4f52dfa6
3 changed files with 38 additions and 15 deletions

View file

@ -5,7 +5,7 @@ from django.utils import timezone
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 @@ class MastodonSiteCheck(BaseJob):
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 @@ class MastodonSiteCheck(BaseJob):
"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},
# )
logger.info(
f"Mastodon Site Check finished, {count_checked} checked, {count_unreachable} unreachable, {count_disabled} disabled."
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, {count_refreshed} refreshed."
)

View file

@ -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 @@ class MastodonApplication(models.Model):
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

View file

@ -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"