chiark / gitweb /
Separate logical instance domain from physical URL.
authorSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 19:15:35 +0000 (19:15 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 2 Dec 2023 19:15:35 +0000 (19:15 +0000)
Some instances don't have the same domain name, e.g. Julia Evans's
personal instance lives at https://social.jvns.ca but the domain name
for username purposes is user@jvns.ca. Now I think we're correctly
distinguishing the two concepts.

client.py
login.py

index 92569efbe6a0daf7ed752a99a95c51ee48acc020..1a051157cc65dd4b99649b556108e1fd8d2588a7 100644 (file)
--- a/client.py
+++ b/client.py
@@ -25,18 +25,18 @@ class Client:
         self.bearer_token = None
         self.log_response = lambda *args, **kws: None
 
-    def set_instance(self, instance):
-        self.instance = instance
+    def set_instance_url(self, instance_url):
+        self.instance_url = instance_url
         self.urls = {
             None: '',
-            'auth': "https://" + instance + "/oauth/",
-            'api': "https://" + instance + "/api/v1/",
+            'auth': instance_url + "/oauth/",
+            'api': instance_url + "/api/v1/",
         }
 
-    def set_username(self, username, account_id):
+    def set_username(self, username, instance_domain, account_id):
         self.username = username
-        self.fq_username = f"{self.username}@{self.instance}"
-        self.at_fq_username = f"@{self.username}@{self.instance}"
+        self.instance_domain = instance_domain
+        self.fq_username = f"{self.username}@{self.instance_domain}"
         self.account_id = account_id
 
     def enable_debug(self, logfile):
index aab5afccfdbabdba7af8d3e6f5dc7cea56275af6..279f28f9ba12e057c3747ac4fa7d1ca51beca942 100644 (file)
--- a/login.py
+++ b/login.py
@@ -12,11 +12,11 @@ class LoginUI(client.Client):
     def run(self):
         redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
 
-        instance = input("Enter a Mastodon instance name: ")
-        if "://" not in instance:
-            instance = "https://" + instance
+        instance_url = input("Enter a Mastodon instance URL: ")
+        if "://" not in instance_url:
+            instance_url = "https://" + instance_url
 
-        self.set_instance(instance)
+        self.set_instance_url(instance_url)
 
         app = self.post(
             "apps",
@@ -66,12 +66,14 @@ class LoginUI(client.Client):
 
         self.bearer_token = user_token['access_token']
         account = self.get("accounts/verify_credentials")
+        instance = self.get("instance")
         self.bearer_token = None
 
         data = {
             'account_id': account['id'],
             'username': account['username'],
-            'instance': instance.split("://", 1)[-1],
+            'instance_url': instance_url,
+            'instance_domain': instance['uri'],
             'client_id': client_id,
             'client_secret': client_secret,
             'user_token': user_token['access_token'],
@@ -96,6 +98,7 @@ def setup_client(cl):
 
     with open(config_file()) as fh:
         data = json.load(fh)
-    cl.set_instance(data['instance'])
-    cl.set_username(data['username'], data['account_id'])
+    cl.set_instance_url(data['instance_url'])
+    cl.set_username(data['username'], data['instance_domain'],
+                    data['account_id'])
     cl.bearer_token = data['user_token']