Skip to content

Commit

Permalink
Fix reception capabilities
Browse files Browse the repository at this point in the history
- Fix parsing of unknown reception capabilities
- Add new `ReceptionCapability` items
- Remove `ReceptionCapabilitiesError` (breaking)
  • Loading branch information
threema-lenny committed Feb 21, 2023
1 parent 40652e0 commit 4008b5b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async def capabilities(self, request):
if (from_, secret) not in pytest.values['msgapi']['api_identities']:
return web.Response(status=401)
elif id_ == 'ECHOECHO':
return web.Response(body=b'text,image,video,file')
return web.Response(body=b'text,image,video,file,quatsch')
elif id_ == '*MOCKING':
return web.Response(body=b'text,image,video,file')
return web.Response(status=404)
Expand Down
27 changes: 17 additions & 10 deletions threema/gateway/_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
IDServerError,
KeyServerError,
MessageServerError,
ReceptionCapabilitiesError,
ReceptionCapabilitiesServerError,
)
from .key import Key
Expand All @@ -36,9 +35,15 @@ class ReceptionCapability(enum.Enum):
"""
text = 'text'
image = 'image'
video = 'video'
group = 'group'
audio = 'audio'
video = 'video'
file = 'file'
poll = 'ballot'
one_to_one_audio_call = 'call'
one_to_one_video_call = 'videocall'
perfect_forward_security = 'pfs'
group_call = 'groupcall'


@aio_run_proxy
Expand Down Expand Up @@ -224,7 +229,8 @@ async def get_id(self, **mode):
@async_ttl_cache(ttl=5 * 60)
async def get_reception_capabilities(self, id_):
"""
Get the reception capabilities of a Threema ID.
Get the reception capabilities of a Threema ID. Unknown capabilities are
being discarded.
Arguments:
- `id_`: A Threema ID.
Expand All @@ -234,13 +240,14 @@ async def get_reception_capabilities(self, id_):
get_coroutine = self._get(self.urls['get_reception_capabilities'].format(id_))
response = await get_coroutine
if response.status == 200:
try:
text = await response.text()
return {ReceptionCapability(capability.strip())
for capability in text.split(',')}
except ValueError as exc:
await response.release()
raise ReceptionCapabilitiesError('Invalid reception capability') from exc
text = await response.text()
capabilities = set()
for capability in text.split(','):
try:
capabilities.add(ReceptionCapability(capability.strip()))
except ValueError:
pass
return capabilities
else:
await raise_server_error(response, ReceptionCapabilitiesServerError)

Expand Down
11 changes: 1 addition & 10 deletions threema/gateway/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'IDServerError',
'GatewayKeyError',
'KeyServerError',
'ReceptionCapabilitiesError',
'ReceptionCapabilitiesServerError',
'CreditsServerError',
'DirectionError',
Expand Down Expand Up @@ -117,15 +116,7 @@ class KeyServerError(GatewayKeyError, GatewayServerError):
}


class ReceptionCapabilitiesError(GatewayError):
"""
An invalid reception capability has been returned.
"""


class ReceptionCapabilitiesServerError(
ReceptionCapabilitiesError, GatewayServerError
):
class ReceptionCapabilitiesServerError(GatewayServerError):
"""
The server responded with an error code while fetching the reception
capabilities of a Threema ID.
Expand Down

0 comments on commit 4008b5b

Please sign in to comment.