From c28941e58ff33d290f80d7836354231439b8fe66 Mon Sep 17 00:00:00 2001 From: Eli <9064062+snood1205@users.noreply.github.com> Date: Wed, 26 Mar 2025 11:39:41 -0400 Subject: [PATCH] Start handling replies --- at_client.py | 7 ++++--- error_handler.py | 10 +++++----- mention_listener.py | 35 ++++++++++++++++++++--------------- scheduler.py | 7 ++++--- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/at_client.py b/at_client.py index 920d963..79e882f 100644 --- a/at_client.py +++ b/at_client.py @@ -10,9 +10,10 @@ id_resolver = IdResolver() -def post_reply(post, parent_cid, parent_uri): - parent = models.com.atproto.repo.strong_ref.Main(cid=parent_cid, uri=parent_uri) - reply_to = ReplyRef(parent=parent, root=parent) +def post_reply(post, cid, parent_uri, root_uri): + parent = models.com.atproto.repo.strong_ref.Main(cid=cid, uri=parent_uri) + root = models.com.atproto.repo.strong_ref.Main(cid=cid, uri=root_uri) + reply_to = ReplyRef(parent=parent, root=root) client.send_post(post, reply_to=reply_to) diff --git a/error_handler.py b/error_handler.py index d4d6dfc..5a9eb43 100644 --- a/error_handler.py +++ b/error_handler.py @@ -3,7 +3,7 @@ from at_client import resolve_handle, build_mention_post, post_reply -def handle_no_run_at(did, parent_cid, parent_uri): +def handle_no_run_at(did, parent_cid, parent_uri, root_uri): warning(f"No run at was parsed for post at URI: {parent_uri}") handle = resolve_handle(did) post = build_mention_post( @@ -11,15 +11,15 @@ def handle_no_run_at(did, parent_cid, parent_uri): did, ", unfortunately I was unable to parse your time. Please try again in a different format (e.g. '50 minutes' or 'January 1, 2040')", ) - post_reply(post, parent_cid, parent_uri) + post_reply(post, str(parent_cid), parent_uri, root_uri) -def handle_run_at_in_past(did, parent_cid, parent_uri): - warning(f"Run at was parsed to be in the past for post at URI: {parent_uri}") +def handle_run_at_in_past(did, parent_cid, parent_uri, root_uri, run_at): + warning(f"Run at {run_at} was parsed to be in the past for post at URI: {parent_uri}") handle = resolve_handle(did) post = build_mention_post( handle, did, ", your reminder time appears to be in the past. I can only handle reminders for the future.", ) - post_reply(post, parent_cid, parent_uri) + post_reply(post, str(parent_cid), parent_uri, root_uri) diff --git a/mention_listener.py b/mention_listener.py index facc538..3d6561c 100644 --- a/mention_listener.py +++ b/mention_listener.py @@ -29,17 +29,21 @@ def parse_create_op(commit): if not blocks: continue record = models.get_or_create(blocks, strict=False) - if not record.facets: - continue - for facet in record.facets: - for feature in facet.features: - if isinstance(feature, Mention) and feature.did == account_did: - return record.text, uri, op.cid - - -def enqueue_reminder(did, run_at, post_cid, post_uri): + if record.facets: + for facet in record.facets: + for feature in facet.features: + if isinstance(feature, Mention) and feature.did == account_did: + return record.text, uri, op.cid, record.reply + reply = getattr(record, "reply", None) + if reply: + parent_did = AtUri.from_str(reply.parent.uri).hostname + if parent_did == account_did: + return record.text, uri, str(op.cid), record.reply + + +def enqueue_reminder(did, run_at, cid, parent_uri, root_uri): handle = resolve_handle(did) - task = {"did": did, "handle": handle, "post_cid": post_cid, "post_uri": post_uri} + task = {"did": did, "handle": handle, "cid": cid, "parent_uri": parent_uri, "root_uri": root_uri} redis.zadd("task_queue", {dumps(task): run_at.timestamp()}) @@ -60,15 +64,16 @@ def handle_firehose_event(message_frame): result = parse_create_op(commit) if not result: return - message, uri, cid = result - post_uri = f"at://{commit.repo}/app.bsky.feed.post/{uri.rkey}" + message, uri, cid, reply = result + parent_uri = reply.parent.uri if reply else f"at://{commit.repo}/app.bsky.feed.post/{uri.rkey}" + root_uri = reply.root.uri if reply else parent_uri run_at = parse_run_at(message) if not run_at: - return handle_no_run_at(commit.repo, cid, post_uri) + return handle_no_run_at(commit.repo, cid, parent_uri, root_uri) if run_at <= datetime.now(): - return handle_run_at_in_past(commit.repo, cid, post_uri) + return handle_run_at_in_past(commit.repo, cid, parent_uri, root_uri, run_at) - enqueue_reminder(commit.repo, run_at, str(cid), post_uri) + enqueue_reminder(commit.repo, run_at, str(cid), parent_uri, root_uri) def listen_for_mentions(stop_event): diff --git a/scheduler.py b/scheduler.py index 3a07f22..6048059 100644 --- a/scheduler.py +++ b/scheduler.py @@ -8,11 +8,12 @@ def run_task(task): handle = task["handle"] did = task["did"] - parent_cid = task["post_cid"] - parent_uri = task["post_uri"] + cid = task["cid"] + parent_uri = task["parent_uri"] + root_uri = task["root_uri"] post = build_mention_post(handle, did, ", your reminder is ready!") - post_reply(post, parent_cid, parent_uri) + post_reply(post, cid, parent_uri, root_uri) def query_for_and_post_reminders(stop_event):