From 279badb2bf29ef8ced8296f7008b2924f0084b5b Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 21 Apr 2022 17:31:47 +0200 Subject: [PATCH] Refactor Mobilizon.Federation.ActivityPub.Types.Events tags handling Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/types/events.ex | 29 ++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/federation/activity_pub/types/events.ex b/lib/federation/activity_pub/types/events.ex index 2016a417c..6d0546fe2 100644 --- a/lib/federation/activity_pub/types/events.ex +++ b/lib/federation/activity_pub/types/events.ex @@ -268,14 +268,7 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Events do description: description, mentions: mentions, # Exclude tags with length > 40 - tags: - Enum.filter(tags, fn tag -> - case tag do - # For some reason transmogrifier gives us this - %{title: tag} -> String.length(tag) < 40 - tag -> String.length(tag) < 40 - end - end) + tags: Enum.filter(tags, &exclude_too_long_tags/1) }) else args @@ -287,13 +280,7 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Events do args, :tags, [], - &Enum.uniq_by(&1, fn tag -> - case tag do - # For some reason transmogrifier gives us this - %{title: tag} -> String.downcase(tag) - tag -> String.downcase(tag) - end - end) + &Enum.uniq_by(&1, fn tag -> tag |> tag_to_string() |> String.downcase() end) ) # Check that we can only allow anonymous participation if our instance allows it @@ -315,4 +302,16 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Events do |> Map.update(:tags, [], &ConverterUtils.fetch_tags/1) |> Map.update(:contacts, [], &ConverterUtils.fetch_actors/1) end + + @spec exclude_too_long_tags(%{title: String.t()} | String.t()) :: boolean() + defp exclude_too_long_tags(tag) do + tag + |> tag_to_string() + |> String.length() + |> Kernel.<(40) + end + + @spec tag_to_string(%{title: String.t()} | String.t()) :: String.t() + defp tag_to_string(%{title: tag}), do: tag + defp tag_to_string(tag), do: tag end