Refactor get_room_last_message_time()

- Refactor get_last_date_that_has_logs()

- use NATURAL JOIN in SQL query instead of multiple SELECT via
_build_contact_where

- make code more concise

- update method documentation
This commit is contained in:
Philipp Hörist 2017-07-16 17:18:27 +02:00
parent 1e380473cd
commit 9feee7d9dc
3 changed files with 45 additions and 55 deletions

View File

@ -2562,19 +2562,16 @@ class Connection(CommonConnection, ConnectionHandlers):
# last date/time in history to avoid duplicate # last date/time in history to avoid duplicate
if room_jid not in self.last_history_time: if room_jid not in self.last_history_time:
# Not in memory, get it from DB # Not in memory, get it from DB
last_log = None last_log = 0
# Do not check if we are not logging for this room
if gajim.config.should_log(self.name, room_jid): if gajim.config.should_log(self.name, room_jid):
# Check time first in the FAST table # Check time first in the FAST table
last_log = gajim.logger.get_room_last_message_time(room_jid) last_log = gajim.logger.get_room_last_message_time(
if last_log is None: self.name, room_jid)
# Not in special table, get it from messages DB if not last_log:
last_log = gajim.logger.get_last_date_that_has_logs(room_jid, last_log = 0
is_room=True)
# Create self.last_history_time[room_jid] even if not logging, # Create self.last_history_time[room_jid] even if not logging,
# could be used in connection_handlers # could be used in connection_handlers
if last_log is None:
last_log = 0
self.last_history_time[room_jid] = last_log self.last_history_time[room_jid] = last_log
p = nbxmpp.Presence(to='%s/%s' % (room_jid, nick), p = nbxmpp.Presence(to='%s/%s' % (room_jid, nick),

View File

@ -807,58 +807,51 @@ class Logger:
date.timestamp(), date.timestamp(),
(date + delta).timestamp())).fetchall() (date + delta).timestamp())).fetchall()
def get_last_date_that_has_logs(self, jid, account=None, is_room=False): def get_last_date_that_has_logs(self, account, jid):
""" """
Return last time (in seconds since EPOCH) for which we had logs Get the timestamp of the last message we received for the jid.
(excluding statuses)
:param account: The account
:param jid: The jid for which we request the last timestamp
returns a timestamp or None
""" """
where_sql = '' jids = self._get_family_jids(account, jid)
if not is_room:
where_sql, jid_tuple = self._build_contact_where(account, jid) kinds = map(str, [KindConstant.STATUS,
else: KindConstant.GCSTATUS])
try:
jid_id = self.get_jid_id(jid, 'ROOM') sql = '''
except exceptions.PysqliteOperationalError:
# Error trying to create a new jid_id. This means there is no log
return None
where_sql = 'jid_id = ?'
jid_tuple = (jid_id,)
self.cur.execute('''
SELECT MAX(time) as time FROM logs SELECT MAX(time) as time FROM logs
WHERE (%s) NATURAL JOIN jids WHERE jid IN ({jids})
AND kind NOT IN (%d, %d) AND kind NOT IN ({kinds})
''' % (where_sql, KindConstant.STATUS, KindConstant.GCSTATUS), '''.format(jids=', '.join('?' * len(jids)),
jid_tuple) kinds=', '.join(kinds))
results = self.cur.fetchone() # fetchone() returns always at least one Row with all
if results is not None: # attributes set to None because of the MAX() function
result = results.time return self.con.execute(sql, (*jids,)).fetchone().time
else:
result = None
return result
def get_room_last_message_time(self, jid): def get_room_last_message_time(self, account, jid):
""" """
Return FASTLY last time (in seconds since EPOCH) for which we had logs Get the timestamp of the last message we received in a room.
for that room from rooms_last_message_time table
"""
try:
jid_id = self.get_jid_id(jid, 'ROOM')
except exceptions.PysqliteOperationalError:
# Error trying to create a new jid_id. This means there is no log
return None
where_sql = 'jid_id = %s' % jid_id
self.cur.execute('''
SELECT time FROM rooms_last_message_time
WHERE (%s)
''' % (where_sql))
results = self.cur.fetchone() :param account: The account
if results is not None:
result = results.time :param jid: The jid for which we request the last timestamp
else:
result = None returns a timestamp or None
return result """
sql = '''
SELECT time FROM rooms_last_message_time
NATURAL JOIN jids WHERE jid = ?
'''
row = self.con.execute(sql, (jid,)).fetchone()
if not row:
return self.get_last_date_that_has_logs(account, jid)
return row.time
def set_room_last_message_time(self, jid, time): def set_room_last_message_time(self, jid, time):
""" """

View File

@ -305,7 +305,7 @@ class HistoryWindow:
# select logs for last date we have logs with contact # select logs for last date we have logs with contact
self.calendar.set_sensitive(True) self.calendar.set_sensitive(True)
last_log = \ last_log = \
gajim.logger.get_last_date_that_has_logs(self.jid, self.account) gajim.logger.get_last_date_that_has_logs(self.account, self.jid)
date = time.localtime(last_log) date = time.localtime(last_log)