Use new DB API for logging status and errors

This commit is contained in:
Philipp Hörist 2017-08-08 14:41:55 +02:00
parent 635ce52856
commit 608655deed
3 changed files with 43 additions and 40 deletions

View file

@ -1059,7 +1059,13 @@ class ConnectionHandlersBase:
if gajim.config.get('log_contact_status_changes') and \ if gajim.config.get('log_contact_status_changes') and \
gajim.config.should_log(self.name, obj.jid): gajim.config.should_log(self.name, obj.jid):
gajim.logger.write('status', obj.jid, obj.status, obj.show) show = gajim.logger.convert_show_values_to_db_api_values(obj.show)
if show is not None:
gajim.logger.insert_into_logs(nbxmpp.JID(obj.jid).getStripped(),
time_time(),
KindConstant.STATUS,
message=obj.status,
show=show)
def _nec_gc_presence_received(self, obj): def _nec_gc_presence_received(self, obj):
if obj.conn.name != self.name: if obj.conn.name != self.name:
@ -1162,8 +1168,12 @@ class ConnectionHandlersBase:
subject = msg.getSubject() subject = msg.getSubject()
if session.is_loggable(): if session.is_loggable():
gajim.logger.write('error', frm, error_msg, tim=tim, gajim.logger.insert_into_logs(nbxmpp.JID(frm).getStripped(),
subject=subject) tim,
KindConstant.ERROR,
message=error_msg,
subject=subject)
gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self, gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
fjid=frm, error_code=msg.getErrorCode(), error_msg=error_msg, fjid=frm, error_code=msg.getErrorCode(), error_msg=error_msg,
msg=msgtxt, time_=tim, session=session, stanza=msg)) msg=msgtxt, time_=tim, session=session, stanza=msg))

View file

@ -932,7 +932,15 @@ class GcPresenceReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
if jid: if jid:
# we know real jid, save it in db # we know real jid, save it in db
st += ' (%s)' % jid st += ' (%s)' % jid
gajim.logger.write('gcstatus', self.fjid, st, self.show) show = gajim.logger.convert_show_values_to_db_api_values(self.show)
if show is not None:
fjid = nbxmpp.JID(self.fjid)
gajim.logger.insert_into_logs(fjid.getStripped(),
time_time(),
KindConstant.GCSTATUS,
contact_name=fjid.getResource(),
message=st,
show=show)
if self.avatar_sha == '': if self.avatar_sha == '':
# contact has no avatar # contact has no avatar
puny_nick = helpers.sanitize_filename(self.nick) puny_nick = helpers.sanitize_filename(self.nick)

View file

@ -328,7 +328,7 @@ class Logger:
self.jids_already_in.append(jid) self.jids_already_in.append(jid)
return jid_id return jid_id
def convert_human_values_to_db_api_values(self, kind, show): def convert_kind_values_to_db_api_values(self, kind):
""" """
Convert from string style to constant ints for db Convert from string style to constant ints for db
""" """
@ -349,25 +349,30 @@ class Logger:
elif kind == 'error': elif kind == 'error':
kind_col = KindConstant.ERROR kind_col = KindConstant.ERROR
return kind_col
def convert_show_values_to_db_api_values(self, show):
"""
Convert from string style to constant ints for db
"""
if show == 'online': if show == 'online':
show_col = ShowConstant.ONLINE return ShowConstant.ONLINE
elif show == 'chat': elif show == 'chat':
show_col = ShowConstant.CHAT return ShowConstant.CHAT
elif show == 'away': elif show == 'away':
show_col = ShowConstant.AWAY return ShowConstant.AWAY
elif show == 'xa': elif show == 'xa':
show_col = ShowConstant.XA return ShowConstant.XA
elif show == 'dnd': elif show == 'dnd':
show_col = ShowConstant.DND return ShowConstant.DND
elif show == 'offline': elif show == 'offline':
show_col = ShowConstant.OFFLINE return ShowConstant.OFFLINE
elif show is None: elif show is None:
show_col = None return ShowConstant.ONLINE
else: # invisible in GC when someone goes invisible else: # invisible in GC when someone goes invisible
# it's a RFC violation .... but we should not crash # it's a RFC violation .... but we should not crash
show_col = 'UNKNOWN' return None
return kind_col, show_col
def convert_human_transport_type_to_db_api_values(self, type_): def convert_human_transport_type_to_db_api_values(self, type_):
""" """
@ -552,7 +557,7 @@ class Logger:
all_messages.append((results, shown)) all_messages.append((results, shown))
return all_messages return all_messages
def write(self, kind, jid, message=None, show=None, tim=None, subject=None, def write(self, kind, jid, message=None, tim=None, subject=None,
additional_data=None, mam_query=False): additional_data=None, mam_query=False):
""" """
Write a row (status, gcstatus, message etc) to logs database Write a row (status, gcstatus, message etc) to logs database
@ -583,29 +588,11 @@ class Logger:
else: else:
time_col = float(time.time()) time_col = float(time.time())
kind_col, show_col = self.convert_human_values_to_db_api_values(kind, kind_col = self.convert_kind_values_to_db_api_values(kind)
show)
write_unread = False write_unread = False
try: try:
# now we may have need to do extra care for some values in columns if kind == 'gc_msg':
if kind == 'status': # we store (not None) time, jid, show, msg
# status for roster items
jid_id = self.get_jid_id(jid)
if show is None: # show is None (xmpp), but we say that 'online'
show_col = ShowConstant.ONLINE
elif kind == 'gcstatus':
# status in ROOM (for pm status see status)
if show is None: # show is None (xmpp), but we say that 'online'
show_col = ShowConstant.ONLINE
jid, nick = jid.split('/', 1)
# re-get jid_id for the new jid
jid_id = self.get_jid_id(jid, 'ROOM')
contact_name_col = nick
elif kind == 'gc_msg':
if jid.find('/') != -1: # if it has a / if jid.find('/') != -1: # if it has a /
jid, nick = jid.split('/', 1) jid, nick = jid.split('/', 1)
else: else:
@ -624,10 +611,8 @@ class Logger:
# Save in unread table only if it's not a pm # Save in unread table only if it's not a pm
write_unread = True write_unread = True
if show_col == 'UNKNOWN': # unknown show, do not log
return
values = (jid_id, contact_name_col, time_col, kind_col, show_col, values = (jid_id, contact_name_col, time_col, kind_col, None,
message_col, subject_col, additional_data_col) message_col, subject_col, additional_data_col)
return self.commit_to_db(values, write_unread) return self.commit_to_db(values, write_unread)
@ -1181,7 +1166,7 @@ class Logger:
:param type_: A JIDConstant :param type_: A JIDConstant
""" """
if kind == KindConstant.GC_MSG: if kind in (KindConstant.GC_MSG, KindConstant.GCSTATUS):
type_ = JIDConstant.ROOM_TYPE type_ = JIDConstant.ROOM_TYPE
sql = 'INSERT OR IGNORE INTO jids (jid, type) VALUES (?, ?)' sql = 'INSERT OR IGNORE INTO jids (jid, type) VALUES (?, ?)'
self.con.execute(sql, (jid, type_)) self.con.execute(sql, (jid, type_))