diff --git a/src/common/connection_handlers.py b/src/common/connection_handlers.py index 9fab409e0aced40b8280bd52edfdfa60a07b3d74..da336aa8f285cd64b9e052529b216ec2121aa2ed 100644 --- a/src/common/connection_handlers.py +++ b/src/common/connection_handlers.py @@ -1367,6 +1367,17 @@ class ConnectionHandlersBase: else: return None + def get_latest_session(self, jid): + """ + Get the session that we last sent a message to + """ + if jid not in self.sessions: + return None + sessions = self.sessions[jid].values() + if not sessions: + return None + return sorted(sessions, key=operator.attrgetter('last_send'))[-1] + def find_controlless_session(self, jid, resource=None): """ Find an active session that doesn't have a control attached diff --git a/src/common/connection_handlers_events.py b/src/common/connection_handlers_events.py index 94f3eabdd57f32c0c69a5593b423f2138f328558..8a9202e5c55a4964c460268f265759007e7b7abb 100644 --- a/src/common/connection_handlers_events.py +++ b/src/common/connection_handlers_events.py @@ -1109,8 +1109,17 @@ class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): self.session = None if self.mtype != 'groupchat': - self.session = self.conn.get_or_create_session(self.fjid, - self.thread_id) + if gajim.interface.is_pm_contact(self.fjid, account) and \ + self.mtype == 'error': + self.session = self.conn.find_session(self.fjid, self.thread_id) + if not self.session: + self.session = self.conn.get_latest_session(self.fjid) + if not self.session: + self.session = self.conn.make_new_session(self.fjid, + self.thread_id, type_='pm') + else: + self.session = self.conn.get_or_create_session(self.fjid, + self.thread_id) if self.thread_id and not self.session.received_thread_id: self.session.received_thread_id = True