Philipp Hörist pushed to branch master at gajim / gajim
Commits:
0e13ea76 by Philipp Hörist at 2019-11-04T20:06:21Z
Add execution time debug info for all database querys
Print a warning message for all database querys > 50 ms
- - - - -
1 changed file:
- gajim/common/logger.py
Changes:
=====================================
gajim/common/logger.py
=====================================
@@ -183,6 +183,15 @@ def caps_decoder(dict_):
features=features,
dataforms=dataforms)
+def timeit(func):
+ def func_wrapper(self, *args, **kwargs):
+ start = time.monotonic_ns()
+ result = func(self, *args, **kwargs)
+ exec_time = (time.monotonic_ns() - start) / 1000000
+ level = 30 if exec_time > 50 else 10
+ log.log(level, 'Execution time for %s: %s ms', func.__name__, exec_time)
+ return result
+ return func_wrapper
def _convert_disco_info(disco_info):
return parse_disco_info(Iq(node=disco_info))
@@ -284,6 +293,7 @@ class Logger:
log.exception('Error')
sys.exit()
+ @timeit
def _migrate_logs(self, con):
if self._get_user_version(con) == 0:
# All migrations from 0.16.9 until 1.0.0
@@ -336,6 +346,7 @@ class Logger:
]
self._execute_multiple(con, statements)
+ @timeit
def _migrate_cache(self, con):
if self._get_user_version(con) == 0:
# All migrations from 0.16.9 until 1.0.0
@@ -434,6 +445,7 @@ class Logger:
self._con.close()
sys.exit()
+ @timeit
def _set_synchronous(self, sync):
try:
if sync:
@@ -459,6 +471,7 @@ class Logger:
def _like(search_str):
return '%{}%'.format(search_str)
+ @timeit
def commit(self):
try:
self._con.commit()
@@ -472,6 +485,7 @@ class Logger:
return
self._commit_timout_id = GLib.timeout_add(500, self.commit)
+ @timeit
def simple_commit(self, sql_to_commit):
"""
Helper to commit
@@ -479,6 +493,7 @@ class Logger:
self._con.execute(sql_to_commit)
self._timeout_commit()
+ @timeit
def _get_jid_ids_from_db(self):
"""
Load all jid/jid_id tuples into a dict for faster access
@@ -536,6 +551,7 @@ class Logger:
jid = app.get_jid_from_account(account)
return self.get_jid_id(jid, type_=JIDConstant.NORMAL_TYPE)
+ @timeit
def get_jid_id(self, jid, kind=None, type_=None):
"""
Get the jid id from a jid.
@@ -692,6 +708,7 @@ class Logger:
if sub == SubscriptionConstant.BOTH:
return 'both'
+ @timeit
def insert_unread_events(self, message_id, jid_id):
"""
Add unread message with id: message_id
@@ -701,6 +718,7 @@ class Logger:
self._con.execute(sql, (message_id, jid_id))
self._timeout_commit()
+ @timeit
def set_read_messages(self, message_ids):
"""
Mark all messages with ids in message_ids as read
@@ -709,6 +727,7 @@ class Logger:
sql = 'DELETE FROM unread_messages WHERE message_id IN (%s)' % ids
self.simple_commit(sql)
+ @timeit
def set_shown_unread_msgs(self, msg_log_id):
"""
Mark unread message as shown un GUI
@@ -717,6 +736,7 @@ class Logger:
msg_log_id
self.simple_commit(sql)
+ @timeit
def reset_shown_unread_messages(self):
"""
Set shown field to False in unread_messages table
@@ -724,6 +744,7 @@ class Logger:
sql = 'UPDATE unread_messages SET shown = 0'
self.simple_commit(sql)
+ @timeit
def get_unread_msgs(self):
"""
Get all unread messages
@@ -755,6 +776,7 @@ class Logger:
all_messages.append((result, shown))
return all_messages
+ @timeit
def get_last_conversation_lines(self, account, jid, pending):
"""
Get recent messages
@@ -818,6 +840,7 @@ class Logger:
start_of_day = int(time.mktime(local_time))
return start_of_day
+ @timeit
def get_conversation_for_date(self, account, jid, date):
"""
Load the complete conversation with a given jid on a specific date
@@ -849,6 +872,7 @@ class Logger:
(date.timestamp(),
(date + delta).timestamp())).fetchall()
+ @timeit
def search_log(self, account, jid, query, date=None):
"""
Search the conversation log for messages containing the `query` string.
@@ -890,6 +914,7 @@ class Logger:
return self._con.execute(sql, tuple(jids) + (query,)).fetchall()
+ @timeit
def get_days_with_logs(self, account, jid, year, month):
"""
Request the days in a month where we received messages
@@ -930,6 +955,7 @@ class Logger:
(date.timestamp(),
(date + delta).timestamp())).fetchall()
+ @timeit
def get_last_date_that_has_logs(self, account, jid):
"""
Get the timestamp of the last message we received for the jid.
@@ -956,6 +982,7 @@ class Logger:
# attributes set to None because of the MAX() function
return self._con.execute(sql, tuple(jids)).fetchone().time
+ @timeit
def get_first_date_that_has_logs(self, account, jid):
"""
Get the timestamp of the first message we received for the jid.
@@ -982,6 +1009,7 @@ class Logger:
# attributes set to None because of the MIN() function
return self._con.execute(sql, tuple(jids)).fetchone().time
+ @timeit
def get_date_has_logs(self, account, jid, date):
"""
Get single timestamp of a message we received for the jid
@@ -1013,6 +1041,7 @@ class Logger:
return self._con.execute(
sql, tuple(jids) + (start, end)).fetchone()
+ @timeit
def save_transport_type(self, jid, type_):
"""
Save the type of the transport in DB
@@ -1034,6 +1063,7 @@ class Logger:
self._con.execute(sql, (jid, type_id))
self._timeout_commit()
+ @timeit
def get_transports_type(self):
"""
Return all the type of the transports in DB
@@ -1047,6 +1077,7 @@ class Logger:
result.type)
return answer
+ @timeit
def load_caps_data(self):
'''
Load caps cache data
@@ -1064,6 +1095,7 @@ class Logger:
cache[(row.hash_method, row.hash)] = data
return cache
+ @timeit
def add_caps_entry(self, hash_method, hash_, caps_data):
serialized = json.dumps(caps_data, cls=CapsEncoder)
self._con.execute('''
@@ -1072,12 +1104,14 @@ class Logger:
''', (hash_method, hash_, serialized, int(time.time())))
self._timeout_commit()
+ @timeit
def update_caps_time(self, method, hash_):
sql = '''UPDATE caps_cache SET last_seen = ?
WHERE hash_method = ? and hash = ?'''
self._con.execute(sql, (int(time.time()), method, hash_))
self._timeout_commit()
+ @timeit
def clean_caps_table(self):
"""
Remove caps which was not seen for 3 months
@@ -1087,6 +1121,7 @@ class Logger:
(timestamp,))
self._timeout_commit()
+ @timeit
def replace_roster(self, account_name, roster_version, roster):
"""
Replace current roster in DB by a new one
@@ -1120,6 +1155,7 @@ class Logger:
app.config.set_per('accounts', account_name, 'roster_version',
roster_version)
+ @timeit
def del_contact(self, account_jid, jid):
"""
Remove jid from account_jid roster
@@ -1137,6 +1173,7 @@ class Logger:
(account_jid_id, jid_id))
self._timeout_commit()
+ @timeit
def add_or_update_contact(self, account_jid, jid, name, sub, ask, groups,
commit=True):
"""
@@ -1176,6 +1213,7 @@ class Logger:
if commit:
self._timeout_commit()
+ @timeit
def get_roster(self, account_jid):
"""
Return the accound_jid roster in NonBlockingRoster format
@@ -1220,6 +1258,7 @@ class Logger:
return data
+ @timeit
def remove_roster(self, account_jid):
"""
Remove the roster of an account
@@ -1241,6 +1280,7 @@ class Logger:
self._con.executescript(sql)
self._timeout_commit()
+ @timeit
def deduplicate_muc_message(self, account, jid, resource,
timestamp, message_id):
"""
@@ -1288,6 +1328,7 @@ class Logger:
return True
return False
+ @timeit
def search_for_duplicate(self, account, jid, timestamp, msg):
"""
Check if a message is already in the `logs` table
@@ -1323,6 +1364,7 @@ class Logger:
return True
return False
+ @timeit
def find_stanza_id(self, account, archive_jid, stanza_id, origin_id=None,
groupchat=False):
"""
@@ -1395,6 +1437,7 @@ class Logger:
"""
return self.get_jid_id(jid, kind, type_)
+ @timeit
def insert_into_logs(self, account, jid, time_, kind,
unread=True, **kwargs):
"""
@@ -1443,6 +1486,7 @@ class Logger:
return lastrowid
+ @timeit
def set_message_error(self, account_jid, jid, message_id, error):
"""
Update the corresponding message with the error
@@ -1471,6 +1515,7 @@ class Logger:
self._con.execute(sql, (error, account_id, jid_id, message_id))
self._timeout_commit()
+ @timeit
def set_marker(self, account_jid, jid, message_id, state):
"""
Update the marker state of the corresponding message
@@ -1503,6 +1548,7 @@ class Logger:
self._con.execute(sql, (state, account_id, jid_id, message_id))
self._timeout_commit()
+ @timeit
def set_avatar_sha(self, account_jid, jid, sha=None):
"""
Set the avatar sha of a jid on an account
@@ -1525,6 +1571,7 @@ class Logger:
self._con.execute(sql, (sha, account_jid_id, jid_id))
self._timeout_commit()
+ @timeit
def set_muc_avatar_sha(self, jid, sha=None):
"""
Set the avatar sha of a MUC
@@ -1546,6 +1593,7 @@ class Logger:
self._timeout_commit()
+ @timeit
def get_muc_avatar_sha(self, jid):
"""
Get the avatar sha of a MUC
@@ -1559,6 +1607,7 @@ class Logger:
if row is not None:
return row.avatar_sha
+ @timeit
def get_archive_infos(self, jid):
"""
Get the archive infos
@@ -1570,6 +1619,7 @@ class Logger:
sql = '''SELECT * FROM last_archive_message WHERE jid_id = ?'''
return self._con.execute(sql, (jid_id,)).fetchone()
+ @timeit
def set_archive_infos(self, jid, **kwargs):
"""
Set archive infos
@@ -1614,6 +1664,7 @@ class Logger:
log.info('Set message archive info: %s %s', jid, kwargs)
self._timeout_commit()
+ @timeit
def get_last_disco_info(self, jid, max_age=0):
"""
Get last disco info from jid
@@ -1648,6 +1699,7 @@ class Logger:
self._disco_info_cache[jid] = disco_info
return disco_info
+ @timeit
def set_last_disco_info(self, jid, disco_info):
"""
Get last disco info from jid
View it on GitLab: https://dev.gajim.org/gajim/gajim/commit/0e13ea76abf31d29a83ccff94448e7ed01…
--
View it on GitLab: https://dev.gajim.org/gajim/gajim/commit/0e13ea76abf31d29a83ccff94448e7ed01…
You're receiving this email because of your account on dev.gajim.org.
Philipp Hörist pushed to branch master at gajim / gajim
Commits:
56aa7baa by Philipp Hörist at 2019-11-01T21:43:34Z
Zeroconf: Fix pylint errors
- Rename vars
- Make vars/methods private
- - - - -
14d6074f by Philipp Hörist at 2019-11-02T15:19:57Z
Remove unused encrypted attribute
Encryption information is saved in additional_data attribute
- - - - -
6b723a89 by Philipp Hörist at 2019-11-02T15:25:23Z
Copy JID before modifying
- - - - -
02999050 by Philipp Hörist at 2019-11-02T15:31:59Z
Remove unused attributes
- - - - -
700747e0 by Philipp Hörist at 2019-11-02T18:23:38Z
Remove subject attribute from decrypted-message event
It's available via properties attribute
- - - - -
2f8058b2 by Philipp Hörist at 2019-11-02T18:30:12Z
Remove thread_id attribute from decrypted-message event
It's available via properties attribute
- - - - -
fc8930aa by Philipp Hörist at 2019-11-02T18:36:16Z
Remove attention attribute from decrypted-message event
It's available via properties attribute
- - - - -
05f9bd0b by Philipp Hörist at 2019-11-02T18:42:43Z
Remove user_nick attribute from decrypted-message event
It's available via properties attribute
- - - - -
056279ed by Philipp Hörist at 2019-11-02T19:03:23Z
Remove timestamp attribute from decrypted-message event
It's available via properties attribute
- - - - -
caf48a40 by Philipp Hörist at 2019-11-02T19:14:23Z
Remove message_id attribute from decrypted-message event
It's available via properties attribute
- - - - -
e8eb3e3d by Philipp Hörist at 2019-11-02T20:42:21Z
Remove sent attribute from decrypted-message event
It's available via properties attribute
- - - - -
55e1299d by Philipp Hörist at 2019-11-02T20:43:26Z
Remove unused forwarded attribute from decrypted-message event
- - - - -
83bff1b8 by Philipp Hörist at 2019-11-02T21:22:09Z
Remove ReStructured text -> XHTML conversion
This can be easily added back as a plugin if there is demand
- - - - -
ff8a1f28 by Philipp Hörist at 2019-11-03T21:02:03Z
Refactor XHTML event code
- Move xhtml data into additional_data
- Remove xhtml event attribute
- Allows now to store both message and xhtml data in the database
- Always store xhtml data
- - - - -
30 changed files:
- gajim/chat_control.py
- gajim/chat_control_base.py
- gajim/common/app.py
- gajim/common/caps_cache.py
- gajim/common/config.py
- gajim/common/connection.py
- gajim/common/connection_handlers_events.py
- gajim/common/events.py
- gajim/common/helpers.py
- gajim/common/modules/mam.py
- gajim/common/modules/message.py
- gajim/common/modules/misc.py
- gajim/common/modules/receipts.py
- gajim/common/modules/user_nickname.py
- − gajim/common/rst_xhtml_generator.py
- gajim/common/zeroconf/connection_handlers_zeroconf.py
- gajim/common/zeroconf/roster_zeroconf.py
- gajim/common/zeroconf/zeroconf_avahi.py
- gajim/common/zeroconf/zeroconf_bonjour.py
- gajim/conversation_textview.py
- gajim/data/gui/preferences_window.ui
- gajim/groupchat_control.py
- gajim/gtk/features.py
- gajim/gtk/history.py
- gajim/gtk/htmltextview.py
- gajim/gtk/preferences.py
- gajim/message_textview.py
- gajim/remote_control.py
- gajim/session.py
- mypy.ini
The diff was not included because it is too large.
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/fd75817fa45ed9191bdbb4c2ae62252c7…
--
View it on GitLab: https://dev.gajim.org/gajim/gajim/compare/fd75817fa45ed9191bdbb4c2ae62252c7…
You're receiving this email because of your account on dev.gajim.org.
Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits:
1a844d50 by Philipp Hörist at 2019-11-03T21:08:31Z
Refactor XHTML code
- setXHTML expects now a body Node
- getXHTML returns the html Node
- Add XHTMLData struct
- - - - -
4 changed files:
- nbxmpp/modules/message.py
- nbxmpp/modules/misc.py
- nbxmpp/protocol.py
- nbxmpp/structs.py
Changes:
=====================================
nbxmpp/modules/message.py
=====================================
@@ -19,8 +19,10 @@ import logging
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import NS_DATA
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import StanzaIDData
+from nbxmpp.structs import XHTMLData
from nbxmpp.util import error_factory
from nbxmpp.const import MessageType
@@ -79,7 +81,17 @@ class BaseMessage:
forms = stanza.getTags('x', namespace=NS_DATA)
if forms:
properties.forms = forms
- properties.xhtml = stanza.getXHTML()
+
+ xhtml = stanza.getXHTML()
+ if xhtml is None:
+ return
+
+ if xhtml.getTag('body', namespace=NS_XHTML) is None:
+ log.warning('xhtml without body found')
+ log.warning(stanza)
+ return
+
+ properties.xhtml = XHTMLData(xhtml)
@staticmethod
def _parse_type(stanza):
=====================================
nbxmpp/modules/misc.py
=====================================
@@ -22,6 +22,7 @@ from nbxmpp.protocol import NS_FORWARD
from nbxmpp.protocol import NS_MUC_USER
from nbxmpp.protocol import NS_MAM_1
from nbxmpp.protocol import NS_MAM_2
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import InvalidFrom
from nbxmpp.protocol import InvalidStanza
@@ -118,3 +119,16 @@ def unwrap_mam(stanza, own_jid):
archive=stanza.getFrom(),
namespace=result.getNamespace(),
timestamp=delay_timestamp)
+
+
+def build_xhtml_body(xhtml, xmllang=None):
+ try:
+ if xmllang is not None:
+ body = '<body xmlns="%s" xml:lang="%s">%s</body>' % (
+ NS_XHTML, xmllang, xhtml)
+ else:
+ body = '<body xmlns="%s">%s</body>' % (NS_XHTML, xhtml)
+ except Exception as error:
+ log.error('Error while building xhtml node: %s', error)
+ return None
+ return body
=====================================
nbxmpp/protocol.py
=====================================
@@ -26,7 +26,6 @@ from base64 import b64encode
from precis_i18n import get_profile
from nbxmpp.stringprepare import nameprep
from nbxmpp.simplexml import Node
-from nbxmpp.simplexml import NodeBuilder
def ascii_upper(s):
return s.upper()
@@ -1232,7 +1231,7 @@ class Message(Protocol):
payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
if body:
self.setBody(body)
- if xhtml:
+ if xhtml is not None:
self.setXHTML(xhtml)
if subject is not None:
self.setSubject(subject)
@@ -1243,22 +1242,8 @@ class Message(Protocol):
"""
return self.getTagData('body')
- def getXHTML(self, xmllang=None):
- """
- Return serialized xhtml-im element text of the message
-
- TODO: Returning a DOM could make rendering faster.
- """
- xhtml = self.getTag('html', namespace=NS_XHTML_IM)
- if xhtml:
- if xmllang:
- body = xhtml.getTag('body',
- namespace=NS_XHTML,
- attrs={'xml:lang': xmllang})
- else:
- body = xhtml.getTag('body', namespace=NS_XHTML)
- return str(body)
- return None
+ def getXHTML(self):
+ return self.getTag('html', namespace=NS_XHTML_IM)
def getSubject(self):
"""
@@ -1293,25 +1278,20 @@ class Message(Protocol):
Set the text of the message"""
self.setTagData('body', val)
- def setXHTML(self, val, xmllang=None):
- """
- Sets the xhtml text of the message (XEP-0071). The parameter is the
- "inner html" to the body.
- """
- try:
- if xmllang:
- dom = NodeBuilder('<body xmlns="%s" xml:lang="%s">%s</body>' \
- % (NS_XHTML, xmllang, val)).getDom()
+ def setXHTML(self, body, add=False):
+ if isinstance(body, str):
+ body = Node(node=body)
+ if add:
+ xhtml = self.getTag('html', namespace=NS_XHTML_IM)
+ if xhtml is not None:
+ xhtml.addChild(node=body)
else:
- dom = NodeBuilder('<body xmlns="%s">%s</body>' % (NS_XHTML,
- val), 0).getDom()
- if self.getTag('html'):
- self.getTag('html').addChild(node=dom)
- else:
- self.setTag('html', namespace=NS_XHTML_IM).addChild(node=dom)
- except Exception as e:
- print("Error" + str(e))
- # FIXME: log. we could not set xhtml (parse error, whatever)
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
+ else:
+ xhtml_nodes = self.getTags('html', namespace=NS_XHTML_IM)
+ for xhtml in xhtml_nodes:
+ self.delChild(xhtml)
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
def setSubject(self, val):
"""
=====================================
nbxmpp/structs.py
=====================================
@@ -26,6 +26,7 @@ from nbxmpp.protocol import NS_MAM_2
from nbxmpp.protocol import NS_MUC
from nbxmpp.protocol import NS_MUC_INFO
from nbxmpp.protocol import NS_CLIENT
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import Protocol
from nbxmpp.const import MessageType
from nbxmpp.const import AvatarState
@@ -831,3 +832,26 @@ class PresenceProperties:
return self.muc_user.role
except Exception:
return None
+
+
+class XHTMLData:
+ def __init__(self, xhtml):
+ self._bodys = {}
+ for body in xhtml.getTags('body', namespace=NS_XHTML):
+ lang = body.getXmlLang()
+ self._bodys[lang] = body
+
+ def get_body(self, pref_lang=None):
+ if pref_lang is not None:
+ body = self._bodys.get(pref_lang)
+ if body is not None:
+ return str(body)
+
+ body = self._bodys.get('en')
+ if body is not None:
+ return str(body)
+
+ body = self._bodys.get(None)
+ if body is not None:
+ return str(body)
+ return str(self._bodys.popitem()[1])
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/1a844d5042f90e42f577dc57fa…
--
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/1a844d5042f90e42f577dc57fa…
You're receiving this email because of your account on dev.gajim.org.
Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits:
05e74605 by Philipp Hörist at 2019-11-03T20:59:27Z
Refactor XHTML event code
- Move xhtml data into additional_data
- Remove xhtml event attribute
- Allows now to store both message and xhtml data in the database
- Always store xhtml data
- - - - -
4 changed files:
- nbxmpp/modules/message.py
- nbxmpp/modules/misc.py
- nbxmpp/protocol.py
- nbxmpp/structs.py
Changes:
=====================================
nbxmpp/modules/message.py
=====================================
@@ -19,8 +19,10 @@ import logging
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import NS_DATA
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import StanzaIDData
+from nbxmpp.structs import XHTMLData
from nbxmpp.util import error_factory
from nbxmpp.const import MessageType
@@ -79,7 +81,17 @@ class BaseMessage:
forms = stanza.getTags('x', namespace=NS_DATA)
if forms:
properties.forms = forms
- properties.xhtml = stanza.getXHTML()
+
+ xhtml = stanza.getXHTML()
+ if xhtml is None:
+ return
+
+ if xhtml.getTag('body', namespace=NS_XHTML) is None:
+ log.warning('xhtml without body found')
+ log.warning(stanza)
+ return
+
+ properties.xhtml = XHTMLData(xhtml)
@staticmethod
def _parse_type(stanza):
=====================================
nbxmpp/modules/misc.py
=====================================
@@ -22,6 +22,7 @@ from nbxmpp.protocol import NS_FORWARD
from nbxmpp.protocol import NS_MUC_USER
from nbxmpp.protocol import NS_MAM_1
from nbxmpp.protocol import NS_MAM_2
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import NodeProcessed
from nbxmpp.protocol import InvalidFrom
from nbxmpp.protocol import InvalidStanza
@@ -118,3 +119,16 @@ def unwrap_mam(stanza, own_jid):
archive=stanza.getFrom(),
namespace=result.getNamespace(),
timestamp=delay_timestamp)
+
+
+def build_xhtml_body(xhtml, xmllang=None):
+ try:
+ if xmllang is not None:
+ body = '<body xmlns="%s" xml:lang="%s">%s</body>' % (
+ NS_XHTML, xmllang, xhtml)
+ else:
+ body = '<body xmlns="%s">%s</body>' % (NS_XHTML, xhtml)
+ except Exception as error:
+ log.error('Error while building xhtml node: %s', error)
+ return None
+ return body
=====================================
nbxmpp/protocol.py
=====================================
@@ -26,7 +26,6 @@ from base64 import b64encode
from precis_i18n import get_profile
from nbxmpp.stringprepare import nameprep
from nbxmpp.simplexml import Node
-from nbxmpp.simplexml import NodeBuilder
def ascii_upper(s):
return s.upper()
@@ -1232,7 +1231,7 @@ class Message(Protocol):
payload=payload, timestamp=timestamp, xmlns=xmlns, node=node)
if body:
self.setBody(body)
- if xhtml:
+ if xhtml is not None:
self.setXHTML(xhtml)
if subject is not None:
self.setSubject(subject)
@@ -1243,22 +1242,8 @@ class Message(Protocol):
"""
return self.getTagData('body')
- def getXHTML(self, xmllang=None):
- """
- Return serialized xhtml-im element text of the message
-
- TODO: Returning a DOM could make rendering faster.
- """
- xhtml = self.getTag('html', namespace=NS_XHTML_IM)
- if xhtml:
- if xmllang:
- body = xhtml.getTag('body',
- namespace=NS_XHTML,
- attrs={'xml:lang': xmllang})
- else:
- body = xhtml.getTag('body', namespace=NS_XHTML)
- return str(body)
- return None
+ def getXHTML(self):
+ return self.getTag('html', namespace=NS_XHTML_IM)
def getSubject(self):
"""
@@ -1293,25 +1278,20 @@ class Message(Protocol):
Set the text of the message"""
self.setTagData('body', val)
- def setXHTML(self, val, xmllang=None):
- """
- Sets the xhtml text of the message (XEP-0071). The parameter is the
- "inner html" to the body.
- """
- try:
- if xmllang:
- dom = NodeBuilder('<body xmlns="%s" xml:lang="%s">%s</body>' \
- % (NS_XHTML, xmllang, val)).getDom()
+ def setXHTML(self, body, add=False):
+ if isinstance(body, str):
+ body = Node(node=body)
+ if add:
+ xhtml = self.getTag('html', namespace=NS_XHTML_IM)
+ if xhtml is not None:
+ xhtml.addChild(node=body)
else:
- dom = NodeBuilder('<body xmlns="%s">%s</body>' % (NS_XHTML,
- val), 0).getDom()
- if self.getTag('html'):
- self.getTag('html').addChild(node=dom)
- else:
- self.setTag('html', namespace=NS_XHTML_IM).addChild(node=dom)
- except Exception as e:
- print("Error" + str(e))
- # FIXME: log. we could not set xhtml (parse error, whatever)
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
+ else:
+ xhtml_nodes = self.getTags('html', namespace=NS_XHTML_IM)
+ for xhtml in xhtml_nodes:
+ self.delChild(xhtml)
+ self.addChild('html', namespace=NS_XHTML_IM, payload=body)
def setSubject(self, val):
"""
=====================================
nbxmpp/structs.py
=====================================
@@ -26,6 +26,7 @@ from nbxmpp.protocol import NS_MAM_2
from nbxmpp.protocol import NS_MUC
from nbxmpp.protocol import NS_MUC_INFO
from nbxmpp.protocol import NS_CLIENT
+from nbxmpp.protocol import NS_XHTML
from nbxmpp.protocol import Protocol
from nbxmpp.const import MessageType
from nbxmpp.const import AvatarState
@@ -831,3 +832,26 @@ class PresenceProperties:
return self.muc_user.role
except Exception:
return None
+
+
+class XHTMLData:
+ def __init__(self, xhtml):
+ self._bodys = {}
+ for body in xhtml.getTags('body', namespace=NS_XHTML):
+ lang = body.getXmlLang()
+ self._bodys[lang] = body
+
+ def get_body(self, pref_lang=None):
+ if pref_lang is not None:
+ body = self._bodys.get(pref_lang)
+ if body is not None:
+ return str(body)
+
+ body = self._bodys.get('en')
+ if body is not None:
+ return str(body)
+
+ body = self._bodys.get(None)
+ if body is not None:
+ return str(body)
+ return str(self._bodys.popitem()[1])
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/05e74605cf99c5955b3e09bd2b…
--
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/05e74605cf99c5955b3e09bd2b…
You're receiving this email because of your account on dev.gajim.org.
Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits:
7b0f5868 by Philipp Hörist at 2019-11-02T20:38:33Z
MessageProperties: Add more carbon propertys
- - - - -
1 changed file:
- nbxmpp/structs.py
Changes:
=====================================
nbxmpp/structs.py
=====================================
@@ -613,6 +613,14 @@ class MessageProperties:
def is_carbon_message(self):
return self.carbon is not None
+ @property
+ def is_sent_carbon(self):
+ return self.carbon is not None and self.carbon.is_sent
+
+ @property
+ def is_received_carbon(self):
+ return self.carbon is not None and self.carbon.is_received
+
@property
def is_mam_message(self):
return self.mam is not None
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/7b0f5868cd169c9411578268ea…
--
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/commit/7b0f5868cd169c9411578268ea…
You're receiving this email because of your account on dev.gajim.org.