comparison sat/plugins/plugin_xep_0060.py @ 3757:5bda9d2e8b35

plugin XEP-0060: use serialised options in `psSubscribe` + triggers update
author Goffi <goffi@goffi.org>
date Fri, 13 May 2022 18:29:42 +0200
parents aa923e6b369f
children b7cef1b24f83
comparison
equal deleted inserted replaced
3756:aa923e6b369f 3757:5bda9d2e8b35
15 15
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 19
20 from typing import Optional, List, Tuple 20 from typing import Optional, List, Tuple, Dict, Union
21 from collections import namedtuple 21 from collections import namedtuple
22 import urllib.request, urllib.parse, urllib.error 22 import urllib.request, urllib.parse, urllib.error
23 from functools import reduce 23 from functools import reduce
24 from zope.interface import implementer 24 from zope.interface import implementer
25 from twisted.words.xish import domish 25 from twisted.words.xish import domish
253 async_=True, 253 async_=True,
254 ) 254 )
255 host.bridge.addMethod( 255 host.bridge.addMethod(
256 "psSubscribe", 256 "psSubscribe",
257 ".plugin", 257 ".plugin",
258 in_sign="ssa{ss}s", 258 in_sign="ssss",
259 out_sign="s", 259 out_sign="s",
260 method=self._subscribe, 260 method=self._subscribe,
261 async_=True, 261 async_=True,
262 ) 262 )
263 host.bridge.addMethod( 263 host.bridge.addMethod(
1145 1145
1146 def _subscribe(self, service, nodeIdentifier, options, profile_key=C.PROF_KEY_NONE): 1146 def _subscribe(self, service, nodeIdentifier, options, profile_key=C.PROF_KEY_NONE):
1147 client = self.host.getClient(profile_key) 1147 client = self.host.getClient(profile_key)
1148 service = None if not service else jid.JID(service) 1148 service = None if not service else jid.JID(service)
1149 d = defer.ensureDeferred( 1149 d = defer.ensureDeferred(
1150 self.subscribe(client, service, nodeIdentifier, options=options or None) 1150 self.subscribe(
1151 client,
1152 service,
1153 nodeIdentifier,
1154 options=data_format.deserialise(options)
1155 )
1151 ) 1156 )
1152 d.addCallback(lambda subscription: subscription.subscriptionIdentifier or "") 1157 d.addCallback(lambda subscription: subscription.subscriptionIdentifier or "")
1153 return d 1158 return d
1154 1159
1155 async def subscribe( 1160 async def subscribe(
1159 nodeIdentifier: str, 1164 nodeIdentifier: str,
1160 sub_jid: Optional[jid.JID] = None, 1165 sub_jid: Optional[jid.JID] = None,
1161 options: Optional[dict] = None 1166 options: Optional[dict] = None
1162 ) -> pubsub.Subscription: 1167 ) -> pubsub.Subscription:
1163 # TODO: reimplement a subscribtion cache, checking that we have not subscription before trying to subscribe 1168 # TODO: reimplement a subscribtion cache, checking that we have not subscription before trying to subscribe
1169 cont, trigger_sub = await self.host.trigger.asyncReturnPoint(
1170 "XEP-0060_subscribe", client, service, nodeIdentifier, sub_jid, options,
1171 )
1172 if not cont:
1173 return trigger_sub
1164 subscription = await client.pubsub_client.subscribe( 1174 subscription = await client.pubsub_client.subscribe(
1165 service, nodeIdentifier, sub_jid or client.jid.userhostJID(), options=options, 1175 service, nodeIdentifier, sub_jid or client.jid.userhostJID(), options=options,
1166 sender=client.jid.userhostJID() 1176 sender=client.jid.userhostJID()
1167 )
1168 await self.host.trigger.asyncPoint(
1169 "XEP-0060_subscribe", client, service, nodeIdentifier, sub_jid, options,
1170 subscription
1171 ) 1177 )
1172 return subscription 1178 return subscription
1173 1179
1174 def _unsubscribe(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE): 1180 def _unsubscribe(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
1175 client = self.host.getClient(profile_key) 1181 client = self.host.getClient(profile_key)
1179 async def unsubscribe( 1185 async def unsubscribe(
1180 self, 1186 self,
1181 client: SatXMPPEntity, 1187 client: SatXMPPEntity,
1182 service: jid.JID, 1188 service: jid.JID,
1183 nodeIdentifier: str, 1189 nodeIdentifier: str,
1184 sub_jid=None, 1190 sub_jid: Optional[jid.JID] = None,
1185 subscriptionIdentifier=None, 1191 subscriptionIdentifier: Optional[str] = None,
1186 sender=None, 1192 sender: Optional[jid.JID] = None,
1187 ): 1193 ) -> None:
1194 if not await self.host.trigger.asyncPoint(
1195 "XEP-0060_unsubscribe", client, service, nodeIdentifier, sub_jid,
1196 subscriptionIdentifier, sender
1197 ):
1198 return
1188 await client.pubsub_client.unsubscribe( 1199 await client.pubsub_client.unsubscribe(
1189 service, 1200 service,
1190 nodeIdentifier, 1201 nodeIdentifier,
1191 sub_jid or client.jid.userhostJID(), 1202 sub_jid or client.jid.userhostJID(),
1192 subscriptionIdentifier, 1203 subscriptionIdentifier,
1193 sender, 1204 sender,
1194 )
1195 await self.host.trigger.asyncPoint(
1196 "XEP-0060_unsubscribe", client, service, nodeIdentifier, sub_jid,
1197 subscriptionIdentifier, sender
1198 ) 1205 )
1199 1206
1200 def _subscriptions(self, service, nodeIdentifier="", profile_key=C.PROF_KEY_NONE): 1207 def _subscriptions(self, service, nodeIdentifier="", profile_key=C.PROF_KEY_NONE):
1201 client = self.host.getClient(profile_key) 1208 client = self.host.getClient(profile_key)
1202 service = None if not service else jid.JID(service) 1209 service = None if not service else jid.JID(service)