comparison sat_pubsub/backend.py @ 340:567e486bce24

backend (notifications): use inline callbacks in _notifyPublish + added PEP data in _prepareNotify
author Goffi <goffi@goffi.org>
date Sun, 20 Aug 2017 11:16:51 +0200
parents 516b282aa542
children b49f75a26156
comparison
equal deleted inserted replaced
339:516b282aa542 340:567e486bce24
1016 self.features.append("groupblog") 1016 self.features.append("groupblog")
1017 1017
1018 # if self.backend.supportsPublishModel(): #XXX: this feature is not really described in XEP-0060, we just can see it in examples 1018 # if self.backend.supportsPublishModel(): #XXX: this feature is not really described in XEP-0060, we just can see it in examples
1019 # self.features.append("publish_model") # but it's necessary for microblogging comments (see XEP-0277) 1019 # self.features.append("publish_model") # but it's necessary for microblogging comments (see XEP-0277)
1020 1020
1021
1022 def getFullItem(self, item_data):
1023 """ Attach item configuration to this item
1024
1025 Used to give item configuration back to node's owner (and *only* to owner)
1026 """
1027 # TODO: a test should check that only the owner get the item configuration back
1028
1029 item, item_config = item_data.item, item_data.config
1030 new_item = deepcopy(item)
1031 if item_config:
1032 new_item.addChild(item_config.toElement())
1033 return new_item
1034
1035 @defer.inlineCallbacks
1021 def _notifyPublish(self, data): 1036 def _notifyPublish(self, data):
1022 items_data = data['items_data'] 1037 items_data = data['items_data']
1023 node = data['node'] 1038 node = data['node']
1024 pep = data['pep'] 1039 pep = data['pep']
1025 recipient = data['recipient'] 1040 recipient = data['recipient']
1026 1041
1027 def afterPrepare(result): 1042 owners, notifications_filtered = yield self._prepareNotify(items_data, node, data.get('subscription'), pep, recipient)
1028 owners, notifications_filtered = result 1043
1029 #we notify the owners 1044 # we notify the owners
1030 #FIXME: check if this comply with XEP-0060 (option needed ?) 1045 # FIXME: check if this comply with XEP-0060 (option needed ?)
1031 #TODO: item's access model have to be sent back to owner 1046 # TODO: item's access model have to be sent back to owner
1032 #TODO: same thing for getItems 1047 # TODO: same thing for getItems
1033 1048
1034 def getFullItem(item_data): 1049 for owner_jid in owners:
1035 """ Attach item configuration to this item 1050 notifications_filtered.append(
1036 Used to give item configuration back to node's owner (and *only* to owner) 1051 (owner_jid,
1037 """ 1052 set([pubsub.Subscription(node.nodeIdentifier,
1038 #TODO: a test should check that only the owner get the item configuration back 1053 owner_jid,
1039 1054 'subscribed')]),
1040 item, item_config = item_data.item, item_data.config 1055 [self.getFullItem(item_data) for item_data in items_data]))
1041 new_item = deepcopy(item) 1056
1042 if item_config: 1057 if pep:
1043 new_item.addChild(item_config.toElement()) 1058 defer.returnValue(self.backend.privilege.notifyPublish(
1044 return new_item 1059 recipient,
1045 1060 node.nodeIdentifier,
1046 for owner_jid in owners: 1061 notifications_filtered))
1047 notifications_filtered.append( 1062
1048 (owner_jid, 1063 else:
1049 set([pubsub.Subscription(node.nodeIdentifier, 1064 defer.returnValue(self.pubsubService.notifyPublish(
1050 owner_jid, 1065 self.serviceJID,
1051 'subscribed')]), 1066 node.nodeIdentifier,
1052 [getFullItem(item_data) for item_data in items_data])) 1067 notifications_filtered))
1053
1054 if pep:
1055 return self.backend.privilege.notifyPublish(
1056 recipient,
1057 node.nodeIdentifier,
1058 notifications_filtered)
1059
1060 else:
1061 return self.pubsubService.notifyPublish(
1062 self.serviceJID,
1063 node.nodeIdentifier,
1064 notifications_filtered)
1065
1066 d = self._prepareNotify(items_data, node, data.get('subscription'))
1067 d.addCallback(afterPrepare)
1068 return d
1069 1068
1070 def _notifyRetract(self, data): 1069 def _notifyRetract(self, data):
1071 items_data = data['items_data'] 1070 items_data = data['items_data']
1072 node = data['node'] 1071 node = data['node']
1073 pep = data['pep'] 1072 pep = data['pep']
1095 return self.pubsubService.notifyRetract( 1094 return self.pubsubService.notifyRetract(
1096 self.serviceJID, 1095 self.serviceJID,
1097 node.nodeIdentifier, 1096 node.nodeIdentifier,
1098 notifications_filtered) 1097 notifications_filtered)
1099 1098
1100 d = self._prepareNotify(items_data, node, data.get('subscription')) 1099 d = self._prepareNotify(items_data, node, data.get('subscription'), pep, recipient)
1101 d.addCallback(afterPrepare) 1100 d.addCallback(afterPrepare)
1102 return d 1101 return d
1103 1102
1104 1103
1105 @defer.inlineCallbacks 1104 @defer.inlineCallbacks
1106 def _prepareNotify(self, items_data, node, subscription=None): 1105 def _prepareNotify(self, items_data, node, subscription=None, pep=None, recipient=None):
1107 """Do a bunch of permissions check and filter notifications 1106 """Do a bunch of permissions check and filter notifications
1108 1107
1109 The owner is not added to these notifications, 1108 The owner is not added to these notifications,
1110 it must be called by the calling method 1109 it must be added by the calling method
1111 @param items_data(tuple): must contain: 1110 @param items_data(tuple): must contain:
1112 - item (domish.Element) 1111 - item (domish.Element)
1113 - access_model (unicode) 1112 - access_model (unicode)
1114 - access_list (dict as returned getItemsById, or item_config) 1113 - access_list (dict as returned getItemsById, or item_config)
1115 @param node(LeafNode): node hosting the items 1114 @param node(LeafNode): node hosting the items
1118 @return (tuple): will contain: 1117 @return (tuple): will contain:
1119 - notifications_filtered 1118 - notifications_filtered
1120 - node_owner_jid 1119 - node_owner_jid
1121 - items_data 1120 - items_data
1122 """ 1121 """
1123
1124
1125 if subscription is None: 1122 if subscription is None:
1126 notifications = yield self.backend.getNotifications(node, items_data) 1123 notifications = yield self.backend.getNotifications(node, items_data)
1127 else: 1124 else:
1128 notifications = [(subscription.subscriber, [subscription], items_data)] 1125 notifications = [(subscription.subscriber, [subscription], items_data)]
1129 1126