Skip to content

Commit

Permalink
refresh Mastodon 4.x site token if unable to verify it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 3, 2025
1 parent 65f340e commit 48d9729
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
30 changes: 17 additions & 13 deletions mastodon/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
)
Expand Down Expand Up @@ -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."
)
21 changes: 20 additions & 1 deletion mastodon/models/mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 48d9729

Please sign in to comment.