diff --git a/gajim/common/logger.py b/gajim/common/logger.py index d35fd6083..e2de43c95 100644 --- a/gajim/common/logger.py +++ b/gajim/common/logger.py @@ -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)