src/pyams_default_theme/shared/common/opengraph.py
changeset 379 240417d006df
parent 203 40bffafce365
child 385 e0f8d5d0e31e
--- a/src/pyams_default_theme/shared/common/opengraph.py	Wed Jan 16 18:55:19 2019 +0100
+++ b/src/pyams_default_theme/shared/common/opengraph.py	Wed Jan 16 18:56:36 2019 +0100
@@ -17,13 +17,14 @@
 
 from pyams_content.component.illustration import IIllustration, IIllustrationTarget
 from pyams_content.component.theme import ITagsInfo
+from pyams_content.features.share.interfaces import ISocialShareInfo
 from pyams_content.shared.common import IWfSharedContent
 from pyams_file.interfaces import IThumbnails
 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
 from pyams_skin.interfaces.configuration import IConfiguration
 from pyams_skin.interfaces.metas import IHTMLContentMetas
 from pyams_skin.layer import IPyAMSUserLayer
-from pyams_skin.metas import PropertyMeta
+from pyams_skin.metas import ContentMeta, PropertyMeta, SchemaMeta
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
 from pyams_utils.registry import get_utility
 from pyams_utils.traversing import get_parent
@@ -32,21 +33,27 @@
 
 
 @adapter_config(name='opengraph', context=(IWfSharedContent, IPyAMSUserLayer, Interface), provides=IHTMLContentMetas)
-class OpengraphSharedContentMetasAdapter(ContextRequestViewAdapter):
-    """Opengraph shared content metas adapter"""
+class SharedContentOpengraphMetasAdapter(ContextRequestViewAdapter):
+    """Shared content opengraph metas adapter"""
 
     order = 15
 
     def get_metas(self):
         context = self.context
+        request = self.request
         i18n = II18n(context)
-        request = self.request
         negotiator = get_utility(INegotiator)
         lang = negotiator.server_language
+
+        description = i18n.query_attribute('description', lang=lang, request=request) or \
+                      i18n.query_attribute('header', lang=lang, request=request)
+
         # main properties
+        yield PropertyMeta('og:type', 'article')
         yield PropertyMeta('og:title', i18n.query_attribute('title', lang=lang, request=request))
-        yield PropertyMeta('og:description', i18n.query_attribute('description', lang=lang, request=request) or '')
-        yield PropertyMeta('og:type', 'article')
+        if description:
+            yield PropertyMeta('og:description', description)
+
         # workflow informations
         dc = IZopeDublinCore(context, None)
         if (dc is not None) and dc.modified:
@@ -57,18 +64,22 @@
                 yield PropertyMeta('article:published_time', pub_info.first_publication_date.isoformat())
             if pub_info.publication_expiration_date:
                 yield PropertyMeta('article:expiration_time', pub_info.publication_expiration_date.isoformat())
+
         # tags
         tags = ITagsInfo(context, None)
         if tags is not None:
             for tag in tags.tags or ():
                 yield PropertyMeta('article:tag', tag.label)
+
         # URL and site name
         yield PropertyMeta('og:url', canonical_url(context, request))
         configuration = IConfiguration(request.root)
         yield PropertyMeta('og:site_name', configuration.title)
+
         # illustration properties
         registry = request.registry
         illustration = None
+        thumbnail = None
         target = context
         while target is not None:
             illustration = registry.queryAdapter(target, IIllustration, name='link')
@@ -80,14 +91,41 @@
         if (target is not None) and (illustration is not None):
             data = II18n(illustration).query_attribute('data', lang=lang, request=request)
             thumbnail = IThumbnails(data).get_thumbnail('800x600')
-            yield PropertyMeta('og:image:url', absolute_url(thumbnail, self.request))
+            yield PropertyMeta('og:image', absolute_url(thumbnail, request))
+            if request.scheme == 'https':
+                yield PropertyMeta('og:image:secure_url', absolute_url(thumbnail, request))
+            else:
+                yield PropertyMeta('og:image:url', absolute_url(thumbnail, request))
             yield PropertyMeta('og:image:type', thumbnail.content_type)
             image_size = thumbnail.image_size
             yield PropertyMeta('og:image:width', image_size[0])
             yield PropertyMeta('og:image:height', image_size[1])
-            yield PropertyMeta('og:image:alt', II18n(illustration).query_attribute('alt_title', lang=lang,
-                                                                                   request=request))
+            alt = II18n(illustration).query_attribute('alt_title', lang=lang, request=request)
+            if alt:
+                yield PropertyMeta('og:image:alt', alt)
+
         # locales properties
         yield PropertyMeta('og:locale', lang)
-        for lang in II18nManager(context).languages or ():
-            yield PropertyMeta('og:locale:alternate', lang)
+        for other_lang in II18nManager(context).languages or ():
+            if other_lang != lang:
+                yield PropertyMeta('og:locale:alternate', other_lang)
+
+        # twitter properties
+        share_info = ISocialShareInfo(request.root, None)
+        if (share_info is not None) and share_info.twitter_account:
+            yield ContentMeta('twitter:site', share_info.twitter_account)
+            yield ContentMeta('twitter:creator', share_info.twitter_creator_account or share_info.twitter_account)
+        if illustration is not None:
+            yield ContentMeta('twitter:card', 'summary_large_image')
+        else:
+            yield ContentMeta('twitter:card', 'summary')
+        yield ContentMeta('twitter:title', i18n.query_attribute('title', lang=lang, request=request))
+        if description:
+            yield ContentMeta('twitter:description', description)
+
+        # Schema.org properties
+        yield SchemaMeta('name', i18n.query_attribute('title', lang=lang, request=request))
+        if description:
+            yield SchemaMeta('description', description)
+        if thumbnail is not None:
+            yield SchemaMeta('image', absolute_url(thumbnail, request))