| ... |
... |
@@ -18,15 +18,14 @@ |
|
18
|
18
|
from nbxmpp.namespaces import Namespace
|
|
19
|
19
|
from nbxmpp.protocol import Iq
|
|
20
|
20
|
from nbxmpp.protocol import Node
|
|
21
|
|
-from nbxmpp.protocol import isResultNode
|
|
22
|
21
|
from nbxmpp.protocol import JID
|
|
23
|
22
|
from nbxmpp.structs import AnnotationNote
|
|
24
|
|
-from nbxmpp.structs import CommonResult
|
|
25
|
|
-from nbxmpp.modules.date_and_time import parse_datetime
|
|
26
|
|
-from nbxmpp.util import call_on_response
|
|
27
|
|
-from nbxmpp.util import callback
|
|
28
|
|
-from nbxmpp.util import raise_error
|
|
|
23
|
+from nbxmpp.errors import StanzaError
|
|
|
24
|
+from nbxmpp.errors import MalformedStanzaError
|
|
|
25
|
+from nbxmpp.task import iq_request_task
|
|
29
|
26
|
from nbxmpp.modules.base import BaseModule
|
|
|
27
|
+from nbxmpp.modules.util import process_response
|
|
|
28
|
+from nbxmpp.modules.date_and_time import parse_datetime
|
|
30
|
29
|
|
|
31
|
30
|
|
|
32
|
31
|
class Annotations(BaseModule):
|
| ... |
... |
@@ -40,21 +39,18 @@ class Annotations(BaseModule): |
|
40
|
39
|
def domain(self):
|
|
41
|
40
|
return self._client.get_bound_jid().domain
|
|
42
|
41
|
|
|
43
|
|
- @call_on_response('_annotations_received')
|
|
|
42
|
+ @iq_request_task
|
|
44
|
43
|
def request_annotations(self):
|
|
45
|
|
- self._log.info('Request annotations for %s', self.domain)
|
|
46
|
|
- payload = Node('storage', attrs={'xmlns': Namespace.ROSTERNOTES})
|
|
47
|
|
- return Iq(typ='get', queryNS=Namespace.PRIVATE, payload=payload)
|
|
|
44
|
+ _task = yield
|
|
48
|
45
|
|
|
49
|
|
- @callback
|
|
50
|
|
- def _annotations_received(self, stanza):
|
|
51
|
|
- if not isResultNode(stanza):
|
|
52
|
|
- return raise_error(self._log.info, stanza)
|
|
|
46
|
+ response = yield _make_request()
|
|
|
47
|
+ if response.isError():
|
|
|
48
|
+ raise StanzaError(response)
|
|
53
|
49
|
|
|
54
|
|
- storage = stanza.getQueryChild('storage')
|
|
|
50
|
+ query = response.getQuery()
|
|
|
51
|
+ storage = query.getTag('storage', namespace=Namespace.ROSTERNOTES)
|
|
55
|
52
|
if storage is None:
|
|
56
|
|
- return raise_error(self._log.warning, stanza, 'stanza-malformed',
|
|
57
|
|
- 'No annotations found')
|
|
|
53
|
+ raise MalformedStanzaError('storage node missing', response)
|
|
58
|
54
|
|
|
59
|
55
|
notes = []
|
|
60
|
56
|
for note in storage.getTags('note'):
|
| ... |
... |
@@ -80,26 +76,33 @@ class Annotations(BaseModule): |
|
80
|
76
|
self._log.info('Received annotations from %s:', self.domain)
|
|
81
|
77
|
for note in notes:
|
|
82
|
78
|
self._log.info(note)
|
|
83
|
|
- return notes
|
|
|
79
|
+ yield notes
|
|
84
|
80
|
|
|
85
|
|
- @call_on_response('_default_response')
|
|
|
81
|
+ @iq_request_task
|
|
86
|
82
|
def set_annotations(self, notes):
|
|
|
83
|
+ _task = yield
|
|
|
84
|
+
|
|
87
|
85
|
self._log.info('Set annotations for %s:', self.domain)
|
|
|
86
|
+
|
|
88
|
87
|
for note in notes:
|
|
89
|
88
|
self._log.info(note)
|
|
90
|
|
- storage = Node('storage', attrs={'xmlns': Namespace.ROSTERNOTES})
|
|
91
|
|
- for note in notes:
|
|
92
|
|
- node = Node('note', attrs={'jid': note.jid})
|
|
93
|
|
- node.setData(note.data)
|
|
94
|
|
- if note.cdate is not None:
|
|
95
|
|
- node.setAttr('cdate', note.cdate)
|
|
96
|
|
- if note.mdate is not None:
|
|
97
|
|
- node.setAttr('mdate', note.mdate)
|
|
98
|
|
- storage.addChild(node=node)
|
|
99
|
|
- return Iq(typ='set', queryNS=Namespace.PRIVATE, payload=storage)
|
|
100
|
|
-
|
|
101
|
|
- @callback
|
|
102
|
|
- def _default_response(self, stanza):
|
|
103
|
|
- if not isResultNode(stanza):
|
|
104
|
|
- return raise_error(self._log.info, stanza)
|
|
105
|
|
- return CommonResult(jid=stanza.getFrom())
|
|
|
89
|
+
|
|
|
90
|
+ response = yield _make_set_request(notes)
|
|
|
91
|
+ yield process_response(response)
|
|
|
92
|
+
|
|
|
93
|
+
|
|
|
94
|
+def _make_request():
|
|
|
95
|
+ payload = Node('storage', attrs={'xmlns': Namespace.ROSTERNOTES})
|
|
|
96
|
+ return Iq(typ='get', queryNS=Namespace.PRIVATE, payload=payload)
|
|
|
97
|
+
|
|
|
98
|
+def _make_set_request(notes):
|
|
|
99
|
+ storage = Node('storage', attrs={'xmlns': Namespace.ROSTERNOTES})
|
|
|
100
|
+ for note in notes:
|
|
|
101
|
+ node = Node('note', attrs={'jid': note.jid})
|
|
|
102
|
+ node.setData(note.data)
|
|
|
103
|
+ if note.cdate is not None:
|
|
|
104
|
+ node.setAttr('cdate', note.cdate)
|
|
|
105
|
+ if note.mdate is not None:
|
|
|
106
|
+ node.setAttr('mdate', note.mdate)
|
|
|
107
|
+ storage.addChild(node=node)
|
|
|
108
|
+ return Iq(typ='set', queryNS=Namespace.PRIVATE, payload=storage)
|