Skip to content

Commit

Permalink
move Repo.write_event to Storage.write_event
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jul 17, 2024
1 parent db1eba4 commit 61f5a8e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 56 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 0 additions & 27 deletions arroba/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 25 additions & 1 deletion arroba/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
26 changes: 0 additions & 26 deletions arroba/tests/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
14 changes: 14 additions & 0 deletions arroba/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 61f5a8e

Please sign in to comment.