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,8 +125,24 @@ class SubscriptionManager:
if not keyword in subscriptions:
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={}):
'''
Adds a subscription to @keyword with data @data for @jid within
@ -182,22 +198,40 @@ class SubscriptionManager:
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.
'''
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:
return
if not jid in self._subscriptions[module]:
return
if not keyword in self._subscriptions[module][jid]:
return
if not item in self._subscriptions[module][jid][keyword]['data']:
return
self._subscriptions[module][jid][keyword]['data'].remove(item)
self.__flush()
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()
def __flush(self):
'''
Write subscription data to disk. Just an interface to StorageManager