Skip to content

Commit

Permalink
ATProto.create_for: don't add user copy id until the end
Browse files Browse the repository at this point in the history
...so that we know we've fully successfully created the repo, profile, etc

for https://console.cloud.google.com/errors/detail/CID-1fzFl4iVwwE;locations=global;time=P30D?project=bridgy-federated
  • Loading branch information
snarfed committed Feb 18, 2025
1 parent 8419b77 commit 2d3a809
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,6 @@ def create_for(cls, user):
also_known_as=user.profile_id())

Object.get_or_create(did_plc.did, raw=did_plc.doc, authed_as=did_plc)
# TODO: move this to ATProto.get_or_create?
add(user.copies, Target(uri=did_plc.did, protocol='atproto'))

cls.set_dns(handle=handle, did=did_plc.did)

Expand Down Expand Up @@ -481,6 +479,9 @@ def create_for(cls, user):
signing_key=did_plc.signing_key,
rotation_key=did_plc.rotation_key)

# don't add the copy id until the end, here, until we've fully
# successfully created the repo, profile, etc
user.add('copies', Target(uri=did_plc.did, protocol='atproto'))
user.put()

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions tests/test_atproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,19 @@ def test_create_for_tombstoned(self, mock_get, mock_post, mock_create_task,

mock_create_task.assert_called() # atproto-commit

@patch.object(tasks_client, 'create_task', return_value=Task(name='my task'))
@patch.object(Repo, 'create', side_effect=AssertionError('nope'))
@patch('requests.post', return_value=requests_response('OK')) # create DID on PLC
def test_create_for_error_doesnt_add_to_copies(self, _, __, ___):
user = self.make_user(id='fake:user', cls=Fake)
assert not user.obj.as1

with self.assertRaises(AssertionError):
ATProto.create_for(user)

self.assertEqual([], user.copies)
self.assertEqual([], user.key.get().copies)

@patch('atproto.DEBUG', new=False)
@patch.object(google.cloud.dns.client.ManagedZone, 'changes')
@patch.object(atproto.dns_discovery_api, 'resourceRecordSets')
Expand Down

0 comments on commit 2d3a809

Please sign in to comment.