plugin XEP-0444, storage: move get_history_from_xmpp_id to storage as it is generally useful.

This commit is contained in:
Goffi 2025-05-06 00:33:57 +02:00
commit 931052f40c
2 changed files with 46 additions and 29 deletions

View file

@ -494,6 +494,49 @@ class Storage:
else:
return History.source == jid_.userhost()
@aio
async def get_history_from_xmpp_id(
self, client: SatXMPPEntity, message_id: str, message_type: str|None
) -> History | None:
"""Retrieves history row that match a specific message ID.
The retrieval criteria vary based on the message type.
@param message_type: The type of the message (one of C.MESS_TYPE_*)
@param message_id: the ID of the message, XEP-0359's stanza-id is used for
groupchat, origin-id from the same XEP otherwise.
@return: History instance with messages, subjects and threads, if any found, None
otherwise.
"""
profile_id = self.profiles[client.profile]
async with self.session() as session:
if message_type == C.MESS_TYPE_GROUPCHAT:
query = select(History).where(
History.profile_id == profile_id, History.stanza_id == message_id
)
else:
query = select(History).where(
History.profile_id == profile_id, History.origin_id == message_id
)
query = (
query
.outerjoin(History.messages)
.outerjoin(History.subjects)
.outerjoin(History.thread)
.options(
contains_eager(History.messages),
contains_eager(History.subjects),
contains_eager(History.thread),
)
)
result = await session.execute(query)
history = result.scalars().first()
return history
@aio
async def history_get(
self,

View file

@ -75,33 +75,6 @@ class XEP_0444:
def get_handler(self, client):
return XEP_0444_Handler()
@aio
async def get_history_from_reaction_id(
self, client: SatXMPPEntity, message_elt: domish.Element, reaction_id: str
) -> History | None:
"""Retrieves history rows that match a specific reaction_id.
The retrieval criteria vary based on the message type, according to XEP-0444.
@param message_elt: The message element, used to determine the type of message.
@param reaction_id: The reaction ID to match in the history.
"""
profile_id = self.host.memory.storage.profiles[client.profile]
async with self.host.memory.storage.session() as session:
if message_elt.getAttribute("type") == C.MESS_TYPE_GROUPCHAT:
query = select(History).where(
History.profile_id == profile_id, History.stanza_id == reaction_id
)
else:
query = select(History).where(
History.profile_id == profile_id, History.origin_id == reaction_id
)
result = await session.execute(query)
history = result.scalars().first()
return history
async def _message_received_trigger(
self,
client: SatXMPPEntity,
@ -111,8 +84,9 @@ class XEP_0444:
reactions_elt = next(message_elt.elements(NS_REACTIONS, "reactions"), None)
if reactions_elt is not None:
reaction_id = reactions_elt.getAttribute("id")
history = await self.get_history_from_reaction_id(
client, message_elt, reaction_id
message_type = message_elt.getAttribute("type")
history = await self.host.memory.storage.get_history_from_xmpp_id(
client, reaction_id, message_type
)
if history is None:
log.warning(