diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index 9fd51a4..dc40593 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -24,26 +24,35 @@ DICTPROXY_TRANSACTION_CHARS = "SBC" class Notifier: - def __init__(self, metadata_dir): - self.metadata_dir = metadata_dir + def __init__(self, vmail_dir): + self.vmail_dir = vmail_dir self.to_notify_queue = Queue() - def set_token(self, guid, token): - guid_path = self.metadata_dir.joinpath(guid) - if not guid_path.exists(): - guid_path.mkdir() - token_path = guid_path / "token" + def get_metadata_dir(self, mbox): + mbox_path = self.vmail_dir.joinpath(mbox) + if not mbox_path.exists(): + mbox_path.mkdir() + metadata_dir = mbox_path / "metadata" + if not metadata_dir.exists(): + metadata_dir.mkdir() + return metadata_dir + + def set_token(self, mbox, token): + metadata_dir = self.get_metadata_dir(mbox) + token_path = metadata_dir / "token" write_path = token_path.with_suffix(".tmp") write_path.write_text(token) write_path.rename(token_path) - def del_token(self, guid): - self.metadata_dir.joinpath(guid).joinpath("token").unlink(missing_ok=True) + def del_token(self, mbox): + metadata_dir = self.get_metadata_dir(mbox) + if metadata_dir is not None: + metadata_dir.joinpath("token").unlink(missing_ok=True) - def get_token(self, guid): - guid_path = self.metadata_dir / guid - if guid_path.exists(): - token_path = guid_path / "token" + def get_token(self, mbox): + metadata_dir = self.get_metadata_dir(mbox) + if metadata_dir is not None: + token_path = metadata_dir / "token" if token_path.exists(): return token_path.read_text() @@ -95,7 +104,6 @@ def handle_dovecot_request(msg, transactions, notifier): # Lpriv/43f5f508a7ea0366dff30200c15250e3/devicetoken\tlkj123poi@c2.testrun.org keyparts = parts[0].split("/") if keyparts[0] == "priv": - # guid = keyparts[1] keyname = keyparts[2] mbox = parts[1] if keyname == "devicetoken": diff --git a/chatmaild/src/chatmaild/tests/test_metadata.py b/chatmaild/src/chatmaild/tests/test_metadata.py index c8d01e4..89fdd9a 100644 --- a/chatmaild/src/chatmaild/tests/test_metadata.py +++ b/chatmaild/src/chatmaild/tests/test_metadata.py @@ -10,30 +10,32 @@ from chatmaild.metadata import ( @pytest.fixture def notifier(tmp_path): - metadata_dir = tmp_path.joinpath("metadata") - metadata_dir.mkdir() - return Notifier(metadata_dir) + vmail_dir = tmp_path.joinpath("vmaildir") + vmail_dir.mkdir() + return Notifier(vmail_dir) def test_notifier_persistence(tmp_path): - metadata_dir = tmp_path.joinpath("metadata") - metadata_dir.mkdir() - notifier1 = Notifier(metadata_dir) - notifier2 = Notifier(metadata_dir) - assert notifier1.get_token(guid="guid00") is None - assert notifier2.get_token(guid="guid00") is None + vmail_dir = tmp_path + vmail_dir.joinpath("user1@example.org").mkdir() + vmail_dir.joinpath("user3@example.org").mkdir() - notifier1.set_token("guid00", "01234") - notifier1.set_token("guid03", "456") - assert notifier2.get_token("guid00") == "01234" - assert notifier2.get_token("guid03") == "456" - notifier2.del_token("guid00") - assert notifier1.get_token("guid00") is None + notifier1 = Notifier(vmail_dir) + notifier2 = Notifier(vmail_dir) + assert notifier1.get_token("user1@example.org") is None + assert notifier2.get_token("user1@example.org") is None + + notifier1.set_token("user1@example.org", "01234") + notifier1.set_token("user3@example.org", "456") + assert notifier2.get_token("user1@example.org") == "01234" + assert notifier2.get_token("user3@example.org") == "456" + notifier2.del_token("user1@example.org") + assert notifier1.get_token("user1@example.org") is None def test_notifier_delete_without_set(notifier): - notifier.del_token("guid00") - assert not notifier.get_token("guid00") + notifier.del_token("user@example.org") + assert not notifier.get_token("user@example.org") def test_handle_dovecot_request_lookup_fails(notifier):