|
| 1 | +import pprint |
| 2 | + |
| 3 | +from django.core.management.base import BaseCommand |
| 4 | + |
| 5 | +from mastodon.api import create_app |
| 6 | +from mastodon.models import MastodonApplication |
| 7 | + |
| 8 | + |
| 9 | +class Command(BaseCommand): |
| 10 | + help = "Manage Mastodon sites" |
| 11 | + |
| 12 | + def add_arguments(self, parser): |
| 13 | + # parser.add_argument("domain", type=str, help="mastodon domain") |
| 14 | + parser.add_argument( |
| 15 | + "--refresh", |
| 16 | + action="store_true", |
| 17 | + help="refresh app registration on all sites", |
| 18 | + ) |
| 19 | + |
| 20 | + def handle(self, *args, **options): |
| 21 | + if options["refresh"]: |
| 22 | + for site in MastodonApplication.objects.exclude(disabled=True): |
| 23 | + allow_multiple_redir = True |
| 24 | + response = create_app(site.api_domain, allow_multiple_redir) |
| 25 | + if response.status_code != 200: |
| 26 | + self.stdout.write( |
| 27 | + f"Error creating app on {site.api_domain}: {response.status_code}" |
| 28 | + ) |
| 29 | + continue |
| 30 | + try: |
| 31 | + data = response.json() |
| 32 | + except Exception: |
| 33 | + self.stdout.write( |
| 34 | + f"Error creating app on {site.api_domain}: unable to parse response" |
| 35 | + ) |
| 36 | + continue |
| 37 | + site.app_id = data["id"] |
| 38 | + site.client_id = data["client_id"] |
| 39 | + site.client_secret = data["client_secret"] |
| 40 | + site.vapid_key = data.get("vapid_key") |
| 41 | + site.save( |
| 42 | + update_fields=["app_id", "client_id", "client_secret", "vapid_key"] |
| 43 | + ) |
| 44 | + self.stdout.write(f"updated app on {site.api_domain}") |
| 45 | + self.stdout.write(self.style.SUCCESS("Done.")) |
0 commit comments