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
if room_jid not in self.last_history_time:
# Not in memory, get it from DB
last_log = None
# Do not check if we are not logging for this room
last_log = 0
if gajim.config.should_log(self.name, room_jid):
# Check time first in the FAST table
last_log = gajim.logger.get_room_last_message_time(room_jid)
if last_log is None:
# Not in special table, get it from messages DB
last_log = gajim.logger.get_last_date_that_has_logs(room_jid,
is_room=True)
last_log = gajim.logger.get_room_last_message_time(
self.name, room_jid)
if not last_log:
last_log = 0
# Create self.last_history_time[room_jid] even if not logging,
# could be used in connection_handlers
if last_log is None:
last_log = 0
self.last_history_time[room_jid] = last_log
p = nbxmpp.Presence(to='%s/%s' % (room_jid, nick),

View File

@ -807,58 +807,51 @@ class Logger:
date.timestamp(),
(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
(excluding statuses)
Get the timestamp of the last message we received for the jid.
:param account: The account
:param jid: The jid for which we request the last timestamp
returns a timestamp or None
"""
where_sql = ''
if not is_room:
where_sql, jid_tuple = self._build_contact_where(account, jid)
else:
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 = ?'
jid_tuple = (jid_id,)
self.cur.execute('''
jids = self._get_family_jids(account, jid)
kinds = map(str, [KindConstant.STATUS,
KindConstant.GCSTATUS])
sql = '''
SELECT MAX(time) as time FROM logs
WHERE (%s)
AND kind NOT IN (%d, %d)
''' % (where_sql, KindConstant.STATUS, KindConstant.GCSTATUS),
jid_tuple)
NATURAL JOIN jids WHERE jid IN ({jids})
AND kind NOT IN ({kinds})
'''.format(jids=', '.join('?' * len(jids)),
kinds=', '.join(kinds))
results = self.cur.fetchone()
if results is not None:
result = results.time
else:
result = None
return result
# fetchone() returns always at least one Row with all
# attributes set to None because of the MAX() function
return self.con.execute(sql, (*jids,)).fetchone().time
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
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))
Get the timestamp of the last message we received in a room.
results = self.cur.fetchone()
if results is not None:
result = results.time
else:
result = None
return result
:param account: The account
:param jid: The jid for which we request the last timestamp
returns a timestamp or None
"""
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):
"""

View File

@ -305,7 +305,7 @@ class HistoryWindow:
# select logs for last date we have logs with contact
self.calendar.set_sensitive(True)
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)