From f449acd8156c10fdba4f03b40f43431bb29c7192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Thu, 27 Jul 2017 20:39:21 +0200 Subject: [PATCH] Refactor MamDecryptedMessageReceivedEvent - Condition type = groupchat is not needed because we drop messages type groupchat that come from the user archive. To get these messages we will query the MUC. - Because of this the logging method save_if_not_exists() can be much simpler --- gajim/common/connection_handlers.py | 2 +- gajim/common/connection_handlers_events.py | 50 ++++++-------------- gajim/common/logger.py | 53 ++++++++-------------- gajim/common/message_archiving.py | 8 ++-- 4 files changed, 37 insertions(+), 76 deletions(-) diff --git a/gajim/common/connection_handlers.py b/gajim/common/connection_handlers.py index fe024b54b..6a1ba316b 100644 --- a/gajim/common/connection_handlers.py +++ b/gajim/common/connection_handlers.py @@ -1095,7 +1095,7 @@ class ConnectionHandlersBase: DecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) else: gajim.nec.push_incoming_event( - MamDecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) + MamDecryptedMessageReceivedEvent(None, **vars(obj))) def _nec_decrypted_message_received(self, obj): if obj.conn.name != self.name: diff --git a/gajim/common/connection_handlers_events.py b/gajim/common/connection_handlers_events.py index ec68749c5..2a421784c 100644 --- a/gajim/common/connection_handlers_events.py +++ b/gajim/common/connection_handlers_events.py @@ -1082,18 +1082,16 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): else: self.stanza_id = self.msg_.getID() - self.with_ = str(to) + self.with_ = to self.direction = 'to' - self.resource = to.getResource() else: if self.result.getNamespace() == nbxmpp.NS_MAM_2: self.stanza_id = self.result.getID() else: self.stanza_id = self.msg_.getID() - self.with_ = str(frm) + self.with_ = frm self.direction = 'from' - self.resource = frm.getResource() if not self.stanza_id: log.debug('Could not retrieve stanza-id') @@ -1118,38 +1116,18 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): base_network_events = [] def generate(self): - self.nick = None - msg_ = self.msg_obj.msg_ - if not hasattr(self, 'additional_data'): - self.additional_data = self.msg_obj.additional_data - self.with_ = self.msg_obj.with_ - self.direction = self.msg_obj.direction - self.timestamp = self.msg_obj.timestamp - res = self.msg_obj.resource - self.msgtxt = self.msg_obj.msgtxt - is_pm = gajim.logger.jid_is_room_jid(self.with_) - if msg_.getAttr('type') == 'groupchat': - if is_pm == False: - log.warn('JID %s is marked as normal contact in database ' - 'but we got a groupchat message from it.', self.with_) - return - if is_pm == None: - gajim.logger.get_jid_id(self.with_, 'ROOM') - self.nick = res - else: - if is_pm == None: - # we don't know this JID, we need to disco it. - server = gajim.get_server_from_jid(self.with_) - 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, - res]] - self.conn.discoverInfo(server) - else: - self.conn.mam_awaiting_disco_result[server].append( - [self.with_, self.direction, self.timestamp, self.msgtxt, - res]) - return + is_pm = gajim.logger.jid_is_room_jid(self.with_.getStripped()) + if is_pm is None: + # 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.discoverInfo(server) + else: + self.conn.mam_awaiting_disco_result[server].append( + [self.with_, self.direction, self.timestamp, self.msgtxt]) + return return True class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): diff --git a/gajim/common/logger.py b/gajim/common/logger.py index 9d07e65ca..a3f01d96e 100644 --- a/gajim/common/logger.py +++ b/gajim/common/logger.py @@ -1139,53 +1139,36 @@ class Logger: (account_jid_id,)) self._timeout_commit() - def save_if_not_exists(self, with_, direction, tim, msg='', nick=None, additional_data=None): + def save_if_not_exists(self, with_, direction, tim, msg='', is_pm=False, additional_data=None): if additional_data is None: additional_data = {} - if tim: - time_col = float(tim) - else: - time_col = float(time.time()) + if not msg: return - if self.jid_is_from_pm(with_) or nick: - # It's a groupchat message - if nick: - # It's a message from a groupchat occupent - type_ = 'gc_msg' - with_ = with_ + '/' + nick - else: - # It's a server message message, we don't log them - return + if is_pm: + type_ = 'gc_msg' else: if direction == 'from': type_ = 'chat_msg_recv' elif direction == 'to': type_ = 'chat_msg_sent' - jid_id = self.get_jid_id(with_) - where_sql = 'jid_id = %s AND message=?' % jid_id - if type_ == 'gc_msg': - # We cannot differentiate gc message and pm messages, so look in - # both logs - with_2 = gajim.get_jid_without_resource(with_) - if with_ != with_2: - jid_id2 = self.get_jid_id(with_2) - where_sql = 'jid_id in (%s, %s) AND message=?' % (jid_id, - jid_id2) - start_time = time_col - 300 # 5 minutes arrount given time - end_time = time_col + 300 # 5 minutes arrount given time - self.cur.execute(''' - SELECT log_line_id FROM logs - WHERE (%s) - AND time BETWEEN %d AND %d - ORDER BY time - ''' % (where_sql, start_time, end_time), (msg,)) - results = self.cur.fetchall() - if results: + + start_time = tim - 300 # 5 minutes arrount given time + end_time = tim + 300 # 5 minutes arrount given time + + sql = ''' + SELECT * FROM logs + NATURAL JOIN jids WHERE jid = ? AND message = ? + AND time BETWEEN ? AND ? + ''' + + result = self.con.execute(sql, (str(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, + self.write(type_, str(with_), message=msg, tim=tim, additional_data=additional_data, mam_query=True) def _nec_gc_message_received(self, obj): diff --git a/gajim/common/message_archiving.py b/gajim/common/message_archiving.py index a3cfaeef8..38bef8f7c 100644 --- a/gajim/common/message_archiving.py +++ b/gajim/common/message_archiving.py @@ -92,15 +92,15 @@ class ConnectionArchive313(ConnectionArchive): for identity in obj.identities: if identity['category'] == 'conference': # it's a groupchat - for with_, direction, tim, msg_txt, res in \ + for with_, direction, tim, msg_txt in \ self.mam_awaiting_disco_result[obj.jid]: gajim.logger.get_jid_id(with_, 'ROOM') gajim.logger.save_if_not_exists(with_, direction, tim, - msg=msg_txt, nick=res) + msg=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, res in \ + for with_, direction, tim, msg_txt in \ self.mam_awaiting_disco_result[obj.jid]: gajim.logger.get_jid_id(with_) gajim.logger.save_if_not_exists(with_, direction, tim, @@ -134,7 +134,7 @@ class ConnectionArchive313(ConnectionArchive): if obj.conn.name != self.name: return gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.timestamp, - msg=obj.msgtxt, nick=obj.nick, additional_data=obj.additional_data) + msg=obj.msgtxt, additional_data=obj.additional_data) def get_query_id(self): self.mam_query_id = self.connection.getAnID()