comparison sat/plugins/plugin_xep_0163.py @ 3823:5d72dc52ee4a

plugin XEP-0163: allow unset `event_type` in `addPEPEvent`, check conflict + type hints
author Goffi <goffi@goffi.org>
date Wed, 29 Jun 2022 12:08:53 +0200
parents 33d75cd3c371
children 524856bd7b19
comparison
equal deleted inserted replaced
3822:65bac82e4049 3823:5d72dc52ee4a
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from typing import Optional, Callable
20 from sat.core.i18n import _ 21 from sat.core.i18n import _
21 from sat.core import exceptions 22 from sat.core import exceptions
22 from sat.core.constants import Const as C 23 from sat.core.constants import Const as C
23 from sat.core.log import getLogger 24 from sat.core.log import getLogger
24 25
25 log = getLogger(__name__)
26 from twisted.words.xish import domish 26 from twisted.words.xish import domish
27 27
28 from wokkel import disco, pubsub 28 from wokkel import disco, pubsub
29 from wokkel.formats import Mood 29 from wokkel.formats import Mood
30 from sat.tools.common import data_format 30 from sat.tools.common import data_format
31
32
33 log = getLogger(__name__)
31 34
32 NS_USER_MOOD = "http://jabber.org/protocol/mood" 35 NS_USER_MOOD = "http://jabber.org/protocol/mood"
33 36
34 PLUGIN_INFO = { 37 PLUGIN_INFO = {
35 C.PI_NAME: "Personal Eventing Protocol Plugin", 38 C.PI_NAME: "Personal Eventing Protocol Plugin",
69 @param profile: profile we are handling 72 @param profile: profile we are handling
70 """ 73 """
71 disco_info.extend(list(map(disco.DiscoFeature, self.pep_events))) 74 disco_info.extend(list(map(disco.DiscoFeature, self.pep_events)))
72 return True 75 return True
73 76
74 def addPEPEvent(self, event_type, node, in_callback, out_callback=None, notify=True): 77 def addPEPEvent(
78 self,
79 event_type: Optional[str],
80 node: str,
81 in_callback: Callable,
82 out_callback: Optional[Callable] = None,
83 notify: bool = True
84 ) -> None:
75 """Add a Personal Eventing Protocol event manager 85 """Add a Personal Eventing Protocol event manager
76 86
77 @param event_type(unicode): type of the event (always uppercase), 87 @param event_type: type of the event (stored uppercase),
78 can be MOOD, TUNE, etc 88 only used when out_callback is set.
79 @param node(unicode): namespace of the node (e.g. http://jabber.org/protocol/mood 89 Can be MOOD, TUNE, etc.
90 @param node: namespace of the node (e.g. http://jabber.org/protocol/mood
80 for User Mood) 91 for User Mood)
81 @param in_callback(callable): method to call when this event occur 92 @param in_callback: method to call when this event occur
82 the callable will be called with (itemsEvent, profile) as arguments 93 the callable will be called with (itemsEvent, profile) as arguments
83 @param out_callback(callable,None): method to call when we want to publish this 94 @param out_callback: method to call when we want to publish this
84 event (must return a deferred) 95 event (must return a deferred)
85 the callable will be called when sendPEPEvent is called 96 the callable will be called when sendPEPEvent is called
86 @param notify(bool): add autosubscribe (+notify) if True 97 @param notify: add autosubscribe (+notify) if True
87 """ 98 """
88 if out_callback: 99 if event_type and out_callback:
100 event_type = event_type.upper()
101 if event_type in self.pep_out_cb:
102 raise exceptions.ConflictError(
103 f"event_type {event_type!r} already exists"
104 )
89 self.pep_out_cb[event_type] = out_callback 105 self.pep_out_cb[event_type] = out_callback
90 self.pep_events.add(node) 106 self.pep_events.add(node)
91 if notify: 107 if notify:
92 self.pep_events.add(node + "+notify") 108 self.pep_events.add(node + "+notify")
93 109