Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep message reference when updating cache messages #1192

Merged
merged 4 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/1192.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Properly garbage collect message references in the cache
- Properly deserialize `PartialMessage.referenced_message` as a partial message
7 changes: 6 additions & 1 deletion hikari/impl/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,12 @@ def _set_message(

referenced_message: typing.Optional[cache_utility.RefCell[cache_utility.MessageData]] = None
if message.referenced_message:
referenced_message = self._set_message(message.referenced_message)
reference_id = message.referenced_message.id
referenced_message = self._message_entries.get(reference_id) or self._referenced_messages.get(reference_id)

if referenced_message:
# Since the message is partial, if we don't have it cached, there is nothing we can do about it
referenced_message.object.update(message.referenced_message)

# Only increment ref counts if this wasn't previously cached.
if message.id not in self._referenced_messages and message.id not in self._message_entries:
Expand Down
4 changes: 2 additions & 2 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2514,9 +2514,9 @@ def deserialize_message(
if "message_reference" in payload:
message_reference = self._deserialize_message_reference(payload["message_reference"])

referenced_message: typing.Optional[message_models.Message] = None
referenced_message: typing.Optional[message_models.PartialMessage] = None
if referenced_message_payload := payload.get("referenced_message"):
referenced_message = self.deserialize_message(referenced_message_payload)
referenced_message = self.deserialize_partial_message(referenced_message_payload)

application: typing.Optional[message_models.MessageApplication] = None
if "application" in payload:
Expand Down
3 changes: 0 additions & 3 deletions hikari/internal/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,6 @@ def build_from_entity(
if not member and message.member:
member = RefCell(MemberData.build_from_entity(message.member))

if not referenced_message and message.referenced_message:
referenced_message = RefCell(MessageData.build_from_entity(message.referenced_message))

interaction = (
MessageInteractionData.build_from_entity(message.interaction, user=interaction_user)
if message.interaction
Expand Down
9 changes: 6 additions & 3 deletions hikari/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ class PartialMessage(snowflakes.Unique):
This is a string used for validating a message was sent.
"""

referenced_message: undefined.UndefinedNoneOr[Message] = attr.field(hash=False, eq=False, repr=False)
referenced_message: undefined.UndefinedNoneOr[PartialMessage] = attr.field(hash=False, eq=False, repr=False)
"""The message that was replied to.

If `type` is `MessageType.REPLY` and `hikari.undefined.UNDEFINED`, Discord's
Expand Down Expand Up @@ -1737,8 +1737,11 @@ class Message(PartialMessage):
nonce: typing.Optional[str] = attr.field(hash=False, eq=False, repr=False)
"""The message nonce. This is a string used for validating a message was sent."""

referenced_message: typing.Optional[Message] = attr.field(hash=False, eq=False, repr=False)
"""The message that was replied to."""
referenced_message: typing.Optional[PartialMessage] = attr.field(hash=False, eq=False, repr=False)
"""The message that was replied to.

If `type` is `MessageType.REPLY` and `builtins.None`, the message was deleted.
"""

interaction: typing.Optional[MessageInteraction] = attr.field(hash=False, eq=False, repr=False)
"""Information about the interaction this message was created by."""
Expand Down
2 changes: 1 addition & 1 deletion tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4818,7 +4818,7 @@ def test_deserialize_message(
assert message.message_reference.guild_id == 278325129692446720
assert isinstance(message.message_reference, message_models.MessageReference)

assert message.referenced_message == entity_factory_impl.deserialize_message(referenced_message)
assert message.referenced_message == entity_factory_impl.deserialize_partial_message(referenced_message)
assert message.flags == message_models.MessageFlag.IS_CROSSPOST

# Sticker
Expand Down