From 299ebfc4c8a51ca68765ba79d5f067541b18282a Mon Sep 17 00:00:00 2001 From: Shyam Sunder Date: Mon, 27 Sep 2021 16:10:41 -0400 Subject: [PATCH] server/opengraph: add remaining post tags --- server/szurubooru/api/opengraph_api.py | 34 ++++++++-- server/szurubooru/tests/api/test_opengraph.py | 62 ++++++++++++++++--- 2 files changed, 83 insertions(+), 13 deletions(-) diff --git a/server/szurubooru/api/opengraph_api.py b/server/szurubooru/api/opengraph_api.py index 5f7017ac..f71bc853 100644 --- a/server/szurubooru/api/opengraph_api.py +++ b/server/szurubooru/api/opengraph_api.py @@ -145,22 +145,44 @@ def get_post_html( ) -> rest.Response: try: post = _get_post(params) - title = f"Post {_get_post_id(params)} - {config.config['name']}" + title = f"{config.config['name']} - post {_get_post_id(params)}" except posts.InvalidPostIdError: # Return the default template and let the browser JS handle the 404 return _get_html_template() metadata = { "og:site_name": config.config["name"], - "og:type": "image", - "og:title": title, "og:url": f"{ctx.url_prefix}/post/{params['post_id']}", + "og:title": title, + "twitter:title": title, + "og:type": "article", } # Note: ctx.user will always be the anonymous user if auth.has_privilege(ctx.user, "posts:view"): - # TODO: Host Header and Proxy Prefix - metadata["og:image"] = posts.get_post_content_url(post) - + metadata["og:article:published_time"] = post.creation_time.isoformat() + if post.last_edit_time: + metadata[ + "og:article:modified_time" + ] = post.last_edit_time.isoformat() + metadata["og:image:alt"] = " ".join( + tag.first_name for tag in post.tags + ) + if post.type in (model.Post.TYPE_VIDEO): + metadata["twitter:card"] = "player" + metadata["og:video:url"] = posts.get_post_content_url(post) + metadata["twitter:player:stream"] = posts.get_post_content_url( + post + ) + metadata["og:image:url"] = posts.get_post_thumbnail_url(post) + if post.canvas_width and post.canvas_height: + metadata["og:video:width"] = str(post.canvas_width) + metadata["og:video:height"] = str(post.canvas_height) + metadata["twitter:player:width"] = str(post.canvas_width) + metadata["twitter:player:height"] = str(post.canvas_height) + else: + metadata["twitter:card"] = "summary_large_image" + metadata["og:image:url"] = posts.get_post_content_url(post) + metadata["twitter:image"] = posts.get_post_content_url(post) return _get_html_template( meta_tags=metadata, title=title, prefix=ctx.url_prefix ) diff --git a/server/szurubooru/tests/api/test_opengraph.py b/server/szurubooru/tests/api/test_opengraph.py index 8ab05aeb..4a63ecbc 100644 --- a/server/szurubooru/tests/api/test_opengraph.py +++ b/server/szurubooru/tests/api/test_opengraph.py @@ -3,7 +3,7 @@ from unittest.mock import patch import pytest import yattag -from szurubooru import api, db +from szurubooru import api, db, model from szurubooru.func import auth, posts @@ -14,8 +14,11 @@ def _make_meta_tag(name, content): @pytest.mark.parametrize("view_priv", [True, False]) +@pytest.mark.parametrize( + "post_type", [model.Post.TYPE_IMAGE, model.Post.TYPE_VIDEO] +) def test_get_post_html( - config_injector, context_factory, post_factory, view_priv + config_injector, context_factory, post_factory, view_priv, post_type ): config_injector( { @@ -25,16 +28,61 @@ def test_get_post_html( ) ctx = context_factory() ctx.url_prefix = "/someprefix" - db.session.add(post_factory(id=1)) + post = post_factory(id=1, type=post_type) + post.canvas_width = 1920 + post.canvas_height = 1080 + db.session.add(post) db.session.flush() with patch("szurubooru.func.auth.has_privilege"), patch( "szurubooru.func.posts.get_post_content_url" - ): + ), patch("szurubooru.func.posts.get_post_thumbnail_url"): auth.has_privilege.return_value = view_priv posts.get_post_content_url.return_value = "/content-url" + posts.get_post_thumbnail_url.return_value = "/thumbnail-url" ret = api.opengraph_api.get_post_html(ctx, {"post_id": 1}) assert _make_meta_tag("og:site_name", "test installation") in ret - assert _make_meta_tag("og:title", "Post 1 - test installation") in ret - if view_priv: - assert _make_meta_tag("og:image", "/content-url") in ret + assert _make_meta_tag("og:url", "/someprefix/post/1") in ret + assert _make_meta_tag("og:title", "test installation - post 1") in ret + assert _make_meta_tag("twitter:title", "test installation - post 1") in ret + assert _make_meta_tag("og:type", "article") in ret + assert ( + bool( + _make_meta_tag("og:article:published_time", "1996-01-01T00:00:00") + in ret + ) + == view_priv + ) + if post_type == model.Post.TYPE_VIDEO: + assert ( + bool(_make_meta_tag("twitter:card", "player") in ret) == view_priv + ) + assert ( + bool( + _make_meta_tag("twitter:player:stream", "/content-url") in ret + ) + == view_priv + ) + assert ( + bool(_make_meta_tag("og:video:url", "/content-url") in ret) + == view_priv + ) + assert ( + bool(_make_meta_tag("og:image:url", "/thumbnail-url") in ret) + == view_priv + ) + assert ( + bool(_make_meta_tag("og:video:width", "1920") in ret) == view_priv + ) + assert ( + bool(_make_meta_tag("og:video:height", "1080") in ret) == view_priv + ) + else: + assert ( + bool(_make_meta_tag("twitter:card", "summary_large_image") in ret) + == view_priv + ) + assert ( + bool(_make_meta_tag("twitter:image", "/content-url") in ret) + == view_priv + )