Mercurial > libervia-backend
diff src/plugins/plugin_xep_0071.py @ 1955:633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
/!\ database schema has been modified, do a backup before updating
message have been refactored, here are the main changes:
- languages are now handled
- all messages have an uid (internal to SàT)
- message updating is anticipated
- subject is now first class
- new naming scheme is used newMessage => messageNew, getHistory => historyGet, sendMessage => messageSend
- minimal compatibility refactoring in quick_frontend/Primitivus, better refactoring should follow
- threads handling
- delayed messages are saved into history
- info messages may also be saved in history (e.g. to keep track of people joining/leaving a room)
- duplicate messages should be avoided
- historyGet return messages in right order, no need to sort again
- plugins have been updated to follow new features, some of them need to be reworked (e.g. OTR)
- XEP-0203 (Delayed Delivery) is now fully handled in core, the plugin just handle disco and creation of a delay element
- /!\ jp and Libervia are currently broken, as some features of Primitivus
It has been put in one huge commit to avoid breaking messaging between changes.
This is the main part of message refactoring, other commits will follow to take profit of the new features/behaviour.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 24 May 2016 22:11:04 +0200 |
parents | 2daf7b4c6756 |
children | a2bc5089c2eb |
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0071.py Mon Apr 18 18:35:19 2016 +0200 +++ b/src/plugins/plugin_xep_0071.py Tue May 24 22:11:04 2016 +0200 @@ -22,6 +22,7 @@ from sat.core.log import getLogger log = getLogger(__name__) +from twisted.internet import defer from wokkel import disco, iwokkel from zope.interface import implements # from lxml import etree @@ -75,68 +76,108 @@ def __init__(self, host): log.info(_("XHTML-IM plugin initialization")) self.host = host - self.synt_plg = self.host.plugins["TEXT-SYNTAXES"] - self.synt_plg.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self.synt_plg.OPT_HIDDEN]) + self._s = self.host.plugins["TEXT-SYNTAXES"] + self._s.addSyntax(self.SYNTAX_XHTML_IM, lambda xhtml: xhtml, self.XHTML2XHTML_IM, [self._s.OPT_HIDDEN]) host.trigger.add("MessageReceived", self.messageReceivedTrigger) - host.trigger.add("sendMessage", self.sendMessageTrigger) + host.trigger.add("messageSend", self.messageSendTrigger) def getHandler(self, profile): return XEP_0071_handler(self) - def _messagePostTreat(self, data, body_elt): - """ Callback which manage the post treatment of the message in case of XHTML-IM found + def _messagePostTreat(self, data, body_elts): + """Callback which manage the post treatment of the message in case of XHTML-IM found @param data: data send by MessageReceived trigger through post_treat deferred - @param xhtml_im: XHTML-IM body element found + @param body_elts: XHTML-IM body elements found @return: the data with the extra parameter updated """ - #TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message + # TODO: check if text only body is empty, then try to convert XHTML-IM to pure text and show a warning message def converted(xhtml): - data['extra']['xhtml'] = xhtml - return data - d = self.synt_plg.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) - d.addCallback(converted) - return d + if lang: + data['extra']['xhtml_{}'.format(lang)] = xhtml + else: + data['extra']['xhtml'] = xhtml - def _sendMessageAddRich(self, mess_data, profile): + defers = [] + for body_elt in body_elts: + lang = body_elt.getAttribute('xml:lang', '') + d = self._s.convert(body_elt.toXml(), self.SYNTAX_XHTML_IM, safe=True) + d.addCallback(converted, lang) + defers.append(d) + + d_list = defer.DeferredList(defers) + d_list.addCallback(lambda dummy: data) + return d_list + + def _messageSendAddRich(self, data, client): """ Construct XHTML-IM node and add it XML element - @param mess_data: message data as sended by sendMessage callback + + @param data: message data as sended by messageSend callback """ - def syntax_converted(xhtml_im): - message_elt = mess_data['xml'] - html_elt = message_elt.addElement('html', NS_XHTML_IM) - body_elt = html_elt.addElement('body', NS_XHTML) + # at this point, either ['extra']['rich'] or ['extra']['xhtml'] exists + # but both can't exist at the same time + message_elt = data['xml'] + html_elt = message_elt.addElement((NS_XHTML_IM, 'html')) + + def syntax_converted(xhtml_im, lang): + body_elt = html_elt.addElement((NS_XHTML, 'body')) + if lang: + body_elt['xml:lang'] = lang + data['extra']['xhtml_{}'.format(lang)] = xhtml_im + else: + data['extra']['xhtml'] = xhtml_im body_elt.addRawXml(xhtml_im) - mess_data['extra']['xhtml'] = xhtml_im - return mess_data - syntax = self.synt_plg.getCurrentSyntax(profile) - rich = mess_data['extra'].get('rich', '') - xhtml = mess_data['extra'].get('xhtml', '') - if rich: - d = self.synt_plg.convert(rich, syntax, self.SYNTAX_XHTML_IM) - if xhtml: - raise exceptions.DataError(_("Can't have xhtml and rich content at the same time")) - d.addCallback(syntax_converted) - return d + syntax = self._s.getCurrentSyntax(client.profile) + defers = [] + try: + rich = data['extra']['rich'] + except KeyError: + # we have directly XHTML + for lang, xhtml in data['extra']['xhtml'].iteritems(): + d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM) + d.addCallback(syntax_converted, lang) + defers.append(d) + else: + # we have rich syntax to convert + for lang, rich_data in rich.iteritems(): + d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM) + d.addCallback(syntax_converted, lang) + defers.append(d) + d_list = defer.DeferredList(defers) + d_list.addCallback(lambda dummy: data) + return d_list - def messageReceivedTrigger(self, message, post_treat, profile): + def messageReceivedTrigger(self, client, message, post_treat): """ Check presence of XHTML-IM in message """ try: html_elt = message.elements(NS_XHTML_IM, 'html').next() - body_elt = html_elt.elements(NS_XHTML, 'body').next() - # OK, we have found rich text - post_treat.addCallback(self._messagePostTreat, body_elt) except StopIteration: # No XHTML-IM pass + else: + body_elts = html_elt.elements(NS_XHTML, 'body') + post_treat.addCallback(self._messagePostTreat, body_elts) return True - def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): + def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): """ Check presence of rich text in extra """ - if 'rich' in mess_data['extra'] or 'xhtml' in mess_data['extra']: - post_xml_treatments.addCallback(self._sendMessageAddRich, profile) + rich = {} + xhtml = {} + for key, value in data['extra'].iteritems(): + if key.startswith('rich'): + rich[key[5:]] = value + elif key.startswith('xhtml'): + xhtml[key[6:]] = value + if rich and xhtml: + raise exceptions.DataError(_(u"Can't have XHTML and rich content at the same time")) + if rich or xhtml: + if rich: + data['rich'] = rich + else: + data['xhtml'] = xhtml + post_xml_treatments.addCallback(self._messageSendAddRich, client) return True def _purgeStyle(self, styles_raw):