Use new DB methods for MAM Messages

- also push a new MamDecryptedMessageReceived
event after disco instead of writing directly to the DB
This commit is contained in:
Philipp Hörist 2017-08-01 18:42:01 +02:00
parent 16fd64873e
commit 1f5e927ca6
3 changed files with 45 additions and 61 deletions

View file

@ -40,7 +40,7 @@ from common import i18n
from common import dataforms from common import dataforms
from common import exceptions from common import exceptions
from common.zeroconf.zeroconf import Constant 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.pep import SUPPORTED_PERSONAL_USER_EVENTS
from common.jingle_transport import JingleTransportSocks5 from common.jingle_transport import JingleTransportSocks5
from common.file_props import FilesProp from common.file_props import FilesProp
@ -1059,7 +1059,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
self.stanza_id = self.msg_.getID() self.stanza_id = self.msg_.getID()
self.with_ = to self.with_ = to
self.direction = 'to' self.kind = KindConstant.CHAT_MSG_SENT
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()
@ -1067,7 +1067,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
self.stanza_id = self.msg_.getID() self.stanza_id = self.msg_.getID()
self.with_ = frm self.with_ = frm
self.direction = 'from' self.kind = KindConstant.CHAT_MSG_RECV
if not self.stanza_id: if not self.stanza_id:
log.debug('Could not retrieve stanza-id') log.debug('Could not retrieve stanza-id')
@ -1095,19 +1095,27 @@ class MamDecryptedMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
if not self.msgtxt: if not self.msgtxt:
# For example Chatstates, Receipts, Chatmarkers # For example Chatstates, Receipts, Chatmarkers
log.debug('Received MAM message without text') log.debug('Received MAM message without text')
return False return
self.is_pm = gajim.logger.jid_is_room_jid(self.with_.getStripped()) self.is_pm = gajim.logger.jid_is_room_jid(self.with_.getStripped())
if self.is_pm is None: 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. # we don't know this JID, we need to disco it.
server = self.with_.getDomain() 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]
[self.with_, self.direction, self.timestamp, self.msgtxt]]
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)
[self.with_, self.direction, self.timestamp, self.msgtxt])
return return
if self.is_pm:
self.with_ = str(self.with_)
else:
self.with_ = self.with_.getStripped()
return True return True
class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent): class MessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):

View file

@ -1139,41 +1139,6 @@ class Logger:
(account_jid_id,)) (account_jid_id,))
self._timeout_commit() 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): def search_for_duplicate(self, jid, timestamp, msg):
""" """
Check if a message is already in the `logs` table Check if a message is already in the `logs` table

View file

@ -25,6 +25,7 @@ import nbxmpp
from common import gajim from common import gajim
from common import ged from common import ged
from common.logger import KindConstant, JIDConstant
import common.connection_handlers_events as ev import common.connection_handlers_events as ev
log = logging.getLogger('gajim.c.message_archiving') log = logging.getLogger('gajim.c.message_archiving')
@ -77,22 +78,27 @@ class ConnectionArchive313:
del self.mam_awaiting_disco_result[obj.jid] del self.mam_awaiting_disco_result[obj.jid]
def _nec_agent_info(self, obj): def _nec_agent_info(self, obj):
if obj.jid in self.mam_awaiting_disco_result: if obj.jid not in self.mam_awaiting_disco_result:
return
for identity in obj.identities: for identity in obj.identities:
if identity['category'] == 'conference': if identity['category'] != 'conference':
continue
# it's a groupchat # it's a groupchat
for with_, direction, tim, msg_txt in \ for msg_obj in self.mam_awaiting_disco_result[obj.jid]:
self.mam_awaiting_disco_result[obj.jid]: gajim.logger.insert_jid(msg_obj.with_.getStripped(),
gajim.logger.get_jid_id(with_.getStripped(), 'ROOM') type_=JIDConstant.ROOM_TYPE)
gajim.logger.save_if_not_exists(with_, direction, tim, gajim.nec.push_incoming_event(
msg_txt, is_pm=True) ev.MamDecryptedMessageReceivedEvent(
None, disco=True, **vars(msg_obj)))
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 in \ for msg_obj in self.mam_awaiting_disco_result[obj.jid]:
self.mam_awaiting_disco_result[obj.jid]: gajim.logger.insert_jid(msg_obj.with_.getStripped())
gajim.logger.get_jid_id(with_.getStripped()) gajim.nec.push_incoming_event(
gajim.logger.save_if_not_exists(with_, direction, tim, msg_txt) ev.MamDecryptedMessageReceivedEvent(
None, disco=True, **vars(msg_obj)))
del self.mam_awaiting_disco_result[obj.jid] del self.mam_awaiting_disco_result[obj.jid]
def _nec_result_finished(self, obj): def _nec_result_finished(self, obj):
@ -121,8 +127,13 @@ class ConnectionArchive313:
def _nec_mam_decrypted_message_received(self, obj): def _nec_mam_decrypted_message_received(self, obj):
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, duplicate = gajim.logger.search_for_duplicate(
obj.msgtxt, is_pm=obj.is_pm, additional_data=obj.additional_data) 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): def get_query_id(self):
self.mam_query_id = self.connection.getAnID() self.mam_query_id = self.connection.getAnID()