server/opengraph: add remaining post tags

This commit is contained in:
Shyam Sunder 2021-09-27 16:10:41 -04:00
parent fff0999e6a
commit 299ebfc4c8
2 changed files with 83 additions and 13 deletions

View file

@ -145,22 +145,44 @@ def get_post_html(
) -> rest.Response: ) -> rest.Response:
try: try:
post = _get_post(params) 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: except posts.InvalidPostIdError:
# Return the default template and let the browser JS handle the 404 # Return the default template and let the browser JS handle the 404
return _get_html_template() return _get_html_template()
metadata = { metadata = {
"og:site_name": config.config["name"], "og:site_name": config.config["name"],
"og:type": "image",
"og:title": title,
"og:url": f"{ctx.url_prefix}/post/{params['post_id']}", "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 # Note: ctx.user will always be the anonymous user
if auth.has_privilege(ctx.user, "posts:view"): if auth.has_privilege(ctx.user, "posts:view"):
# TODO: Host Header and Proxy Prefix metadata["og:article:published_time"] = post.creation_time.isoformat()
metadata["og:image"] = posts.get_post_content_url(post) 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( return _get_html_template(
meta_tags=metadata, title=title, prefix=ctx.url_prefix meta_tags=metadata, title=title, prefix=ctx.url_prefix
) )

View file

@ -3,7 +3,7 @@ from unittest.mock import patch
import pytest import pytest
import yattag import yattag
from szurubooru import api, db from szurubooru import api, db, model
from szurubooru.func import auth, posts 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("view_priv", [True, False])
@pytest.mark.parametrize(
"post_type", [model.Post.TYPE_IMAGE, model.Post.TYPE_VIDEO]
)
def test_get_post_html( 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( config_injector(
{ {
@ -25,16 +28,61 @@ def test_get_post_html(
) )
ctx = context_factory() ctx = context_factory()
ctx.url_prefix = "/someprefix" 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() db.session.flush()
with patch("szurubooru.func.auth.has_privilege"), patch( with patch("szurubooru.func.auth.has_privilege"), patch(
"szurubooru.func.posts.get_post_content_url" "szurubooru.func.posts.get_post_content_url"
): ), patch("szurubooru.func.posts.get_post_thumbnail_url"):
auth.has_privilege.return_value = view_priv auth.has_privilege.return_value = view_priv
posts.get_post_content_url.return_value = "/content-url" 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}) 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:site_name", "test installation") in ret
assert _make_meta_tag("og:title", "Post 1 - test installation") in ret assert _make_meta_tag("og:url", "/someprefix/post/1") in ret
if view_priv: assert _make_meta_tag("og:title", "test installation - post 1") in ret
assert _make_meta_tag("og:image", "/content-url") 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
)