Commit ff68a5df authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Receipts: Refactor helper method for building receipts

- Add helper isMucPM
- Move receipt build code into receipts module
- Improve receipt build code
parent 69d0c55b
......@@ -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:
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:
message = Message(to=jid, typ=typ)
if is_muc_pm:
message.setTag('x', namespace=NS_MUC_USER)
message_id = generate_id()
return message
......@@ -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())
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):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment