core, bridge, quick_frontend: MUC private messages history management:

- history now store message type
    - sqlite3 storage: fixed resource management in getHistory
    - bridge: added message type in getHistory return value
    - quick_chats: (ugly) hacks to manage private history filtering/nickname printing
This commit is contained in:
Goffi 2012-10-17 00:35:48 +02:00
commit fef2b1a620
8 changed files with 52 additions and 30 deletions

View file

@ -100,18 +100,28 @@ class QuickChat():
debug (_("now we print history")) debug (_("now we print history"))
def onHistory(history): def onHistory(history):
for line in history: for line in history:
timestamp, from_jid, to_jid, message = line timestamp, from_jid, to_jid, message, _type = line
if ((self.type == 'group' and _type != 'groupchat') or
(self.type == 'one2one' and _type == 'groupchat')):
continue
self.printMessage(JID(from_jid), message, profile, timestamp) self.printMessage(JID(from_jid), message, profile, timestamp)
def onHistoryError(err): def onHistoryError(err):
error (_("Can't get history")) error (_("Can't get history"))
self.host.bridge.getHistory(self.host.profiles[profile]['whoami'].short, self.target.short, 20, callback=onHistory, errback=onHistoryError) if self.target.startswith(const_PRIVATE_PREFIX):
target = unescapePrivate(self.target)
else:
target = self.target.short
self.host.bridge.getHistory(self.host.profiles[profile]['whoami'].short, target, 20, callback=onHistory, errback=onHistoryError)
def _get_nick(self, jid): def _get_nick(self, jid):
"""Return nick of this jid when possible""" """Return nick of this jid when possible"""
if jid.startswith(const_PRIVATE_PREFIX): if self.target.startswith(const_PRIVATE_PREFIX):
return unescapePrivate(jid).resource unescaped = unescapePrivate(self.target)
if jid.startswith(const_PRIVATE_PREFIX) or unescaped.short == jid.short:
return unescaped.resource
return jid.resource if self.type == "group" else (self.host.contact_list.getCache(jid,'nick') or self.host.contact_list.getCache(jid,'name') or jid.node) return jid.resource if self.type == "group" else (self.host.contact_list.getCache(jid,'nick') or self.host.contact_list.getCache(jid,'name') or jid.node)
def printMessage(self, from_jid, msg, profile, timestamp): def printMessage(self, from_jid, msg, profile, timestamp):

View file

@ -268,7 +268,7 @@ class DbusObject(dbus.service.Object):
return self._callback("getEntityData", unicode(jid), keys, unicode(profile)) return self._callback("getEntityData", unicode(jid), keys, unicode(profile))
@dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
in_signature='ssib', out_signature='a(dsss)', in_signature='ssib', out_signature='a(dssss)',
async_callbacks=('callback', 'errback')) async_callbacks=('callback', 'errback'))
def getHistory(self, from_jid, to_jid, limit, between=True, callback=None, errback=None): def getHistory(self, from_jid, to_jid, limit, between=True, callback=None, errback=None):
return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, callback=callback, errback=errback) return self._callback("getHistory", unicode(from_jid), unicode(to_jid), limit, between, callback=callback, errback=errback)

View file

@ -462,14 +462,14 @@ async=
type=method type=method
category=core category=core
sig_in=ssib sig_in=ssib
sig_out=a(dsss) sig_out=a(dssss)
param_3_default=True param_3_default=True
doc=Get history of a communication between two entities doc=Get history of a communication between two entities
doc_param_0=from_jid: source JID (bare jid for catch all, full jid else) doc_param_0=from_jid: source JID (bare jid for catch all, full jid else)
doc_param_1=to_jid: dest JID (bare jid for catch all, full jid else) doc_param_1=to_jid: dest JID (bare jid for catch all, full jid else)
doc_param_2=limit: max number of history elements to get (0 for the whole history) doc_param_2=limit: max number of history elements to get (0 for the whole history)
doc_param_3=between: True if we want history between the two jids (in both direction), False if we only want messages from from_jid to to_jid doc_param_3=between: True if we want history between the two jids (in both direction), False if we only want messages from from_jid to to_jid
doc_return=Ordered list (by timestamp) of tuples (timestamp, full from_jid, full to_jid, message) doc_return=Ordered list (by timestamp) of tuples (timestamp, full from_jid, full to_jid, message, type)
[addContact] [addContact]
type=method type=method

View file

@ -499,7 +499,7 @@ class SAT(service.Service):
message.addElement("body", "jabber:client", mess_data["message"]) message.addElement("body", "jabber:client", mess_data["message"])
client.xmlstream.send(message) client.xmlstream.send(message)
if mess_data["type"]!="groupchat": if mess_data["type"]!="groupchat":
self.memory.addToHistory(current_jid, jid.JID(to), unicode(mess_data["message"]), profile=profile) #we don't add groupchat message to history, as we get them back self.memory.addToHistory(current_jid, jid.JID(to), unicode(mess_data["message"]), unicode(mess_data["type"]), profile=profile) #we don't add groupchat message to history, as we get them back
#and they will be added then #and they will be added then
self.bridge.newMessage(message['from'], unicode(mess_data["message"]), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it self.bridge.newMessage(message['from'], unicode(mess_data["message"]), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it

View file

@ -113,7 +113,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
mess_body = e.children[0] if e.children else "" mess_body = e.children[0] if e.children else ""
self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], profile=self.parent.profile) self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], profile=self.parent.profile)
if not u"delay" in [elem.name for elem in message.elements()]: #we don't save delayed messages in history if not u"delay" in [elem.name for elem in message.elements()]: #we don't save delayed messages in history
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, profile=self.parent.profile) self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile)
break break
class SatRosterProtocol(xmppim.RosterClientProtocol): class SatRosterProtocol(xmppim.RosterClientProtocol):

View file

@ -610,9 +610,9 @@ class Memory:
@param name: Name of the profile""" @param name: Name of the profile"""
return self.params.deleteProfile(name) return self.params.deleteProfile(name)
def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile="@NONE@"): def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile="@NONE@"):
assert(profile!="@NONE@") assert(profile!="@NONE@")
return self.storage.addToHistory(from_jid, to_jid, message, timestamp, profile) return self.storage.addToHistory(from_jid, to_jid, message, _type, timestamp, profile)
def getHistory(self, from_jid, to_jid, limit=0, between=True): def getHistory(self, from_jid, to_jid, limit=0, between=True):
return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between) return self.storage.getHistory(jid.JID(from_jid), jid.JID(to_jid), limit, between)

View file

@ -46,7 +46,13 @@ class SqliteStorage():
info(_("The database is new, creating the tables")) info(_("The database is new, creating the tables"))
database_creation = [ database_creation = [
"CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))", "CREATE TABLE profiles (id INTEGER PRIMARY KEY ASC, name TEXT, UNIQUE (name))",
"CREATE TABLE history (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id))", "CREATE TABLE message_types (type TEXT PRIMARY KEY)",
"INSERT INTO message_types VALUES ('chat')",
"INSERT INTO message_types VALUES ('error')",
"INSERT INTO message_types VALUES ('groupchat')",
"INSERT INTO message_types VALUES ('headline')",
"INSERT INTO message_types VALUES ('normal')",
"CREATE TABLE history (id INTEGER PRIMARY KEY ASC, profile_id INTEGER, source TEXT, dest TEXT, source_res TEXT, dest_res TEXT, timestamp DATETIME, message TEXT, type TEXT, FOREIGN KEY(profile_id) REFERENCES profiles(id), FOREIGN KEY(type) REFERENCES message_types(type))",
"CREATE TABLE param_gen (category TEXT, name TEXT, value TEXT, PRIMARY KEY (category,name))", "CREATE TABLE param_gen (category TEXT, name TEXT, value TEXT, PRIMARY KEY (category,name))",
"CREATE TABLE param_ind (category TEXT, name TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (category,name,profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id))", "CREATE TABLE param_ind (category TEXT, name TEXT, profile_id INTEGER, value TEXT, PRIMARY KEY (category,name,profile_id), FOREIGN KEY(profile_id) REFERENCES profiles(id))",
"CREATE TABLE private_gen (namespace TEXT, key TEXT, value TEXT, PRIMARY KEY (namespace, key))", "CREATE TABLE private_gen (namespace TEXT, key TEXT, value TEXT, PRIMARY KEY (namespace, key))",
@ -169,17 +175,18 @@ class SqliteStorage():
return d return d
#History #History
def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile=None): def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile=None):
"""Store a new message in history """Store a new message in history
@param from_jid: full source JID @param from_jid: full source JID
@param to_jid: full dest JID @param to_jid: full dest JID
@param message: message @param message: message
@param _type: message type (see RFC 6121 §5.2.2)
@param timestamp: timestamp in seconds since epoch, or None to use current time @param timestamp: timestamp in seconds since epoch, or None to use current time
""" """
assert(profile!=None) assert(profile!=None)
d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, profile_id) VALUES (?,?,?,?,?,?,?)", d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, type, profile_id) VALUES (?,?,?,?,?,?,?,?)",
(from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(), (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(),
message, self.profiles[profile])) message, _type, self.profiles[profile]))
d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" % d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" %
{"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message}))) {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message})))
return d return d
@ -194,33 +201,38 @@ class SqliteStorage():
query_result.reverse() query_result.reverse()
result = [] result = []
for row in query_result: for row in query_result:
timestamp, source, source_res, dest, dest_res, message = row timestamp, source, source_res, dest, dest_res, message, _type= row
result.append((timestamp, "%s/%s" % (source, source_res) if source_res else source, result.append((timestamp, "%s/%s" % (source, source_res) if source_res else source,
"%s/%s" % (dest, dest_res) if dest_res else dest, "%s/%s" % (dest, dest_res) if dest_res else dest,
message)) message, _type))
return result return result
query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message FROM history WHERE"]
query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message, type FROM history WHERE"]
values = [] values = []
def test_jid(_type,_jid):
values.append(_jid.userhost())
if _jid.resource:
values.append(_jid.resource)
return '(%s=? AND %s_res=?)' % (_type, _type)
return '%s=?' % (_type,)
if between: if between:
query_parts.append("(source=? OR source=?) AND (dest=? or dest=?)") query_parts.append("(%s OR %s) AND (%s or %s)" % (test_jid('source', from_jid),
values.extend([from_jid.userhost(), to_jid.userhost(), to_jid.userhost(), from_jid.userhost()]) test_jid('source', to_jid),
test_jid('dest', to_jid),
test_jid('dest', from_jid)))
else: else:
query_parts.append("source=? AND dest=?") query_parts.append("%s AND %s") % (test_jid('source', from_jid),
values.extend([from_jid.userhost(), to_jid.userhost()]) test_jid('dest', to_jid))
if from_jid.resource:
query_parts.append("AND source_res=?")
values.append(from_jid.resource)
if to_jid.resource:
query_parts.append("AND dest_res=?")
values.append(to_jid.resource)
query_parts.append("ORDER BY timestamp DESC") query_parts.append("ORDER BY timestamp DESC")
if limit: if limit:
query_parts.append("LIMIT ?") query_parts.append("LIMIT ?")
values.append(limit) values.append(limit)
d = self.dbpool.runQuery(" ".join(query_parts), values) d = self.dbpool.runQuery(" ".join(query_parts), values)
return d.addCallback(sqliteToDict) return d.addCallback(sqliteToDict)

View file

@ -60,7 +60,7 @@ class FakeMemory(object):
def getProfileName(self, profile_key): def getProfileName(self, profile_key):
return profile_key return profile_key
def addToHistory(self, from_jid, to_jid, message, timestamp=None, profile=None): def addToHistory(self, from_jid, to_jid, message, _type='chat', timestamp=None, profile=None):
pass pass
def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'): def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):