Save account_id in logs table

This commit is contained in:
Philipp Hörist 2017-11-17 21:42:44 +01:00
parent a07470a7d2
commit 95357361bf
6 changed files with 25 additions and 13 deletions

View File

@ -433,7 +433,7 @@ class CommonConnection:
if obj.message is None:
return
app.logger.insert_into_logs(jid, obj.timestamp, obj.kind,
app.logger.insert_into_logs(self.name, jid, obj.timestamp, obj.kind,
message=obj.message,
subject=obj.subject,
additional_data=obj.additional_data,

View File

@ -946,7 +946,8 @@ class ConnectionHandlersBase:
app.config.should_log(self.name, obj.jid):
show = app.logger.convert_show_values_to_db_api_values(obj.show)
if show is not None:
app.logger.insert_into_logs(nbxmpp.JID(obj.jid).getStripped(),
app.logger.insert_into_logs(self.name,
nbxmpp.JID(obj.jid).getStripped(),
time_time(),
KindConstant.STATUS,
message=obj.status,
@ -1072,7 +1073,8 @@ class ConnectionHandlersBase:
# if not obj.nick, it means message comes from room itself
# usually it hold description and can be send at each connection
# so don't store it in logs
app.logger.insert_into_logs(obj.jid,
app.logger.insert_into_logs(self.name,
obj.jid,
obj.timestamp,
KindConstant.GC_MSG,
message=obj.msgtxt,
@ -1092,7 +1094,8 @@ class ConnectionHandlersBase:
subject = msg.getSubject()
if session.is_loggable():
app.logger.insert_into_logs(nbxmpp.JID(frm).getStripped(),
app.logger.insert_into_logs(self.name,
nbxmpp.JID(frm).getStripped(),
tim,
KindConstant.ERROR,
message=error_msg,

View File

@ -954,7 +954,8 @@ class GcPresenceReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
show = app.logger.convert_show_values_to_db_api_values(self.show)
if show is not None:
fjid = nbxmpp.JID(self.fjid)
app.logger.insert_into_logs(fjid.getStripped(),
app.logger.insert_into_logs(self.conn.name,
fjid.getStripped(),
time_time(),
KindConstant.GCSTATUS,
contact_name=fjid.getResource(),

View File

@ -282,6 +282,10 @@ class Logger:
return [user['jid'] for user in family]
return [jid]
def get_account_id(self, account):
jid = app.get_jid_from_account(account)
return self.get_jid_id(jid, type_=JIDConstant.NORMAL_TYPE)
def get_jid_id(self, jid, kind=None, type_=None):
"""
Get the jid id from a jid.
@ -1127,7 +1131,8 @@ class Logger:
"""
return self.get_jid_id(jid, kind, type_)
def insert_into_logs(self, jid, time_, kind, unread=True, **kwargs):
def insert_into_logs(self, account, jid, time_, kind,
unread=True, **kwargs):
"""
Insert a new message into the `logs` table
@ -1144,20 +1149,22 @@ class Logger:
a field in the `logs` table
"""
jid_id = self.get_jid_id(jid, kind=kind)
account_id = self.get_account_id(account)
if 'additional_data' in kwargs:
if not kwargs['additional_data']:
del kwargs['additional_data']
else:
kwargs['additional_data'] = json.dumps(kwargs["additional_data"])
sql = '''
INSERT INTO logs (jid_id, time, kind, {columns})
VALUES (?, ?, ?, {values})
INSERT INTO logs (account_id, jid_id, time, kind, {columns})
VALUES (?, ?, ?, ?, {values})
'''.format(columns=', '.join(kwargs.keys()),
values=', '.join('?' * len(kwargs)))
lastrowid = self.con.execute(sql, (jid_id, time_, kind) + tuple(kwargs.values())).lastrowid
lastrowid = self.con.execute(
sql, (account_id, jid_id, time_, kind) + tuple(kwargs.values())).lastrowid
log.info('Insert into DB: jid: %s, time: %s, kind: %s, stanza_id: %s',
jid, time_, kind, kwargs.get('stanza_id', None))

View File

@ -141,7 +141,8 @@ class ConnectionArchive313:
if duplicate:
# dont propagate the event further
return True
app.logger.insert_into_logs(obj.with_,
app.logger.insert_into_logs(self.name,
obj.with_,
obj.timestamp,
obj.kind,
unread=False,

View File

@ -117,7 +117,7 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
jid = obj.jid
obj.msg_log_id = app.logger.insert_into_logs(
jid, obj.timestamp, log_type,
self.conn.name, jid, obj.timestamp, log_type,
message=msg_to_log,
subject=obj.subject,
additional_data=obj.additional_data,