From 1db98ae161972c353242f049130eaf86feb368b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Mon, 11 May 2026 23:17:53 +0200 Subject: [PATCH] Maybe fix webhook being edited every time? --- discord_rss_bot/feeds.py | 14 ++++++- tests/test_feeds.py | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/discord_rss_bot/feeds.py b/discord_rss_bot/feeds.py index 44d20a2..d35d7d6 100644 --- a/discord_rss_bot/feeds.py +++ b/discord_rss_bot/feeds.py @@ -717,6 +717,7 @@ def update_sent_webhook_record_for_entry( ): return record, False, False + previous_payload: JsonObject = json_object_or_empty(record.get("payload")) webhook, delivery_mode = create_webhook_for_entry( webhook_url_value, entry, @@ -725,12 +726,23 @@ def update_sent_webhook_record_for_entry( ) payload: JsonObject = preserve_previous_embed_media( get_webhook_message_payload(webhook), - json_object_or_empty(record.get("payload")), + previous_payload, ) edit_payload: JsonObject = get_webhook_message_edit_payload(payload, record) payload_hash: str = hash_webhook_payload(payload) if payload_hash == record.get("payload_hash"): return record, False, False + if previous_payload and payload_hash == hash_webhook_payload(previous_payload): + return ( + { + **record, + "payload": payload, + "payload_hash": payload_hash, + "delivery_mode": delivery_mode, + }, + True, + False, + ) now: str = datetime.datetime.now(tz=datetime.UTC).isoformat() try: diff --git a/tests/test_feeds.py b/tests/test_feeds.py index 06863f0..2205cbc 100644 --- a/tests/test_feeds.py +++ b/tests/test_feeds.py @@ -1129,6 +1129,97 @@ def test_update_sent_webhook_record_preserves_existing_embed_image_when_updated_ assert updated_record["payload_hash"] == feeds.hash_webhook_payload(updated_payload) +@patch("discord_rss_bot.feeds.edit_sent_webhook_message") +@patch("discord_rss_bot.feeds.create_webhook_for_entry") +def test_update_sent_webhook_record_skips_edit_when_preserved_image_keeps_payload_unchanged( + mock_create_webhook_for_entry: MagicMock, + mock_edit_sent_webhook_message: MagicMock, +) -> None: + previous_image: JsonObject = { + "url": "https://example.com/original-image.jpg", + "proxy_url": None, + "height": None, + "width": None, + } + old_payload: JsonObject = { + "content": "", + "embeds": [{"description": "Same summary", "image": previous_image, "thumbnail": None}], + "attachments": [], + } + record: feeds.SentWebhookRecord = { + "feed_url": "https://example.com/feed.xml", + "entry_id": "entry-5", + "webhook_url": "https://discord.com/api/webhooks/123/abc", + "message_id": "message-5", + "payload": old_payload, + "payload_hash": feeds.hash_webhook_payload(old_payload), + "update_count": 0, + } + + entry = MagicMock() + entry.id = "entry-5" + + reader = MagicMock() + webhook = MagicMock() + webhook.json = { + "content": "", + "embeds": [{"description": "Same summary", "image": None, "thumbnail": None}], + "attachments": [], + } + mock_create_webhook_for_entry.return_value = (webhook, "embed") + + updated_record, record_changed, message_was_edited = feeds.update_sent_webhook_record_for_entry( + reader, + entry, + record, + ) + + assert updated_record == record + assert record_changed is False + assert message_was_edited is False + mock_edit_sent_webhook_message.assert_not_called() + + +@patch("discord_rss_bot.feeds.edit_sent_webhook_message") +@patch("discord_rss_bot.feeds.create_webhook_for_entry") +def test_update_sent_webhook_record_backfills_missing_payload_hash_without_editing_discord( + mock_create_webhook_for_entry: MagicMock, + mock_edit_sent_webhook_message: MagicMock, +) -> None: + old_payload: JsonObject = { + "content": "", + "embeds": [{"description": "Same summary", "image": None, "thumbnail": None}], + "attachments": [], + } + record: feeds.SentWebhookRecord = { + "feed_url": "https://example.com/feed.xml", + "entry_id": "entry-6", + "webhook_url": "https://discord.com/api/webhooks/123/abc", + "message_id": "message-6", + "payload": old_payload, + "update_count": 0, + } + + entry = MagicMock() + entry.id = "entry-6" + + reader = MagicMock() + webhook = MagicMock() + webhook.json = old_payload + mock_create_webhook_for_entry.return_value = (webhook, "embed") + + updated_record, record_changed, message_was_edited = feeds.update_sent_webhook_record_for_entry( + reader, + entry, + record, + ) + + assert record_changed is True + assert message_was_edited is False + assert updated_record["payload_hash"] == feeds.hash_webhook_payload(old_payload) + mock_edit_sent_webhook_message.assert_not_called() + + def test_update_feeds_and_collect_modified_entries_only_returns_modified_entries() -> None: class StubReader: def __init__(self) -> None: