fix: Implement more functions and fix some

This commit is contained in:
PapaTutuWawa 2021-06-13 13:10:05 +02:00
parent a7973459c5
commit 3b2b89061b

View File

@ -125,7 +125,23 @@ class SubscriptionManager:
if not keyword in subscriptions: if not keyword in subscriptions:
return False return False
return item in subscriptions[keyword] return item in subscriptions[keyword]['data']
def is_subscribed_to_data_one(self, module, jid, keyword, func):
'''
Like is_subscribed_to_data, but returns True if there is at
least one item for which func returns True.
'''
subscriptions = self.get_subscriptions_for(module, jid)
if not subscriptions:
return False
if not keyword in subscriptions:
return False
for item in subscriptions[keyword]['data']:
if func(item):
return True
return False
def add_subscription_for(self, module, jid, keyword, data={}): def add_subscription_for(self, module, jid, keyword, data={}):
''' '''
@ -182,20 +198,38 @@ class SubscriptionManager:
self.__flush() self.__flush()
def remove_item_for_subscription(self, module, jid, keyword, item): def remove_item_for_subscription(self, module, jid, keyword, item, flush=True):
''' '''
The deletion counterpart of append_data_for_subscription. The deletion counterpart of append_data_for_subscription.
''' '''
self.filter_items_for_subscription(module,
jid,
keyword,
func=lambda x: x == item,
flush=flush)
def filter_items_for_subscription(self, module, jid, keyword, func, flush=True):
'''
remove_item_for_subscription but for multiple items
'''
if not module in self._subscriptions: if not module in self._subscriptions:
return return
if not jid in self._subscriptions[module]: if not jid in self._subscriptions[module]:
return return
if not keyword in self._subscriptions[module][jid]: if not keyword in self._subscriptions[module][jid]:
return return
if not item in self._subscriptions[module][jid][keyword]['data']:
return
self._subscriptions[module][jid][keyword]['data'].remove(item) self._subscriptions[module][jid][keyword]['data'] = list(filter(func,
self._subscriptions[module][jid][keyword]['data']))
if not self._subscriptions[module][jid][keyword]['data']:
del self._subscriptions[module][jid][keyword]
if not self._subscriptions[module][jid]:
del self._subscriptions[module][jid]
if not self._subscriptions[module]:
del self._subscriptions[module]
if flush:
self.__flush() self.__flush()
def __flush(self): def __flush(self):