diff src/server/server.py @ 1093:eda7a1c6532a

server: new getAffiliation method: getAffiliation allows to retrieve affiliation to a pubsub node for logged user. The method handle cache in user's session, avoiding requesting pubsub service when possible.
author Goffi <goffi@goffi.org>
date Fri, 01 Jun 2018 13:03:52 +0200
parents 9c41b7e91172
children 01e95ec9df9e
line wrap: on
line diff
--- a/src/server/server.py	Fri Jun 01 12:58:20 2018 +0200
+++ b/src/server/server.py	Fri Jun 01 13:03:52 2018 +0200
@@ -1920,6 +1920,37 @@
         else:
             return (iface(session) for iface in args)
 
+    @defer.inlineCallbacks
+    def getAffiliation(self, request, service, node):
+        """retrieve pubsub node affiliation for current user
+
+        use cache first, and request pubsub service if not cache is found
+        @param request(server.Request): request linked to the session
+        @param service(jid.JID): pubsub service
+        @param node(unicode): pubsub node
+        @return (unicode): affiliation
+        """
+        sat_session = self.getSessionData(request, session_iface.ISATSession)
+        if sat_session.profile is None:
+            raise exceptions.InternalError(u'profile must be set to use this method')
+        affiliation = sat_session.getAffiliation(service, node)
+        if affiliation is not None:
+            defer.returnValue(affiliation)
+        else:
+            try:
+                affiliations = yield self.bridgeCall('psAffiliationsGet', service.full(), node, sat_session.profile)
+            except Exception as e:
+                log.warning("Can't retrieve affiliation for {service}/{node}: {reason}".format(
+                    service=service, node=node, reason=e))
+                affiliation = u""
+            else:
+                try:
+                    affiliation = affiliations[node]
+                except KeyError:
+                    affiliation = u""
+            sat_session.setAffiliation(service, node, affiliation)
+            defer.returnValue(affiliation)
+
     ## Websocket (dynamic pages) ##
 
     def getWebsocketURL(self, request):