Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0071.py @ 2080:3626b2813158
plugin XEP-0071: fixed rich and xhtml sending + add simple text body if not present
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 25 Sep 2016 19:17:38 +0200 |
parents | a2bc5089c2eb |
children | 2d633b3c923d |
comparison
equal
deleted
inserted
replaced
2079:c8e561a5b2b6 | 2080:3626b2813158 |
---|---|
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.constants import Const as C | 21 from sat.core.constants import Const as C |
22 from sat.core import exceptions | 22 from sat.core import exceptions |
23 from sat.core.log import getLogger | 23 from sat.core.log import getLogger |
24 log = getLogger(__name__) | 24 log = getLogger(__name__) |
25 from sat.tools.common import data_format | |
25 | 26 |
26 from twisted.internet import defer | 27 from twisted.internet import defer |
27 from wokkel import disco, iwokkel | 28 from wokkel import disco, iwokkel |
28 from zope.interface import implements | 29 from zope.interface import implements |
29 # from lxml import etree | 30 # from lxml import etree |
107 | 108 |
108 d_list = defer.DeferredList(defers) | 109 d_list = defer.DeferredList(defers) |
109 d_list.addCallback(lambda dummy: data) | 110 d_list.addCallback(lambda dummy: data) |
110 return d_list | 111 return d_list |
111 | 112 |
113 def _fill_body_text(self, text, data, lang): | |
114 data['message'][lang or ''] = text | |
115 message_elt = data['xml'] | |
116 body_elt = message_elt.addElement("body", content=text) | |
117 if lang: | |
118 body_elt[(C.NS_XML, 'lang')] = lang | |
119 | |
120 def _check_body_text(self, data, lang, markup, syntax, defers): | |
121 """check if simple text message exists, and fill if needed""" | |
122 if not (lang or '') in data['message']: | |
123 d = self._s.convert(markup, syntax, self._s.SYNTAX_TEXT) | |
124 d.addCallback(self._fill_body_text, data, lang) | |
125 defers.append(d) | |
126 | |
112 def _messageSendAddRich(self, data, client): | 127 def _messageSendAddRich(self, data, client): |
113 """ Construct XHTML-IM node and add it XML element | 128 """ Construct XHTML-IM node and add it XML element |
114 | 129 |
115 @param data: message data as sended by messageSend callback | 130 @param data: message data as sended by messageSend callback |
116 """ | 131 """ |
128 data['extra']['xhtml'] = xhtml_im | 143 data['extra']['xhtml'] = xhtml_im |
129 body_elt.addRawXml(xhtml_im) | 144 body_elt.addRawXml(xhtml_im) |
130 | 145 |
131 syntax = self._s.getCurrentSyntax(client.profile) | 146 syntax = self._s.getCurrentSyntax(client.profile) |
132 defers = [] | 147 defers = [] |
133 try: | 148 if u'xhtml' in data['extra']: |
134 rich = data['extra']['rich'] | |
135 except KeyError: | |
136 # we have directly XHTML | 149 # we have directly XHTML |
137 for lang, xhtml in data['extra']['xhtml'].iteritems(): | 150 for lang, xhtml in data_format.getSubDict('xhtml', data['extra']): |
151 self._check_body_text(data, lang, xhtml, self._s.SYNTAX_XHTML, defers) | |
138 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM) | 152 d = self._s.convert(xhtml, self._s.SYNTAX_XHTML, self.SYNTAX_XHTML_IM) |
139 d.addCallback(syntax_converted, lang) | 153 d.addCallback(syntax_converted, lang) |
140 defers.append(d) | 154 defers.append(d) |
141 else: | 155 elif u'rich' in data['extra']: |
142 # we have rich syntax to convert | 156 # we have rich syntax to convert |
143 for lang, rich_data in rich.iteritems(): | 157 for lang, rich_data in data_format.getSubDict('rich', data['extra']): |
158 self._check_body_text(data, lang, rich_data, syntax, defers) | |
144 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM) | 159 d = self._s.convert(rich_data, syntax, self.SYNTAX_XHTML_IM) |
145 d.addCallback(syntax_converted, lang) | 160 d.addCallback(syntax_converted, lang) |
146 defers.append(d) | 161 defers.append(d) |
162 else: | |
163 exceptions.InternalError(u"xhtml or rich should be present at this point") | |
147 d_list = defer.DeferredList(defers) | 164 d_list = defer.DeferredList(defers) |
148 d_list.addCallback(lambda dummy: data) | 165 d_list.addCallback(lambda dummy: data) |
149 return d_list | 166 return d_list |
150 | 167 |
151 def messageReceivedTrigger(self, client, message, post_treat): | 168 def messageReceivedTrigger(self, client, message, post_treat): |
160 body_elts = html_elt.elements(NS_XHTML, 'body') | 177 body_elts = html_elt.elements(NS_XHTML, 'body') |
161 post_treat.addCallback(self._messagePostTreat, body_elts) | 178 post_treat.addCallback(self._messagePostTreat, body_elts) |
162 return True | 179 return True |
163 | 180 |
164 def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): | 181 def messageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): |
165 """ Check presence of rich text in extra | 182 """ Check presence of rich text in extra """ |
166 """ | |
167 rich = {} | 183 rich = {} |
168 xhtml = {} | 184 xhtml = {} |
169 for key, value in data['extra'].iteritems(): | 185 for key, value in data['extra'].iteritems(): |
170 if key.startswith('rich'): | 186 if key.startswith('rich'): |
171 rich[key[5:]] = value | 187 rich[key[5:]] = value |