comparison sat_pubsub/backend.py @ 467:d86e0f8a1405

privilege: store roster cache in database: - rosters are now stored on database and restored on startup. This way, presence map can be restored without the need to wait for all contact to send presence again - roster version are checked, if a new version is received, presence map is updated accordingly - roster are not retrieved if presence are received in a too short delay (see ROSTER_TTL), to avoid using too much resources if a client connect/disconnect a lot The current behaviour works around XEP-0356 limitations. An update of the XEP will be needed to get roster pushes and roster version.
author Goffi <goffi@goffi.org>
date Fri, 15 Oct 2021 15:30:18 +0200
parents 391aa65f72b2
children ed9e12701e0f
comparison
equal deleted inserted replaced
466:0d38c3529972 467:d86e0f8a1405
953 raise error.Forbidden() 953 raise error.Forbidden()
954 954
955 if not roster[requestor].subscriptionFrom: 955 if not roster[requestor].subscriptionFrom:
956 raise error.Forbidden() 956 raise error.Forbidden()
957 957
958 d = self.getOwnerRoster(node) 958 d = defer.ensureDeferred(self.getOwnerRoster(node))
959 d.addCallback(gotRoster) 959 d.addCallback(gotRoster)
960 return d 960 return d
961 961
962 @defer.inlineCallbacks 962 @defer.inlineCallbacks
963 def checkRosterGroups(self, node, requestor): 963 def checkRosterGroups(self, node, requestor):
964 """check if requestor is in allowed groups of a node 964 """check if requestor is in allowed groups of a node
965 965
966 @param node(Node): node to check 966 @param node(Node): node to check
967 @param requestor(jid.JID): entity who want to access node 967 @param requestor(jid.JID): entity who want to access node
968 """ 968 """
969 roster = yield self.getOwnerRoster(node) 969 roster = yield defer.ensureDeferred(self.getOwnerRoster(node))
970 970
971 if roster is None: 971 if roster is None:
972 raise error.Forbidden() 972 raise error.Forbidden()
973 973
974 if requestor not in roster: 974 if requestor not in roster:
1075 ) 1075 )
1076 ) 1076 )
1077 d.addCallback(lambda items_data: [item_data.item for item_data in items_data]) 1077 d.addCallback(lambda items_data: [item_data.item for item_data in items_data])
1078 return d 1078 return d
1079 1079
1080 @defer.inlineCallbacks 1080 async def getOwnerRoster(self, node, owners=None):
1081 def getOwnerRoster(self, node, owners=None):
1082 # FIXME: roster of publisher, not owner, must be used 1081 # FIXME: roster of publisher, not owner, must be used
1083 if owners is None: 1082 if owners is None:
1084 owners = yield node.getOwners() 1083 owners = await node.getOwners()
1085 1084
1086 if len(owners) != 1: 1085 if len(owners) != 1:
1087 log.msg('publisher-roster access is not allowed with more than 1 owner') 1086 log.msg('publisher-roster access is not allowed with more than 1 owner')
1088 return 1087 return
1089 1088
1090 owner_jid = owners[0] 1089 owner_jid = owners[0]
1091 1090
1092 try: 1091 try:
1093 roster = yield self.privilege.getRoster(owner_jid) 1092 roster = await self.privilege.getRoster(owner_jid)
1094 except Exception as e: 1093 except Exception as e:
1095 log.msg("Error while getting roster of {owner_jid}: {msg}".format( 1094 log.msg("Error while getting roster of {owner_jid}: {msg}".format(
1096 owner_jid = owner_jid.full(), 1095 owner_jid = owner_jid.full(),
1097 msg = e)) 1096 msg = e))
1098 return 1097 return
1099 defer.returnValue(roster) 1098 return roster
1100 1099
1101 async def getItemsData(self, nodeIdentifier, requestor, recipient, maxItems=None, 1100 async def getItemsData(self, nodeIdentifier, requestor, recipient, maxItems=None,
1102 itemIdentifiers=None, ext_data=None): 1101 itemIdentifiers=None, ext_data=None):
1103 """like getItems but return the whole ItemData""" 1102 """like getItems but return the whole ItemData"""
1104 if maxItems == 0: 1103 if maxItems == 0: