diff --git a/mastodon/api.py b/mastodon/api.py index b976eca6..4e753012 100644 --- a/mastodon/api.py +++ b/mastodon/api.py @@ -90,14 +90,15 @@ def post_toot(site, content, visibility, token, local_only=False): return response -def get_instance_domain(domain_name): +def get_instance_info(domain_name): if domain_name.lower().strip() == TWITTER_DOMAIN: - return TWITTER_DOMAIN + return TWITTER_DOMAIN, '' try: response = get(f'https://{domain_name}/api/v1/instance', headers={'User-Agent': 'NeoDB/1.0'}) - return response.json()['uri'].lower().split('//')[-1].split('/')[0] + j = response.json() + return j['uri'].lower().split('//')[-1].split('/')[0], j['version'] except: - return domain_name + return domain_name, '' def create_app(domain_name): diff --git a/mastodon/auth.py b/mastodon/auth.py index 697acd65..069ecb66 100644 --- a/mastodon/auth.py +++ b/mastodon/auth.py @@ -36,11 +36,12 @@ def get_mastodon_application(domain): return app, error_msg -def get_mastodon_login_url(app, login_domain, request): +def get_mastodon_login_url(app, login_domain, version, request): url = request.scheme + "://" + request.get_host() + reverse('users:OAuth2_login') if login_domain == TWITTER_DOMAIN: return f"https://twitter.com/i/oauth2/authorize?response_type=code&client_id={app.client_id}&redirect_uri={quote(url)}&scope={quote(settings.TWITTER_CLIENT_SCOPE)}&state=state&code_challenge=challenge&code_challenge_method=plain" - return "https://" + login_domain + "/oauth/authorize?client_id=" + app.client_id + "&scope=" + quote(settings.MASTODON_CLIENT_SCOPE) + "&redirect_uri=" + url + "&response_type=code" + scope = 'read' if 'Pixelfed' in version else settings.MASTODON_CLIENT_SCOPE + return "https://" + login_domain + "/oauth/authorize?client_id=" + app.client_id + "&scope=" + quote(scope) + "&redirect_uri=" + url + "&response_type=code" def obtain_token(site, request, code): diff --git a/mastodon/management/commands/wrong_sites.py b/mastodon/management/commands/wrong_sites.py index e3adeec4..f985a1e7 100644 --- a/mastodon/management/commands/wrong_sites.py +++ b/mastodon/management/commands/wrong_sites.py @@ -1,7 +1,7 @@ from django.core.management.base import BaseCommand from mastodon.models import MastodonApplication from django.conf import settings -from mastodon.api import get_instance_domain +from mastodon.api import get_instance_info from users.models import User @@ -12,7 +12,7 @@ class Command(BaseCommand): for site in MastodonApplication.objects.all(): d = site.domain_name login_domain = d.strip().lower().split('//')[-1].split('/')[0].split('@')[-1] - domain = get_instance_domain(login_domain) + domain, version = get_instance_info(login_domain) if d != domain: print(f'{d} should be {domain}') for u in User.objects.filter(mastodon_site=d, is_active=True): diff --git a/mastodon/models.py b/mastodon/models.py index 67aec03f..49346810 100644 --- a/mastodon/models.py +++ b/mastodon/models.py @@ -8,7 +8,7 @@ class MastodonApplication(models.Model): app_id = models.PositiveIntegerField(_('in-site app id')) # TODO Remove? bc 1) it seems useless 2) GoToSocial returns a hash text id client_id = models.CharField(_('client id'), max_length=100) client_secret = models.CharField(_('client secret'), max_length=100) - vapid_key = models.CharField(_('vapid key'), max_length=200) + vapid_key = models.CharField(_('vapid key'), max_length=200, null=True, blank=True) star_mode = models.PositiveIntegerField(_('0: custom emoji; 1: unicode moon; 2: text'), blank=False, default=0) max_status_len = models.PositiveIntegerField(_('max toot len'), blank=False, default=500) diff --git a/users/views.py b/users/views.py index e1c0d8fe..6e530b6e 100644 --- a/users/views.py +++ b/users/views.py @@ -145,12 +145,12 @@ def connect(request): return redirect(reverse("users:login")) login_domain = request.session['swap_domain'] if request.session.get('swap_login') else request.GET.get('domain') login_domain = login_domain.strip().lower().split('//')[-1].split('/')[0].split('@')[-1] - domain = get_instance_domain(login_domain) + domain, version = get_instance_info(login_domain) app, error_msg = get_mastodon_application(domain) if app is None: return render(request, 'common/error.html', {'msg': error_msg, 'secondary_msg': "", }) else: - login_url = get_mastodon_login_url(app, login_domain, request) + login_url = get_mastodon_login_url(app, login_domain, version, request) resp = redirect(login_url) resp.set_cookie("mastodon_domain", domain) return resp