Skip to content

Commit

Permalink
Add message ID to DB entries
Browse files Browse the repository at this point in the history
This should prevent duplicate indexes.
  • Loading branch information
Electronic-Mango committed Nov 11, 2024
1 parent 9c1471b commit a3eb3c1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/db/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def _create_indexes() -> None:
keys=[("chat_id", ASCENDING), ("feed_name", ASCENDING), ("feed_type", ASCENDING)],
unique=True,
)
pinned_index = _pinned_collection.create_index(keys=[("chat_id", ASCENDING)], unique=True)
pinned_index = _pinned_collection.create_index(
keys=[("chat_id", ASCENDING), ("message_id", ASCENDING)], unique=True
)
logger.info(f"Created indexes [{feed_index}] [{pinned_index}]")


Expand Down
5 changes: 4 additions & 1 deletion src/main/db/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ def store_pinned_message(chat_id: int, message: Message) -> None:
logger.info(f"[{chat_id}] Pinning message [{message.id}]")
message_pickled = dumps(message)
message_b64 = b64encode(message_pickled).decode("ascii")
insert_one({"chat_id": chat_id, "message": message_b64}, collection=DB_PINNED_NAME)
insert_one(
{"chat_id": chat_id, "message_id": message.id, "message": message_b64},
collection=DB_PINNED_NAME,
)


def pop_pinned_messages(chat_id: int, bot: Bot) -> list[Message]:
Expand Down
2 changes: 1 addition & 1 deletion src/test/db/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_initialize_db(mongo_client_mock: MagicMock) -> None:
create_pinned_index = pinned_collection_mock.create_index
create_pinned_index.assert_called()
create_pinned_index_kwargs = create_pinned_index.call_args.kwargs
expected_pinned_keys = [("chat_id", ASCENDING)]
expected_pinned_keys = [("chat_id", ASCENDING), ("message_id", ASCENDING)]
assert expected_pinned_keys == create_pinned_index_kwargs.get("keys")
assert create_pinned_index_kwargs.get("unique")

Expand Down

0 comments on commit a3eb3c1

Please sign in to comment.