from typing import Dict, List, Optional, Any from libs.app.common.store import DataStore from libs.fspn.utils.sha256_util import hash_string class DataManager(): def __init__(self): self.store = DataStore(path="p2private/data.json", default_data={"friends":[], "messages":[]}) # ------------------- FRIEND CRUD ------------------- def add_friend(self, pubkey: str, relays): if self.get_friend(pubkey): raise ValueError(f"Friend {pubkey} already exists") friend = {"pubkey": pubkey, "relays": relays} self.store.add_item("friends", friend, unique=True, id_field="pubkey", id=pubkey) return friend def get_friend(self, pubkey: str): return self.store.get_item("friends", "pubkey", pubkey) def delete_friend(self, pubkey: str) -> bool: return self.store.remove_item("friends", "pubkey", pubkey) def list_friends(self): return self.store.list_items("friends") def update_friend(self, pubkey: str, updates: Dict[str, Any]) -> bool: return self.store.update_item("friends", "pubkey", pubkey, updates) # ------------------- MESSAGE CRUD ------------------- def add_message(self, message): if self.get_message(message['hash']): raise ValueError(f"Message {message['hash']} already exists") self.store.add_item("messages", message, unique=True, id_field="hash", id=message["hash"]) return message def get_message(self, hash: str): return self.store.get_item("messages", "hash", hash) def delete_message(self, hash: str) -> bool: return self.store.remove_item("messages", "hash", hash) def list_messages(self): return self.store.list_items("messages") def update_message(self, hash: str, updates: Dict[str, Any]) -> bool: return self.store.update_item("messages", "hash", hash, updates)