diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-10-20 21:27:48 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-10-20 22:04:06 +0300 |
commit | ff68a5df155fef7b3d2d4cd3ce8a6b1a7a71e09c (patch) | |
tree | 267d41e199f39a468b6e8cd6bdc07bcf37b576b1 /nbxmpp | |
parent | 69d0c55b0715ad50b4b3e9bdddc9aa4ef6ffbec8 (diff) |
Receipts: Refactor helper method for building receipts
- Add helper isMucPM
- Move receipt build code into receipts module
- Improve receipt build code
Diffstat (limited to 'nbxmpp')
-rw-r--r-- | nbxmpp/modules/receipts.py | 35 | ||||
-rw-r--r-- | nbxmpp/protocol.py | 14 |
2 files changed, 44 insertions, 5 deletions
diff --git a/nbxmpp/modules/receipts.py b/nbxmpp/modules/receipts.py index f0be9b5..9a31ff5 100644 --- a/nbxmpp/modules/receipts.py +++ b/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 diff --git a/nbxmpp/protocol.py b/nbxmpp/protocol.py index 9b2acbb..54bdd1d 100644 --- a/nbxmpp/protocol.py +++ b/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): |