Skip to content

Commit

Permalink
fix versioning for limited federation instances
Browse files Browse the repository at this point in the history
  • Loading branch information
halcy authored and halcy committed Nov 19, 2022
1 parent 853cd82 commit bc2cb5e
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions mastodon/Mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,14 @@ def __init__(self, client_id=None, client_secret=None, access_token=None,
self.mastodon_major = 1
self.mastodon_minor = 0
self.mastodon_patch = 0
self.version_check_worked = None

# Versioning
if mastodon_version == None and self.version_check_mode != 'none':
self.retrieve_mastodon_version()
elif self.version_check_mode != 'none':
try:
self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(
mastodon_version)
self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(mastodon_version)
except:
raise MastodonVersionError("Bad version specified")

Expand All @@ -477,9 +477,11 @@ def retrieve_mastodon_version(self):
"""
try:
version_str = self.__instance()["version"].split('+')[0]
self.version_check_worked = True
except:
# instance() was added in 1.1.0, so our best guess is 1.0.0.
version_str = "1.0.0"
self.version_check_worked = False

self.mastodon_major, self.mastodon_minor, self.mastodon_patch = parse_version_string(version_str)
return version_str
Expand Down Expand Up @@ -585,20 +587,16 @@ def log_in(self, username=None, password=None, code=None, redirect_uri="urn:ietf
Returns the access token as a string.
"""
if username is not None and password is not None:
params = self.__generate_params(
locals(), ['scopes', 'to_file', 'code', 'refresh_token'])
params = self.__generate_params(locals(), ['scopes', 'to_file', 'code', 'refresh_token'])
params['grant_type'] = 'password'
elif code is not None:
params = self.__generate_params(
locals(), ['scopes', 'to_file', 'username', 'password', 'refresh_token'])
params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'refresh_token'])
params['grant_type'] = 'authorization_code'
elif refresh_token is not None:
params = self.__generate_params(
locals(), ['scopes', 'to_file', 'username', 'password', 'code'])
params = self.__generate_params(locals(), ['scopes', 'to_file', 'username', 'password', 'code'])
params['grant_type'] = 'refresh_token'
else:
raise MastodonIllegalArgumentError(
'Invalid arguments given. username and password or code are required.')
raise MastodonIllegalArgumentError('Invalid arguments given. username and password or code are required.')

params['client_id'] = self.client_id
params['client_secret'] = self.client_secret
Expand All @@ -611,11 +609,9 @@ def log_in(self, username=None, password=None, code=None, redirect_uri="urn:ietf
self.__set_token_expired(int(response.get('expires_in', 0)))
except Exception as e:
if username is not None or password is not None:
raise MastodonIllegalArgumentError(
'Invalid user name, password, or redirect_uris: %s' % e)
raise MastodonIllegalArgumentError('Invalid user name, password, or redirect_uris: %s' % e)
elif code is not None:
raise MastodonIllegalArgumentError(
'Invalid access token or redirect_uris: %s' % e)
raise MastodonIllegalArgumentError('Invalid access token or redirect_uris: %s' % e)
else:
raise MastodonIllegalArgumentError('Invalid request: %s' % e)

Expand All @@ -635,6 +631,10 @@ def log_in(self, username=None, password=None, code=None, redirect_uri="urn:ietf

self.__logged_in_id = None

# Retry version check if needed (might be required in limited federation mode)
if self.version_check_worked == False:
self.retrieve_mastodon_version()

return response['access_token']


Expand Down Expand Up @@ -1427,8 +1427,7 @@ def __ensure_search_params_acceptable(self, account_id, offset, min_id, max_id):
"""
if not account_id is None or not offset is None or not min_id is None or not max_id is None:
if self.verify_minimum_version("2.8.0", cached=True) == False:
raise MastodonVersionError(
"Advanced search parameters require Mastodon 2.8.0+")
raise MastodonVersionError("Advanced search parameters require Mastodon 2.8.0+")

@api_version("1.1.0", "2.8.0", __DICT_VERSION_SEARCHRESULT)
def search(self, q, resolve=True, result_type=None, account_id=None, offset=None, min_id=None, max_id=None, exclude_unreviewed=True):
Expand Down Expand Up @@ -1457,8 +1456,7 @@ def search(self, q, resolve=True, result_type=None, account_id=None, offset=None
Returns a `search result dict`_, with tags as `hashtag dicts`_.
"""
if self.verify_minimum_version("2.4.1", cached=True) == True:
return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id,
offset=offset, min_id=min_id, max_id=max_id)
return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id, offset=offset, min_id=min_id, max_id=max_id)
else:
self.__ensure_search_params_acceptable(
account_id, offset, min_id, max_id)
Expand Down

0 comments on commit bc2cb5e

Please sign in to comment.