diff libervia/server/pages.py @ 1322:a0954b6610aa

pages: identities are not using `data_objects` anymore: - identities are now handler directly with the dict received from backend, without using a specific data object. - a new `fillMissingIdentities` method in `LiberivaPage` will help to get all needed identities before rendering the template, and to avoid missing avatar or nickname. - (blog/view): fill missing identities for main blog items, not only for comments
author Goffi <goffi@goffi.org>
date Sun, 02 Aug 2020 17:45:15 +0200
parents 6e2821e78489
children 898442c4ff68
line wrap: on
line diff
--- a/libervia/server/pages.py	Sat Aug 01 16:56:04 2020 +0200
+++ b/libervia/server/pages.py	Sun Aug 02 17:45:15 2020 +0200
@@ -28,7 +28,7 @@
 import traceback
 from pathlib import Path
 from functools import reduce
-from typing import Optional, List
+from typing import Optional, Union, List
 
 from twisted.web import server
 from twisted.web import resource as web_resource
@@ -42,6 +42,7 @@
 from sat.tools.utils import asDeferred
 from sat.tools.common import date_utils
 from sat.tools.common import utils
+from sat.tools.common import data_format
 from sat.core.log import getLogger
 from sat_frontends.bridge.bridge_frontend import BridgeException
 
@@ -1111,6 +1112,32 @@
         else:
             cache.clear()
 
+    # identities
+
+    async def fillMissingIdentities(
+        self,
+        request: server.Request,
+        entities: List[Union[str, jid.JID, None]],
+    ) -> None:
+        """Check if all entities have an identity cache, get missing ones from backend
+
+        @param request: request with a plugged profile
+        @param entities: entities to check, None or empty strings will be filtered
+        """
+        entities = {str(e) for e in entities if e}
+        profile = self.getProfile(request) or C.SERVICE_PROFILE
+        identities = self.host.getSessionData(
+            request,
+            session_iface.ISATSession
+        ).identities
+        for e in entities:
+            if e not in identities:
+                id_raw = await self.host.bridgeCall(
+                    'identityGet', e, [], True, profile)
+                identities[e] = data_format.deserialise(id_raw)
+
+    # signals, server => browser communication
+
     @classmethod
     def onSignal(cls, host, signal, *args):
         """Generic method which receive registered signals