frontends: small message refactorisation:
- factorize some treatments that were done both in Primitivus and Libervia
- the way it was done, especially with QuickChat.printMessage returning a tuple, was quite disturbing
- FIXME: we can probably remove some arguments and do a deeper factorization e.g with QuickChatText
This commit is contained in:
parent
4d3d6fb8b8
commit
77c0656c85
1 changed files with 51 additions and 43 deletions
|
|
@ -140,20 +140,21 @@ class QuickChat(quick_widgets.QuickWidget):
|
|||
log_msg += _(u" (%d messages)" % size)
|
||||
log.debug(log_msg)
|
||||
|
||||
target = self.target.bare
|
||||
|
||||
def onHistory(history):
|
||||
for line in history:
|
||||
timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
|
||||
timestamp, from_jid, to_jid, message, type_, extra = line # FIXME: extra is unused !
|
||||
if ((self.type == C.CHAT_GROUP and type_ != C.MESS_TYPE_GROUPCHAT) or
|
||||
(self.type == C.CHAT_ONE2ONE and type_ == C.MESS_TYPE_GROUPCHAT)):
|
||||
continue
|
||||
self.printMessage(jid.JID(from_jid), message, {'timestamp':timestamp}, profile)
|
||||
extra["timestamp"] = timestamp
|
||||
self.newMessage(jid.JID(from_jid), target, message, type_, extra, profile)
|
||||
self.afterHistoryPrint()
|
||||
|
||||
def onHistoryError(err):
|
||||
log.error(_("Can't get history"))
|
||||
|
||||
target = self.target.bare
|
||||
|
||||
self.host.bridge.getHistory(unicode(self.host.profiles[profile].whoami.bare), unicode(target), size, True, search, profile, callback=onHistory, errback=onHistoryError)
|
||||
|
||||
def _get_nick(self, entity):
|
||||
|
|
@ -181,27 +182,39 @@ class QuickChat(quick_widgets.QuickWidget):
|
|||
# we have a private message, we forward it to a private conversation widget
|
||||
chat_widget = self.getOrCreatePrivateWidget(target)
|
||||
chat_widget.newMessage(from_jid, target, msg, type_, extra, profile)
|
||||
else:
|
||||
if type_ == C.MESS_TYPE_INFO:
|
||||
self.printInfo(msg, extra=extra)
|
||||
else:
|
||||
self.printMessage(from_jid, msg, extra, profile)
|
||||
|
||||
def printMessage(self, from_jid, msg, extra=None, profile=C.PROF_KEY_NONE):
|
||||
"""Print message in chat window. Must be implemented by child class"""
|
||||
nick = self._get_nick(from_jid)
|
||||
mymess = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare) #mymess = True if message comes from local user
|
||||
if msg.startswith('/me '):
|
||||
self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
|
||||
return
|
||||
return nick, mymess
|
||||
if type_ == C.MESS_TYPE_INFO:
|
||||
self.printInfo(msg, extra=extra)
|
||||
else:
|
||||
nick = self._get_nick(from_jid)
|
||||
if msg.startswith('/me '):
|
||||
self.printInfo('* %s %s' % (nick, msg[4:]), type_='me', extra=extra)
|
||||
else:
|
||||
# my_message is True if message comes from local user
|
||||
my_message = (from_jid.resource == self.nick) if self.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare)
|
||||
try:
|
||||
timestamp = float(extra['timestamp'])
|
||||
except KeyError:
|
||||
timestamp = None
|
||||
self.printMessage(nick, my_message, msg, timestamp, extra, profile)
|
||||
|
||||
def printMessage(self, nick, my_message, message, timestamp, extra=None, profile=C.PROF_KEY_NONE):
|
||||
"""Print message in chat window.
|
||||
|
||||
@param nick (unicode): author nick
|
||||
@param my_message (boolean): True if profile is the author
|
||||
@param message (unicode): message content
|
||||
@param extra (dict): extra data
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def printInfo(self, msg, type_='normal', extra=None):
|
||||
"""Print general info
|
||||
@param msg: message to print
|
||||
@type_: one of:
|
||||
normal: general info like "toto has joined the room"
|
||||
me: "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
|
||||
"""Print general info.
|
||||
|
||||
@param msg (unicode): message to print
|
||||
@param type_ (unicode):
|
||||
- 'normal': general info like "toto has joined the room"
|
||||
- 'me': "/me" information like "/me clenches his fist" ==> "toto clenches his fist"
|
||||
@param extra (dict): message data
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
|
|
|||
Loading…
Reference in a new issue