Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits:
69d0c55b by Philipp Hörist at 2019-10-20T19:04:03Z
Add utility function for generating uuids
- - - - -
ff68a5df by Philipp Hörist at 2019-10-20T19:04:06Z
Receipts: Refactor helper method for building receipts
- Add helper isMucPM
- Move receipt build code into receipts module
- Improve receipt build code
- - - - -
3 changed files:
- nbxmpp/modules/receipts.py
- nbxmpp/protocol.py
- nbxmpp/util.py
Changes:
=====================================
nbxmpp/modules/receipts.py
=====================================
@@ -18,8 +18,12 @@
import logging
from nbxmpp.protocol import NS_RECEIPTS
+from nbxmpp.protocol import NS_MUC_USER
+from nbxmpp.protocol import isMucPM
+from nbxmpp.protocol import Message
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import ReceiptData
+from nbxmpp.util import generate_id
log = logging.getLogger('nbxmpp.m.receipts')
@@ -49,3 +53,34 @@ class Receipts:
return
properties.receipt = ReceiptData(received.getName(), id_)
+
+
+def build_receipt(stanza):
+ if not isinstance(stanza, Message):
+ raise ValueError('Stanza type must be protocol.Message')
+
+ if stanza.getType() == 'error':
+ raise ValueError('Receipt can not be generated for type error
messages')
+
+ if stanza.getID() is None:
+ raise ValueError('Receipt can not be generated for messages without
id')
+
+ if stanza.getTag('received', namespace=NS_RECEIPTS) is not None:
+ raise ValueError('Receipt can not be generated for receipts')
+
+ is_muc_pm = isMucPM(stanza)
+
+ jid = stanza.getFrom()
+ typ = stanza.getType()
+ if typ == 'groupchat' or not is_muc_pm:
+ jid.setBare()
+
+ message = Message(to=jid, typ=typ)
+ if is_muc_pm:
+ message.setTag('x', namespace=NS_MUC_USER)
+ message_id = generate_id()
+ message.setID(message_id)
+ message.setReceiptReceived(stanza.getID())
+ message.setHint('store')
+ message.setOriginID(message_id)
+ return message
=====================================
nbxmpp/protocol.py
=====================================
@@ -580,6 +580,12 @@ def isErrorNode(node):
"""
return node and node.getType() == 'error'
+def isMucPM(message):
+ muc_user = message.getTag('x', namespace=NS_MUC_USER)
+ return (message.getType() in ('chat', 'error') and
+ muc_user is not None and
+ not muc_user.getChildren())
+
class NodeProcessed(Exception):
"""
Exception that should be raised by handler when the handling should be
@@ -1325,11 +1331,6 @@ class Message(Protocol):
"""
self.setTag('origin-id', namespace=NS_SID, attrs={'id': val})
- def buildReceipt(self):
- message = Message(to=self.getFrom().getBare(), typ=self.getType())
- message.setReceiptReceived(self.getID())
- return message
-
def buildReply(self, text=None):
"""
Builds and returns another message object with specified text. The to,
@@ -1377,6 +1378,9 @@ class Message(Protocol):
def setAttention(self):
self.setTag('attention', namespace=NS_ATTENTION)
+ def setHint(self, hint):
+ self.setTag(hint, namespace=NS_HINTS)
+
class Presence(Protocol):
=====================================
nbxmpp/util.py
=====================================
@@ -19,6 +19,7 @@ import logging
import base64
import weakref
import hashlib
+import uuid
from functools import wraps
from functools import lru_cache
@@ -310,3 +311,7 @@ def compute_caps_hash(info, compare=True):
raise DiscoInfoMalformed('Caps hashes differ: %s != %s' % (
b64hash, info.get_caps_hash()))
return b64hash
+
+
+def generate_id():
+ return str(uuid.uuid4())
View it on GitLab:
https://dev.gajim.org/gajim/python-nbxmpp/compare/7bbf8194fbe6b54e41987778214c4f1417f07c11...ff68a5df155fef7b3d2d4cd3ce8a6b1a7a71e09c
--
View it on GitLab:
https://dev.gajim.org/gajim/python-nbxmpp/compare/7bbf8194fbe6b54e41987778214c4f1417f07c11...ff68a5df155fef7b3d2d4cd3ce8a6b1a7a71e09c
You're receiving this email because of your account on dev.gajim.org.
_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits