From 61f5a8efcf9718bfe968a6ecf11c4d1bea20d596 Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 16 Jul 2024 21:36:56 -0700 Subject: [PATCH] move Repo.write_event to Storage.write_event --- README.md | 3 +-- arroba/repo.py | 27 --------------------------- arroba/storage.py | 26 +++++++++++++++++++++++++- arroba/tests/test_repo.py | 26 -------------------------- arroba/tests/test_storage.py | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 59c7510..0ac40c2 100644 --- a/README.md +++ b/README.md @@ -109,9 +109,8 @@ _Non-breaking changes:_ * `mst`: * Add new optional `start` kwarg to `load_all`. -* `repo`: - * Add new `write_event` method. * `storage`: + * Add new `write_event` method. * Add new optional `repo` kwarg to `read_blocks_by_seq` and `read_events_by_seq` to limit returned results to a single repo. * `xrpc_sync`: * Switch `getBlob` from returning HTTP 302 to 301. diff --git a/arroba/repo.py b/arroba/repo.py index 6cc79bd..b6bef15 100644 --- a/arroba/repo.py +++ b/arroba/repo.py @@ -347,30 +347,3 @@ def apply_writes(self, writes): commit_data = Repo.format_commit(repo=self, writes=writes) self.apply_commit(commit_data) return self - - def write_event(self, type, **kwargs): - """Writes a ``subscribeRepos`` event to storage. - - Args: - type (str): ``account`` or ``identity`` - kwargs: included in the event, eg ``active``, `status`` - - Returns: - CID: - """ - assert type in ('account', 'identity', 'tombstone'), type - - seq = self.storage.allocate_seq(SUBSCRIBE_REPOS_NSID) - event = { - '$type': f'com.atproto.sync.subscribeRepos#{type}', - 'seq': seq, - 'did': self.did, - 'time': util.now().isoformat(), - **kwargs - } - block = self.storage.write(self.did, event, seq=seq) - - if self.callback: - self.callback(event) - - return block.cid diff --git a/arroba/storage.py b/arroba/storage.py index ef1d323..9631d27 100644 --- a/arroba/storage.py +++ b/arroba/storage.py @@ -199,7 +199,9 @@ def tombstone_repo(self, repo): repo (Repo) """ self._tombstone_repo(repo) - repo.write_event('tombstone') + block = self.write_event(repo_did=repo.did, type='tombstone') + if repo.callback: + repo.callback(block.decoded) def _tombstone_repo(self, repo): """Marks a repo as tombstoned in storage. @@ -336,6 +338,28 @@ def write(self, repo_did, obj, seq=None): """ raise NotImplementedError() + def write_event(self, repo_did, type, **kwargs): + """Writes a ``subscribeRepos`` event to storage. + + Args: + repo_did (str): + type (str): ``account`` or ``identity`` + kwargs: included in the event, eg ``active``, `status`` + + Returns: + CID: + """ + assert type in ('account', 'identity', 'tombstone'), type + + seq = self.allocate_seq(SUBSCRIBE_REPOS_NSID) + return self.write(repo_did, { + '$type': f'com.atproto.sync.subscribeRepos#{type}', + 'seq': seq, + 'did': repo_did, + 'time': util.now().isoformat(), + **kwargs, + }, seq=seq) + def apply_commit(self, commit_data): """Writes a commit to storage. diff --git a/arroba/tests/test_repo.py b/arroba/tests/test_repo.py index f669209..a095803 100644 --- a/arroba/tests/test_repo.py +++ b/arroba/tests/test_repo.py @@ -191,32 +191,6 @@ def test_apply_commit_callback(self): self.assertEqual(1, len(seen)) self.assertCommitIs(seen[0], create, 2) - def test_write_event(self): - cid = self.repo.write_event('identity', active=False, status='foo') - assert cid - self.assertEqual({ - '$type': 'com.atproto.sync.subscribeRepos#identity', - 'seq': 2, - 'did': 'did:web:user.com', - 'time': NOW.isoformat(), - 'active': False, - 'status': 'foo', - }, self.storage.read(cid).decoded) - - def test_write_event_callback(self): - seen = [] - self.repo.callback = lambda event: seen.append(event) - - cid = self.repo.write_event('account') - block = self.storage.read(cid) - self.assertEqual({ - '$type': 'com.atproto.sync.subscribeRepos#account', - 'seq': 2, - 'did': 'did:web:user.com', - 'time': NOW.isoformat(), - }, block.decoded) - self.assertEqual([block.decoded], seen) - class DatastoreRepoTest(RepoTest, DatastoreTest): """Run all of RepoTest's tests with DatastoreStorage.""" diff --git a/arroba/tests/test_storage.py b/arroba/tests/test_storage.py index 78925a3..ca5fae3 100644 --- a/arroba/tests/test_storage.py +++ b/arroba/tests/test_storage.py @@ -196,3 +196,17 @@ def test_tombstone_repo(self): with self.assertRaises(TombstonedRepo): storage.load_repo('did:user') + + def test_write_event(self): + storage = MemoryStorage() + block = storage.write_event(repo_did='did:user', type='identity', + active=False, status='foo') + self.assertEqual({ + '$type': 'com.atproto.sync.subscribeRepos#identity', + 'seq': 1, + 'did': 'did:user', + 'time': NOW.isoformat(), + 'active': False, + 'status': 'foo', + }, block.decoded) + self.assertEqual(block, storage.read(block.cid))