From 93ba4086717bfc66ed61b4c9ab3d8a89350d74c4 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 4 Aug 2021 10:39:38 +0200 Subject: [PATCH] Improve group profile metadata * Add canonical link * Add link alternate to application/activity+json representation * add robots noindex on remote profiles Signed-off-by: Thomas Citharel --- lib/service/metadata/actor.ex | 46 ++++++++++++++++++++++--- test/service/metadata/metadata_test.exs | 7 ++-- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/lib/service/metadata/actor.ex b/lib/service/metadata/actor.ex index 0e1622515..e0fc59dc7 100644 --- a/lib/service/metadata/actor.ex +++ b/lib/service/metadata/actor.ex @@ -23,11 +23,17 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do Tag.tag(:meta, property: "og:url", content: group.url), Tag.tag(:meta, property: "og:description", content: group.summary), Tag.tag(:meta, property: "og:type", content: "profile"), - Tag.tag(:meta, property: "profile:username", content: group.preferred_username), + Tag.tag(:meta, + property: "profile:username", + content: Actor.preferred_username_and_domain(group) + ), Tag.tag(:meta, property: "twitter:card", content: "summary") ] |> maybe_add_avatar(group) |> add_group_schema(group) + |> add_group_feeds(group) + |> add_canonical(group) + |> maybe_add_no_index(group) end def build_tags(%Actor{} = _actor, _locale), do: [] @@ -42,27 +48,59 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do end end + @spec add_group_schema(list(Tag.t()), Actor.t()) :: list(Tag.t()) defp add_group_schema(tags, %Actor{} = group) do tags ++ [ - ~s{} |> HTML.raw(), + ~s{} |> HTML.raw() + ] + end + + @spec add_group_feeds(list(Tag.t()), Actor.t()) :: list(Tag.t()) + defp add_group_feeds(tags, %Actor{} = group) do + tags ++ + [ Tag.tag(:link, rel: "alternate", type: "application/atom+xml", title: gettext("%{name}'s feed", name: group.name || group.preferred_username) |> HTML.raw(), - href: Routes.feed_url(Endpoint, :actor, group.preferred_username, :atom) + href: + Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), :atom) ), Tag.tag(:link, rel: "alternate", type: "text/calendar", title: gettext("%{name}'s feed", name: group.name || group.preferred_username) |> HTML.raw(), - href: Routes.feed_url(Endpoint, :actor, group.preferred_username, :ics) + href: + Routes.feed_url( + Endpoint, + :actor, + Actor.preferred_username_and_domain(group), + :ics + ) + ), + Tag.tag(:link, + rel: "alternate", + type: "application/activity+json", + href: group.url ) ] end + @spec add_canonical(list(Tag.t()), Actor.t()) :: list(Tag.t()) + defp add_canonical(tags, %Actor{url: group_url}) do + tags ++ [Tag.tag(:link, rel: "canonical", href: group_url)] + end + + @spec maybe_add_no_index(list(Tag.t()), Actor.t()) :: list(Tag.t()) + defp maybe_add_no_index(tags, %Actor{domain: nil}), do: tags + + defp maybe_add_no_index(tags, %Actor{}) do + tags ++ [Tag.tag(:meta, name: "robots", content: "noindex")] + end + # Insert JSON-LD schema by hand because Tag.content_tag wants to escape it defp json(%Actor{} = group) do "group.json" diff --git a/test/service/metadata/metadata_test.exs b/test/service/metadata/metadata_test.exs index d3455bc7c..64f0e3405 100644 --- a/test/service/metadata/metadata_test.exs +++ b/test/service/metadata/metadata_test.exs @@ -12,12 +12,13 @@ defmodule Mobilizon.Service.MetadataTest do import Mobilizon.Factory describe "build_tags/2 for an actor" do + # TODO : Refactor me with DOM assertions, like the event test below test "that is a group gives tags" do - %Actor{} = group = insert(:group, name: "My group") + %Actor{} = group = insert(:group, name: "My group", domain: "remote.domain") assert group |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() == String.trim(""" - + """) assert group @@ -25,7 +26,7 @@ defmodule Mobilizon.Service.MetadataTest do |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() == String.trim(""" - + """) end