auto-discovery toggle, auto-prune with customizable retention

This commit is contained in:
lichenblankie 2026-06-05 01:00:47 +00:00
parent 7ebf35b137
commit f8f9cb6337
4 changed files with 115 additions and 4 deletions

View file

@ -50,20 +50,35 @@ class ForumSync:
allow=RNS.Destination.ALLOW_ALL,
)
self.destination.announce(app_data=FORUM_APP.encode("utf-8"))
# Auto-discover other forum instances via announces
self._announce_handler = _ForumAnnounceHandler(self.fdb, self.identity)
RNS.Transport.register_announce_handler(self._announce_handler)
if self.fdb.get_setting("forum_auto_discover", "1") == "1":
self._enable_announce_handler()
self._running = True
self._thread = threading.Thread(target=self._sync_loop, daemon=True)
self._thread.start()
def stop(self):
self._running = False
self._disable_announce_handler()
def set_auto_discover(self, enabled):
self.fdb.set_setting("forum_auto_discover", "1" if enabled else "0")
if enabled:
self._enable_announce_handler()
else:
self._disable_announce_handler()
def _enable_announce_handler(self):
if self._announce_handler is None:
self._announce_handler = _ForumAnnounceHandler(self.fdb, self.identity)
RNS.Transport.register_announce_handler(self._announce_handler)
def _disable_announce_handler(self):
if self._announce_handler:
try:
RNS.Transport.deregister_announce_handler(self._announce_handler)
except Exception:
pass
self._announce_handler = None
def _rns_handler(self, path, data, request_id, link_id, remote_identity, requested_at):
if remote_identity:
@ -71,6 +86,7 @@ class ForumSync:
return self.handlers_ref().handle_sync(data)
def _sync_loop(self):
prune_counter = 0
while self._running:
try:
instances = self.fdb.get_synced_instances()
@ -83,6 +99,14 @@ class ForumSync:
print(f"[forum] sync error with {inst['instance_hash'][:16]}: {e}")
except Exception:
pass
prune_counter += 1
if prune_counter >= 6:
prune_counter = 0
try:
days = int(self.fdb.get_setting("forum_retention_days", "30"))
self.fdb.prune_old_content(days)
except Exception:
pass
for _ in range(SYNC_INTERVAL):
if not self._running:
return