From 8adb1681318270c15ffc738fe96ba8a927c84240 Mon Sep 17 00:00:00 2001 From: Monkey Do Date: Wed, 8 Jan 2025 17:59:29 +0100 Subject: [PATCH] Add OpenGraph tags to index.html Jinja template We need to add OG meta tags *before* the react page is loaded (at which point react-helmet can take over) as the tags are read on page load in a lot of cases. This system should allow us to add any arbitrary og: meta tag we want on various pages as needed, with sane defaults for existing OG tags such as og:type, og:description and og:title as we have them until now. --- listenbrainz/webserver/templates/index.html | 39 ++++++++++++--------- listenbrainz/webserver/views/playlist.py | 27 +++++++++++--- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/listenbrainz/webserver/templates/index.html b/listenbrainz/webserver/templates/index.html index dc993b9c11..71c1597ca6 100644 --- a/listenbrainz/webserver/templates/index.html +++ b/listenbrainz/webserver/templates/index.html @@ -1,5 +1,16 @@ {% from 'macros.html' import print_message %} - +{% + set opengraph_title = og_meta_tags['title'] if og_meta_tags and og_meta_tags['title'] + else "ListenBrainz" +%} +{% + set opengraph_description = og_meta_tags['description'] if og_meta_tags and og_meta_tags['description'] + else "Track, explore, visualise and share the music you listen to. Follow your favourites and discover great new music." +%} +{% + set opengraph_type = og_meta_tags['type'] if og_meta_tags and og_meta_tags['type'] + else "website" +%} @@ -11,18 +22,13 @@ - - - + + + + + {%- for tagname, tagvalue in (og_meta_tags or {}).items() if tagname not in ['description', 'title', 'type']-%} + + {%- endfor -%} + - - + + /') -def playlist_page(path): - return render_template("index.html") +@playlist_bp.route("/", defaults={'playlist_mbid': ''}) +@playlist_bp.route('//', methods=["GET"]) +def playlist_page(playlist_mbid: str): + current_user_id = None + og_meta_tags = None + + if current_user.is_authenticated: + current_user_id = current_user.id + + if is_valid_uuid(playlist_mbid): + playlist = db_playlist.get_by_mbid(db_conn, ts_conn, playlist_mbid, False) + if playlist is not None and playlist.is_visible_by(current_user_id): + og_meta_tags = { + "title": playlist.name, + "description": playlist.description, + "type": "music:playlist", + "url": f'{current_app.config["SERVER_ROOT_URL"]}/playlist/{playlist_mbid}', + "music:creator": f'{current_app.config["SERVER_ROOT_URL"]}/user/{playlist.creator}', + # "image": Once we have playlist images we can try adding it here + } + return render_template("index.html", og_meta_tags=og_meta_tags) @playlist_bp.route("//", methods=["POST"])