fix README URLs, handler tests, sync improvements, block/retract gossip

This commit is contained in:
lichenblankie 2026-06-05 00:32:30 +00:00
parent a8aabb3427
commit a2d029097d
4 changed files with 440 additions and 36 deletions

View file

@ -39,6 +39,8 @@ class ForumSync:
self._running = False
def _rns_handler(self, path, data, request_id, link_id, remote_identity, requested_at):
if remote_identity:
data["peer_hash"] = remote_identity.hash.hex()
return self.handlers_ref().handle_sync(data)
def _sync_loop(self):
@ -108,11 +110,20 @@ class ForumSync:
posts = [dict(r) for r in ps]
upvotes = [{"thread_id": tid, "instance_hash": instance_hash} for tid in uv]
my_blocks = [h.strip() for h in self.fdb.get_setting("blocked_instances", "").split(",") if h.strip()]
my_peer_blocks = self.fdb.get_peer_block_list()
retracted = [{"id": cid, "type": ct, "author": ai, "at": ra}
for cid, ct, ai, ra in self.fdb.get_raw_retractions()]
request_data = {
"query": {"since": [since]} if since else {},
"threads": threads,
"posts": posts,
"upvotes": upvotes,
"from_hash": self.identity.hash.hex() if self.identity else "local",
"blocks": {"mine": my_blocks, "peers": my_peer_blocks},
"retractions": retracted,
}
receipt = link.request("/forum", data=request_data, timeout=REQUEST_TIMEOUT)
@ -129,12 +140,28 @@ class ForumSync:
data = json.loads(resp["body"])
except (json.JSONDecodeError, KeyError):
data = {}
my_blocks = set(h.strip() for h in self.fdb.get_setting("blocked_instances", "").split(",") if h.strip())
for t in data.get("threads", []):
self.fdb.merge_thread(t)
if t.get("author_instance", "") not in my_blocks:
self.fdb.merge_thread(t)
for p in data.get("posts", []):
self.fdb.merge_post(p)
if p.get("author_instance", "") not in my_blocks:
self.fdb.merge_post(p)
for tid in data.get("upvote_threads", []):
self.fdb.merge_upvote(tid, instance_hash)
# Gossip blocks from peer
peer_blocks = data.get("blocks", {})
for h in peer_blocks.get("mine", []):
if h and h not in my_blocks and instance_hash:
self.fdb.record_peer_block(instance_hash, h)
for h in peer_blocks.get("peers", []):
if h and h not in my_blocks and instance_hash:
self.fdb.record_peer_block(instance_hash, h)
self._apply_peer_blocks()
# Merge incoming retractions
for r in data.get("retractions", []):
if r.get("id") and r.get("type") and r.get("author") and r.get("at"):
self.fdb.merge_retraction(r["id"], r["type"], r["author"], r["at"])
now = time.strftime("%Y-%m-%dT%H:%M:%S")
self.fdb.set_last_sync(instance_hash, now)
else:
@ -142,5 +169,19 @@ class ForumSync:
finally:
link.teardown()
def _apply_peer_blocks(self):
counts = self.fdb.get_peer_block_counts()
blocked = set(h.strip() for h in self.fdb.get_setting("blocked_instances", "").split(",") if h.strip())
auto_blocked = set(h.strip() for h in self.fdb.get_setting("auto_blocked_instances", "").split(",") if h.strip())
changed = False
for h, count in counts.items():
if h not in blocked and h not in auto_blocked and count >= 3:
blocked.add(h)
auto_blocked.add(h)
changed = True
if changed:
self.fdb.set_setting("blocked_instances", ",".join(blocked))
self.fdb.set_setting("auto_blocked_instances", ",".join(auto_blocked))
def handle_sync(self, data):
return self.handlers_ref().handle_sync_request(data)