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
This commit is contained in:
Philipp Hörist 2017-07-27 20:39:21 +02:00
parent cb90f9decd
commit f449acd815
4 changed files with 37 additions and 76 deletions

View file

@ -1095,7 +1095,7 @@ class ConnectionHandlersBase:
DecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) DecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj))
else: else:
gajim.nec.push_incoming_event( gajim.nec.push_incoming_event(
MamDecryptedMessageReceivedEvent(None, conn=self, msg_obj=obj)) MamDecryptedMessageReceivedEvent(None, **vars(obj)))
def _nec_decrypted_message_received(self, obj): def _nec_decrypted_message_received(self, obj):
if obj.conn.name != self.name: if obj.conn.name != self.name:

View file

@ -1082,18 +1082,16 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
else: else:
self.stanza_id = self.msg_.getID() self.stanza_id = self.msg_.getID()
self.with_ = str(to) self.with_ = to
self.direction = 'to' self.direction = 'to'
self.resource = to.getResource()
else: else:
if self.result.getNamespace() == nbxmpp.NS_MAM_2: if self.result.getNamespace() == nbxmpp.NS_MAM_2:
self.stanza_id = self.result.getID() self.stanza_id = self.result.getID()
else: else:
self.stanza_id = self.msg_.getID() self.stanza_id = self.msg_.getID()
self.with_ = str(frm) self.with_ = frm
self.direction = 'from' self.direction = 'from'
self.resource = frm.getResource()
if not self.stanza_id: if not self.stanza_id:
log.debug('Could not retrieve stanza-id') log.debug('Could not retrieve stanza-id')
@ -1118,37 +1116,17 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
base_network_events = [] base_network_events = []
def generate(self): def generate(self):
self.nick = None is_pm = gajim.logger.jid_is_room_jid(self.with_.getStripped())
msg_ = self.msg_obj.msg_ if is_pm is None:
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. # we don't know this JID, we need to disco it.
server = gajim.get_server_from_jid(self.with_) server = self.with_.getDomain()
if server not in self.conn.mam_awaiting_disco_result: if server not in self.conn.mam_awaiting_disco_result:
self.conn.mam_awaiting_disco_result[server] = [ self.conn.mam_awaiting_disco_result[server] = [
[self.with_, self.direction, self.timestamp, self.msgtxt, [self.with_, self.direction, self.timestamp, self.msgtxt]]
res]]
self.conn.discoverInfo(server) self.conn.discoverInfo(server)
else: else:
self.conn.mam_awaiting_disco_result[server].append( self.conn.mam_awaiting_disco_result[server].append(
[self.with_, self.direction, self.timestamp, self.msgtxt, [self.with_, self.direction, self.timestamp, self.msgtxt])
res])
return return
return True return True

View file

@ -1139,53 +1139,36 @@ class Logger:
(account_jid_id,)) (account_jid_id,))
self._timeout_commit() 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: if additional_data is None:
additional_data = {} additional_data = {}
if tim:
time_col = float(tim)
else:
time_col = float(time.time())
if not msg: if not msg:
return return
if self.jid_is_from_pm(with_) or nick: if is_pm:
# It's a groupchat message
if nick:
# It's a message from a groupchat occupent
type_ = 'gc_msg' type_ = 'gc_msg'
with_ = with_ + '/' + nick
else:
# It's a server message message, we don't log them
return
else: else:
if direction == 'from': if direction == 'from':
type_ = 'chat_msg_recv' type_ = 'chat_msg_recv'
elif direction == 'to': elif direction == 'to':
type_ = 'chat_msg_sent' type_ = 'chat_msg_sent'
jid_id = self.get_jid_id(with_)
where_sql = 'jid_id = %s AND message=?' % jid_id start_time = tim - 300 # 5 minutes arrount given time
if type_ == 'gc_msg': end_time = tim + 300 # 5 minutes arrount given time
# We cannot differentiate gc message and pm messages, so look in
# both logs sql = '''
with_2 = gajim.get_jid_without_resource(with_) SELECT * FROM logs
if with_ != with_2: NATURAL JOIN jids WHERE jid = ? AND message = ?
jid_id2 = self.get_jid_id(with_2) AND time BETWEEN ? AND ?
where_sql = 'jid_id in (%s, %s) AND message=?' % (jid_id, '''
jid_id2)
start_time = time_col - 300 # 5 minutes arrount given time result = self.con.execute(sql, (str(with_), msg, start_time, end_time)).fetchone()
end_time = time_col + 300 # 5 minutes arrount given time
self.cur.execute(''' if result:
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:
log.debug('Log already in DB, ignoring it') log.debug('Log already in DB, ignoring it')
return return
log.debug('New log received from server archives, storing it') 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) additional_data=additional_data, mam_query=True)
def _nec_gc_message_received(self, obj): def _nec_gc_message_received(self, obj):

View file

@ -92,15 +92,15 @@ class ConnectionArchive313(ConnectionArchive):
for identity in obj.identities: for identity in obj.identities:
if identity['category'] == 'conference': if identity['category'] == 'conference':
# it's a groupchat # 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]: self.mam_awaiting_disco_result[obj.jid]:
gajim.logger.get_jid_id(with_, 'ROOM') gajim.logger.get_jid_id(with_, 'ROOM')
gajim.logger.save_if_not_exists(with_, direction, tim, 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] del self.mam_awaiting_disco_result[obj.jid]
return return
# it's not a groupchat # 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]: self.mam_awaiting_disco_result[obj.jid]:
gajim.logger.get_jid_id(with_) gajim.logger.get_jid_id(with_)
gajim.logger.save_if_not_exists(with_, direction, tim, gajim.logger.save_if_not_exists(with_, direction, tim,
@ -134,7 +134,7 @@ class ConnectionArchive313(ConnectionArchive):
if obj.conn.name != self.name: if obj.conn.name != self.name:
return return
gajim.logger.save_if_not_exists(obj.with_, obj.direction, obj.timestamp, 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): def get_query_id(self):
self.mam_query_id = self.connection.getAnID() self.mam_query_id = self.connection.getAnID()