Skip to content

Commit

Permalink
dms.receive: detect when the protocol doesn't support the requested u…
Browse files Browse the repository at this point in the history
…ser's handle

fixes #1318
  • Loading branch information
snarfed committed Sep 11, 2024
1 parent f3fac5b commit 249aa38
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 14 additions & 6 deletions dms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from oauth_dropins.webutil import util

from common import create_task, memcache, memcache_key
import ids
import models
import protocol

Expand Down Expand Up @@ -117,24 +118,31 @@ def receive(*, from_user, obj):
content = content.removeprefix('@')

if to_proto.owns_handle(content) is not False:
to_id = to_proto.handle_to_id(content)
if not to_id:
return reply(f"Couldn't find {to_proto.PHRASE} user {handle}")
handle = content
from_proto = from_user.__class__

def reply(text, type=None):
maybe_send(from_proto=to_proto, to_user=from_user, text=text, type=type)
return 'OK', 200

try:
ids.translate_handle(handle=handle, from_=to_proto, to=from_user,
enhanced=False)
except ValueError as e:
logger.warning(e)
return reply(f"Sorry, Bridgy Fed doesn't yet support bridging handle {handle} from {to_proto.PHRASE} to {from_proto.PHRASE}.")

to_id = to_proto.handle_to_id(handle)
if not to_id:
return reply(f"Couldn't find {to_proto.PHRASE} user {handle}")

if not from_user.is_enabled(to_proto):
return reply(f'Please bridge your account to {to_proto.PHRASE} by following this account before requesting another user.')

handle = content
to_user = to_proto.get_or_create(to_id)
if not to_user:
return reply(f"Couldn't find {to_proto.PHRASE} user {handle}")

from_proto = from_user.__class__

if not to_user.obj:
# doesn't exist
return reply(f"Couldn't find {to_proto.PHRASE} user {handle}")
Expand Down
16 changes: 15 additions & 1 deletion tests/test_dms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""Unit tests for dms.py."""
from unittest import mock

from atproto import ATProto
from common import memcache
import dms
from dms import maybe_send, receive
import ids
from models import DM, Follower, Object
from web import Web

from oauth_dropins.webutil.flask_util import NotModified
from .testutil import ExplicitEnableFake, Fake, OtherFake, TestCase
from .test_atproto import DID_DOC

DM_EEFAKE_ALICE_REQUESTS_OTHER_BOB = {
'objectType': 'note',
Expand Down Expand Up @@ -292,7 +295,6 @@ def test_receive_handle_request_rate_limit(self):
ExplicitEnableFake.sent)
self.assertEqual(3, memcache.get('dm-user-requests-eefake-eefake:alice'))


def test_receive_handle_wrong_protocol(self):
self.make_user(id='other.brid.gy', cls=Web)

Expand All @@ -307,3 +309,15 @@ def test_receive_handle_wrong_protocol(self):
self.assertEqual([], ExplicitEnableFake.sent)
self.assertEqual([], OtherFake.sent)
self.assertEqual([], Fake.sent)

@mock.patch('ids.translate_handle', side_effect=ValueError('nope'))
def test_receive_handle_not_supported_in_target_protocol(self, _):
alice, bob = self.make_alice_bob()
obj = Object(our_as1=DM_EEFAKE_ALICE_REQUESTS_OTHER_BOB)

self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj))
self.assertEqual(
[('https://other.brid.gy/#?-dm-eefake:alice-2022-01-02T03:04:05+00:00',
'eefake:alice:target')],
ExplicitEnableFake.sent)
self.assertEqual([], OtherFake.sent)

0 comments on commit 249aa38

Please sign in to comment.