From 1f5e927ca61b5cdc8fa8970546277a76ea7a8dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 1 Aug 2017 18:42:01 +0200 Subject: [PATCH] Use new DB methods for MAM Messages - also push a new MamDecryptedMessageReceived event after disco instead of writing directly to the DB --- gajim/common/connection_handlers_events.py | 24 +++++++---- gajim/common/logger.py | 35 ---------------- gajim/common/message_archiving.py | 47 +++++++++++++--------- 3 files changed, 45 insertions(+), 61 deletions(-) diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index 928e79b8a..6067505df 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -40,7 +40,7 @@ from common import i18n from common import dataforms from common import exceptions from common.zeroconf.zeroconf import Constant -from common.logger import LOG_DB_PATH +from common.logger import LOG_DB_PATH, KindConstant from common.pep import SUPPORTED_PERSONAL_USER_EVENTS from common.jingle_transport import JingleTransportSocks5 from common.file_props import FilesProp @@ -1059,7 +1059,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): self.stanza_id = self.msg_.getID() self.with_ = to - self.direction = 'to' + self.kind = KindConstant.CHAT_MSG_SENT else: if self.result.getNamespace() == nbxmpp.NS_MAM_2: self.stanza_id = self.result.getID() @@ -1067,7 +1067,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): self.stanza_id = self.msg_.getID() self.with_ = frm - self.direction = 'from' + self.kind = KindConstant.CHAT_MSG_RECV if not self.stanza_id: log.debug('Could not retrieve stanza-id') @@ -1095,19 +1095,27 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): if not self.msgtxt: # For example Chatstates, Receipts, Chatmarkers log.debug('Received MAM message without text') - return False + return self.is_pm = gajim.logger.jid_is_room_jid(self.with_.getStripped()) if self.is_pm is None: + # Check if this event is triggered after a disco, so we dont + # run into an endless loop + if hasattr(self, 'disco'): + log.error('JID not known even after sucessful disco') + return # we don't know this JID, we need to disco it. server = self.with_.getDomain() if server not in self.conn.mam_awaiting_disco_result: - self.conn.mam_awaiting_disco_result[server] = [ - [self.with_, self.direction, self.timestamp, self.msgtxt]] + self.conn.mam_awaiting_disco_result[server] = [self] self.conn.discoverInfo(server) else: - self.conn.mam_awaiting_disco_result[server].append( - [self.with_, self.direction, self.timestamp, self.msgtxt]) + self.conn.mam_awaiting_disco_result[server].append(self) return + + if self.is_pm: + self.with_ = str(self.with_) + else: + self.with_ = self.with_.getStripped() return True class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): diff --git a/gajim/common/logger.py b/gajim/common/logger.py index e2de43c95..7a01a0294 100644 --- a/gajim/common/logger.py +++ b/gajim/common/logger.py @@ -1139,41 +1139,6 @@ class Logger: (account_jid_id,)) self._timeout_commit() - def save_if_not_exists(self, with_, direction, tim, msg, is_pm=False, additional_data=None): - if additional_data is None: - additional_data = {} - - if is_pm: - with_ = str(with_) - type_ = 'gc_msg' - else: - with_ = with_.getStripped() - if direction == 'from': - type_ = 'chat_msg_recv' - elif direction == 'to': - type_ = 'chat_msg_sent' - - start_time = tim - 300 # 5 minutes arrount given time - end_time = tim + 300 # 5 minutes arrount given time - - log.debug('start: %s, end: %s, jid: %s, message: %s', - start_time, end_time, with_, msg) - - sql = ''' - SELECT * FROM logs - NATURAL JOIN jids WHERE jid = ? AND message = ? - AND time BETWEEN ? AND ? - ''' - - result = self.con.execute(sql, (with_, msg, start_time, end_time)).fetchone() - - if result: - log.debug('Log already in DB, ignoring it') - return - log.debug('New log received from server archives, storing it') - self.write(type_, with_, message=msg, tim=tim, - additional_data=additional_data, mam_query=True) - def search_for_duplicate(self, jid, timestamp, msg): """ Check if a message is already in the `logs` table diff --git a/gajim/common/message_archiving.py b/gajim/common/message_archiving.py index 4795c8b6b..f102bbee6 100644 --- a/gajim/common/message_archiving.py +++ b/gajim/common/message_archiving.py @@ -25,6 +25,7 @@ import nbxmpp from common import gajim from common import ged +from common.logger import KindConstant, JIDConstant import common.connection_handlers_events as ev log = logging.getLogger('gajim.c.message_archiving') @@ -77,23 +78,28 @@ class ConnectionArchive313: del self.mam_awaiting_disco_result[obj.jid] def _nec_agent_info(self, obj): - if obj.jid in self.mam_awaiting_disco_result: - for identity in obj.identities: - if identity['category'] == 'conference': - # it's a groupchat - for with_, direction, tim, msg_txt in \ - self.mam_awaiting_disco_result[obj.jid]: - gajim.logger.get_jid_id(with_.getStripped(), 'ROOM') - gajim.logger.save_if_not_exists(with_, direction, tim, - msg_txt, is_pm=True) - del self.mam_awaiting_disco_result[obj.jid] - return - # it's not a groupchat - for with_, direction, tim, msg_txt in \ - self.mam_awaiting_disco_result[obj.jid]: - gajim.logger.get_jid_id(with_.getStripped()) - gajim.logger.save_if_not_exists(with_, direction, tim, msg_txt) + if obj.jid not in self.mam_awaiting_disco_result: + return + + for identity in obj.identities: + if identity['category'] != 'conference': + continue + # it's a groupchat + for msg_obj in self.mam_awaiting_disco_result[obj.jid]: + gajim.logger.insert_jid(msg_obj.with_.getStripped(), + type_=JIDConstant.ROOM_TYPE) + gajim.nec.push_incoming_event( + ev.MamDecryptedMessageReceivedEvent( + None, disco=True, **vars(msg_obj))) del self.mam_awaiting_disco_result[obj.jid] + return + # it's not a groupchat + for msg_obj in self.mam_awaiting_disco_result[obj.jid]: + gajim.logger.insert_jid(msg_obj.with_.getStripped()) + gajim.nec.push_incoming_event( + ev.MamDecryptedMessageReceivedEvent( + None, disco=True, **vars(msg_obj))) + del self.mam_awaiting_disco_result[obj.jid] def _nec_result_finished(self, obj): if obj.conn.name != self.name: @@ -121,8 +127,13 @@ class ConnectionArchive313: def _nec_mam_decrypted_message_received(self, obj): if obj.conn.name != self.name: return - gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.timestamp, - obj.msgtxt, is_pm=obj.is_pm, additional_data=obj.additional_data) + duplicate = gajim.logger.search_for_duplicate( + obj.with_, obj.timestamp, obj.msgtxt) + if not duplicate: + gajim.logger.insert_into_logs( + obj.with_, obj.timestamp, obj.kind, + unread=False, + message=obj.msgtxt) def get_query_id(self): self.mam_query_id = self.connection.getAnID()