Add new logger methods

Goal is to make the logger module easier to maintain in the future

- extract deduplication out into a own method, so we can use it
on its own in some other places.

- add new insert_into_logs() method
the DB fields we want to write to are passed as arguments, this makes
it so we dont have to change the method when we add or remove DB fields
in the future.

- instead of using get_jid_id() all around gajim to get the jid_id
before we write to the DB, we only have to pass the jid to
insert_into_logs(), it makes sure to get the correct jid_id or add
one in case it doesnt exist.
This commit is contained in:
Philipp Hörist 2017-07-31 15:43:30 +02:00
parent 6925ce0628
commit 16fd64873e
1 changed files with 82 additions and 0 deletions

View File

@ -1174,6 +1174,88 @@ class Logger:
self.write(type_, with_, message=msg, tim=tim,
additional_data=additional_data, mam_query=True)
def search_for_duplicate(self, jid, timestamp, msg):
"""
Check if a message is already in the `logs` table
:param jid: The jid as string
:param timestamp: The timestamp in UTC epoch
:param msg: The message text
"""
# Add 5 minutes around the timestamp
start_time = timestamp - 300
end_time = timestamp + 300
log.debug('start: %s, end: %s, jid: %s, message: %s',
start_time, end_time, jid, msg)
sql = '''
SELECT * FROM logs
NATURAL JOIN jids WHERE jid = ? AND message = ?
AND time BETWEEN ? AND ?
'''
result = self.con.execute(sql, (jid, msg, start_time, end_time)).fetchone()
if result is not None:
log.debug('Message already in DB')
return True
return False
def insert_jid(self, jid, kind=None, type_=JIDConstant.NORMAL_TYPE):
"""
Insert a new jid into the `jids` table.
:param jid: The jid as string
:param kind: A KindConstant
:param type_: A JIDConstant
"""
if kind == KindConstant.GC_MSG:
type_ = JIDConstant.ROOM_TYPE
sql = 'INSERT OR IGNORE INTO jids (jid, type) VALUES (?, ?)'
self.con.execute(sql, (jid, type_))
self._timeout_commit()
def insert_into_logs(self, jid, time_, kind, unread=True, **kwargs):
"""
Insert a new message into the `logs` table
:param jid: The jid as string
:param time_: The timestamp in UTC epoch
:param kind: A KindConstant
:param unread: If True the message is added to the`unread_messages`
table. Only if kind == CHAT_MSG_RECV
:param kwargs: Every additional named argument must correspond to
a field in the `logs` table
"""
self.insert_jid(jid, kind=kind)
sql = '''
INSERT INTO logs (jid_id, time, kind, {columns})
VALUES ((SELECT jid_id FROM jids WHERE jid = ?), ?, ?, {values})
'''.format(columns=', '.join(kwargs.keys()),
values=', '.join('?' * len(kwargs)))
lastrowid = self.con.execute(sql, (jid, time_, kind, *kwargs.values())).lastrowid
if unread and kind == KindConstant.CHAT_MSG_RECV:
sql = '''INSERT INTO unread_messages (message_id, jid_id)
VALUES (?, (SELECT jid_id FROM jids WHERE jid = ?))'''
self.con.execute(sql, (lastrowid, jid))
self._timeout_commit()
return lastrowid
def _nec_gc_message_received(self, obj):
tim_f = float(obj.timestamp)
tim_int = int(tim_f)